linux - 需要有关 Nginx CGI 配置的帮助

标签 linux nginx cgi fastcgi

我需要设置 nginx 配置,这样 URL“ http://host/cgi-bin/hw.sh/some/path/to/data/ ”应该触发路径“/usr/lib/cgi-”下的 shell 脚本“hw.sh” bin/”。

现在,根据页面https://www.howtoforge.com/serving-cgi-scripts-with-nginx-on-debian-squeeze-ubuntu-11.04-p3中提到的说明,我们需要在“.vhost”文件下设置配置。但是我在路径“/etc/nginx/sites-available/default”下已经有一个默认文件,而不是 .vhost 文件。

当我使用相同的配置时,我会收到 HTTP/1.1 403 Forbidden 错误。我已确保该脚本也需要可执行权限。以下是 nginx 日志中收到的错误。

FastCGI sent in stderr: "Cannot get script name, are DOCUMENT_ROOT and SCRIPT_NAME (or SCRIPT_FILENAME) set and is the script executable?"
while reading response header from upstream, 
client: host_ip, server: localhost, 
request: "HEAD /cgi-bin/hw.sh/some/path/to/data/ HTTP/1.1", 
upstream: "fastcgi://unix:/var/run/fcgiwrap.socket:", host: "host_ip"

我需要帮助编写正确的配置,以便我上面的 URL 执行上述路径下的 hw.sh 脚本并返回正确的输出。有人可以帮我解决这个问题吗?

下面是我在默认文件下使用的配置。

server {
        listen 80 default_server;
[...]    
location /cgi-bin/ {
         # Disable gzip (it makes scripts feel slower since they have to complete
         # before getting gzipped)
         gzip off;
         # Set the root to /usr/lib (inside this location this means that we are
         # giving access to the files under /usr/lib/cgi-bin)
         root  /usr/lib;
         # Fastcgi socket
         fastcgi_pass  unix:/var/run/fcgiwrap.socket;
         # Fastcgi parameters, include the standard ones
         include /etc/nginx/fastcgi_params;
         # Adjust non standard parameters (SCRIPT_FILENAME)
         fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
       }
[...]
}

最佳答案

行“fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;”在配置中导致了问题。 当我将其更改为“fastcgi_param SCRIPT_FILENAME $request_filename;”时,一切都按预期工作。

关于linux - 需要有关 Nginx CGI 配置的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40465186/

相关文章:

linux - 为什么具有相同 UTC 偏移量的两个时区的时间戳不同?

linux - sudo -u 用户名通知发送卡住

linux - 将最后 n 行复制到新文件,然后从原始文件中删除 n 行

ssl - 如何从网站的 SSL 测试结果中消除错误 "TLS_FALLBACK_SCSV not supported"

javascript - 使用 XMLHttpRequest 将数据发布到 CGI 文件会导致 BadHeader

linux - 如何在groupwise中更改多个用户的密码?

linux - 将两个 asp.net 核心应用程序部署到 nginx 的一个解决方案中

redirect - Lumen + nginx = error 500, rewrite or internal redirection cycle while internally redirecting to "/index.php"

linux - 在运行 Apache 服务器的 Ubuntu 机器上播放 VLC,无法播放

python cgi + 用于运行批处理命令的网络界面?