Why are my CGI scripts not executed like PHP ?

Ralph Seichter m16+nginx at monksofcool.net
Sat Apr 7 18:13:32 UTC 2018


On 07.04.18 16:18, Francis Daly wrote:

> This mail is a bit long, but I try to cover the points raised in your
> previous mails too.

I appreciate you taking the time. Like I said, I am new to nginx. Years
of using Apache caused me to expect certain things to happen in certain
ways, and even though I studied nginx documentation and already noted
substantial differences, I'm glad for your thorough description. One
sentence in particular got me thinking:

> Perhaps /tmp/script is not executable by the fcgiwrap user, or does
> not provide correct CGI output when run in this limited environment.

Yesterday I had verified that the CGI test script was executable for
all, ran it with "su nginx -c /path/to/test.cgi", and then basically
forgot about the script, to focus all my attention on nginx, fcgiwrap,
and the other tools in my box.

Turns out that the CGI shell script I quickly typed in Vi lacks a small
but significant detail. https://tools.ietf.org/html/rfc3875 section 6.2
states "The response comprises a message-header and a message-body,
separated by a blank line", and unfortunately I omitted that blank line.

Seeing that, the error message I included in yesterday's email makes
more sense to me: "Upstream prematurely closed FastCGI stdout while
reading response header". With the blank line absent, all returned data
was considered message-header, and when the stream was closed, no
message-body had apparently been received.

As soon as I added the missing blank line to my test.cgi, all worked
smoothly. Here's the relevant section of my nginx configuration:

  server {
    listen *:443 ssl;
    server_name test.mydomain.tld;
    # ...logging and basic SSL stuff here...

    root /var/www/localhost/test;
    index test.cgi;
    location ~ \.cgi$ {
      try_files $uri =404;
      include fastcgi_params;
      fastcgi_pass unix:/run/fcgi-nginx-1;
    }
  }

Doesn't look like much, and according to Git, that's actually what I
used on my very first attempt with spawn-fcgi. I sure wish I had spotted
the script problem earlier. Face, meet palm.

-Ralph


More information about the nginx mailing list