Welcome! Log In Create A New Profile

Advanced

NPH (non-parsed header) script support?

Posted by neek 
NPH (non-parsed header) script support?
June 01, 2009 06:10PM
Hi, does anyone know how to get NGINX (or any CGI gateway) to support NPH (non-parsed header) CGI scripts? I have a system which I'm trying to integrate to my nginx site but nginx only returns 502 ("Bad gateway"). I've been running lighttpd for years and I specifically decided to try to move to nginx because lightty also doesn't support nph, unfortunately.

I'm willing to use any CGI to FCGI gateway, but I'd really like to be able to use just one that runs anything in the /cgi-bin dir, regardless of whether it's an ELF binary, Python, PHP, etc.

I'm running 0.7.59 on FreeBSD 7.2.

thanks in advance!
Re: NPH (non-parsed header) script support?
June 01, 2009 08:11PM
There is no native support for cgi in nginx so you have to proxy your script. The term "NPH" is an Apache specific term and doesn't matter to nginx (BTW).

There are three main choices. You can rewrite your script as a fastcgi. You can proxy to Apache using mod_perl (preferably) or mod_cgi. Or you can use fcgiwrap - see http://nginx.localdomain.pl/wiki/FcgiWrap. I have an app that is written as an "NPH" app and it runs fine using fcgiwrap. It "speaks" fastcgi to nginx and cgi to your app.

To proxy using Apache you can use something like:

[code]
server {
...
location ~ \.pl$ {
proxy_pass 127.0.0.1:80;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
...
}
[/code]

And set Apache to listen on 127.0.0.1:80 (or whatever port you choose).



For fcgiwrap you would use the fastcgi_pass directive (in this case I am using unix socket not tcp):

[code]
location ~ \.pl$ {
fastcgi_pass unix:/tmp/cgi.sock;
include /usr/local/nginx/conf/fastcgi_params;
}
[/code]

You'll need to invoke multiple instances of fcgiwrap as each will only handle one request at a time, but they consume a minimal amount of memory. The script on the page that I linked to above works fine for that. Just set the $num_children value to whatever you need to run your script.

--
Jim Ohlstein



Edited 3 time(s). Last edit at 06/01/2009 08:12PM by Jim Ohlstein.
Re: NPH (non-parsed header) script support?
June 02, 2009 12:52AM
Hi Jim, Thanks for the quick reply. I'm actually using fcgiwrap (launched via spawn-fcgi), though through a port rather than a socket. fcgiwrap launches the script but I just get back a 502 error... Seems something along the way (the script, fcgiwrap, mod_fcgi, or nginx is doing something which causes the 502.

I'm seeing something weird in the error log but I don't really know what to try next:
[quote]2009/06/01 21:34:53 [error] 3329#0: *1371 upstream closed prematurely FastCGI stdout while reading response header from upstream, client: 192.168.0.1, server: EXAMPLE.com, request: "GET /cgi-bin/SCRIPT?param1=foo&param2=bar HTTP/1.1", upstream: "fastcgi://127.0.0.1:9001", host: "www.EXAMPLE.com"[/quote]
(I changed the site's name and the names/params to the script).

All I've found on the net are suggestions to double-check the fastcgi config but afaik, it's correct (though it's entirely likely it's not, since I'm very new to nginx).

Any tips about how to debug where this is failing? Thanks again.
Re: NPH (non-parsed header) script support?
June 02, 2009 02:01AM
Argh, I figured it out. Apparently fcgiwrap is very strict about wanting Content-Type as the first line of the response. I changed around the printfs and got it to work. Thanks for the suggestion, it led me to the answer! :)
Re: NPH (non-parsed header) script support?
June 02, 2009 11:01AM
The only problem with using spawn-fcgi, IIRC, is that it will only create one process (unless that has changed). That's fine for a site that doesn't get many requests. With the script at that site the you can launch multiple instances of fcgiwrap and handle multiple concurrent requests.

--
Jim Ohlstein
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 287
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready