需要有关Nginx CGI配置的帮助

时间:2016-11-07 12:27:01

标签: linux nginx cgi fastcgi

我需要设置nginx配置,以便URL" http://host/cgi-bin/hw.sh/some/path/to/data/ "应该触发shell脚本" hw.sh"在路径下出现" / usr / lib / cgi-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;
       }
[...]
}

1 个答案:

答案 0 :(得分:1)

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