Skip to content
Advertisement

Need help on Nginx CGI Configuration

I need to set the nginx configurations such that the URL “http://host/cgi-bin/hw.sh/some/path/to/data/” should trigger the shell script “hw.sh” present under path “/usr/lib/cgi-bin/“.

Now, according to the instructions mentioned in page https://www.howtoforge.com/serving-cgi-scripts-with-nginx-on-debian-squeeze-ubuntu-11.04-p3, we need to set the configurations under a “.vhost” file. But I have a default file already present under path “/etc/nginx/sites-available/default” instead of a .vhost file.

And when I use the same configurations, I get HTTP/1.1 403 Forbidden error. I have made sure that the script has required executable rights too. Below is the error received in nginx logs.

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"

I need help in writing the correct configuration so that my URL above executes the hw.sh script under the path mentioned above and returns proper output. Could someone please help me out here?

Below is my configuration used under default file.

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;
       }
[...]
}

Advertisement

Answer

Line “fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;” in the configuration was causing the problem. When i changed it to “fastcgi_param SCRIPT_FILENAME $request_filename;”, everything is working as expected.

Advertisement