Keith John Hutchison
Keith John Hutchison

Reputation: 5277

How to set DOCUMENT_ROOT and SCRIPT_NAME correctly for fcgiwrap

I've got a simple script cpuinfo.sh that works and is executable.

I'm getting an error

*224 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: 86.44.146.39, server: staging.example.com, request: "GET /cpuinfo.sh HTTP/1.1", upstream: "fastcgi://unix:/var/run/fcgiwrap.socket:", host: "staging.example.com"

the nginx settings are

location ~ (\.cgi|\.py|\.sh|\.pl|\.lua)$ {
    gzip off;
    autoindex on;
    fastcgi_pass unix:/var/run/fcgiwrap.socket;
    include fastcgi_params;
    fastcgi_param DOCUMENT_ROOT /home/balance/balance-infosystems-web/scripts/;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

I'm expecting fcgiwrap to execute

/home/balance/balance-infosystems-web/scripts/cpuinfo.sh 

I hard coded the script path to debug but I'm still getting the same error.

location ~ (\.cgi|\.py|\.sh|\.pl|\.lua)$ {
    gzip off;
    autoindex on;
    fastcgi_pass unix:/var/run/fcgiwrap.socket;
    include fastcgi_params;
    fastcgi_param DOCUMENT_ROOT /home/balance/balance-infosystems-web/scripts/;
    # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param SCRIPT_FILENAME /home/balance/balance-infosystems-web/scripts/cpuinfo.sh;
}

What needs to be changed in the nginx server config to execute the script correctly?

Upvotes: 5

Views: 14441

Answers (4)

Bojan
Bojan

Reputation: 23

I can confirm that fcgiwrap was producing "Cannot get script name, are DOCUMENT_ROOT and SCRIPT_NAME (or SCRIPT_FILENAME) set and is the script executable?" error for me as well. It turns out that in my case SCRIPT_FILENAME was properly set, however I disregarded the fact that /cgi-bin/ was passed on as well.

Really bad logging on behalf of fcgiwrap, as it makes you think variable is not set at all, while it can not find the file, or has permissions problems. Definitely not granular enough.

As suggested above

strace -f -e trace=file -p $(pidof fcgiwrap)

did the trick for me.

Upvotes: 2

Peter Frost
Peter Frost

Reputation: 561

As others have mentioned this can be a pain to debug, but strace is super helpful:

strace -f -e trace=lstat64 -p $(pidof fcgiwrap)

Tells strace to follow forks -f, only trace syscalls to lstat64, and tells it to attach to the PID -p of fcgiwrap. You should get an output along the lines of:

[pid  1918] lstat64("{The file fastcgi is trying to load}", 0xbee94a50) = -1 ENOENT (No such file or directory)

Upvotes: 6

Ruud Harmsen
Ruud Harmsen

Reputation: 61

I had the same problem. After too many hours of searching in the wrong direction, I finally found the cause. In hindsight the solution was here all the time, right above, but I realise that only now.

For some reason in my case DOCUMENT_ROOT had the value /var/www/cgi-bin, and SCRIPT_NAME was /cgi-bin/somescript.cgi . So if you put those together in the usual way, by writing fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name , SCRIPT_FILENAME gets set to /var/www/cgi-bin/cgi-bin/somescript.cgi, which is a bit overdone and therefore doesn't work. The fix was to put: fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name

Now if only FastCGI, in the error.log via fcgiwrap, had not WONDERED if the variables were set, but instead had TOLD me what they WERE set to, I'd have seen the solution immediately. The golden rule of diagnostic information: be specific and precise.

I only found out after running a printenv script I had lying about somewhere, first stating its SCRIPT_FILENAME explicitly, to be able to run it in the first place.

Upvotes: 6

Keith John Hutchison
Keith John Hutchison

Reputation: 5277

I discovered that DOCUMENT_ROOT can not be reset. I normally have scripts directories away from publicly accessible paths. I knew that the scripts directory was the same level the web directory so I tried.

location ~ (\.cgi|\.py|\.sh|\.pl|\.lua)$ {
    gzip off;
    autoindex on;
    fastcgi_pass unix:/var/run/fcgiwrap.socket;
    fastcgi_param SCRIPT_FILENAME $document_root/../scripts/$fastcgi_script_name;
    include fastcgi_params ;
}

which resolved the issue.

Upvotes: 3

Related Questions