Nginx + fastCGI = 502 bad gateway

Greetings

I’m trying to setup Nagios web interface to work with Nginx, but I’m
showed a 502 Bad Gateway just after login.

This is the nginx log (10.82.1.158 is the server and 10.82.1.106 is the
client):

==> /var/log/nginx/access.https.log <==
10.82.1.106 - swissmsnagios [02/Aug/2011:17:19:15 +0200] “GET
/cgi-bin/nagios3/tac.cgi HTTP/1.1” 502 173
https://10.82.1.158/nagios3/side.html” “Mozilla/5.0 (X11; Linux x86_64)
AppleWebKit/534.30 (KHTML, like Gecko) Ubuntu/11.04
Chromium/12.0.742.112 Chrome/12.0.742.112 Safari/534.30”

==> /var/log/nginx/error.https.log <==
2011/08/02 17:19:15 [warn] 1161#0: *57 using uninitialized “script”
variable, client: 10.82.1.106, server: vb5, request: “GET
/cgi-bin/nagios3/tac.cgi HTTP/1.1”, host: “10.82.1.158”, referrer:
https://10.82.1.158/nagios3/side.html
2011/08/02 17:19:15 [warn] 1161#0: *57 using uninitialized “path_info”
variable, client: 10.82.1.106, server: vb5, request: “GET
/cgi-bin/nagios3/tac.cgi HTTP/1.1”, host: “10.82.1.158”, referrer:
https://10.82.1.158/nagios3/side.html
2011/08/02 17:19:15 [error] 1161#0: *57 connect() failed (111:
Connection refused) while connecting to upstream, client: 10.82.1.106,
server: vb5, request: “GET /cgi-bin/nagios3/tac.cgi HTTP/1.1”, upstream:
“fastcgi://127.0.0.1:49233”, host: “10.82.1.158”, referrer:
https://10.82.1.158/nagios3/side.html

Relevant entry at Nginx configuration:

location ~ ^/cgi-bin/nagios3($|/.*) {
    alias /usr/lib/cgi-bin/nagios3$1;

    fastcgi_pass 127.0.0.1:49233;
    fastcgi_param SCRIPT_FILENAME

$document_root$fastcgi_script_name;

    auth_basic "Nagios Access";
    auth_basic_user_file /etc/nagios3/htpasswd.users;

    fastcgi_param AUTH_USER $remote_user;
    fastcgi_param REMOTE_USER $remote_user;

    fastcgi_param QUERY_STRING $query_string;
    fastcgi_param REQUEST_METHOD $request_method;
    fastcgi_param CONTENT_TYPE $content_type;
    fastcgi_param CONTENT_LENGTH $content_length;
    fastcgi_param SCRIPT_NAME $script;
    fastcgi_param PATH_INFO $path_info;
    fastcgi_param REQUEST_URI $request_uri;
    fastcgi_param DOCUMENT_URI $document_uri;
    fastcgi_param DOCUMENT_ROOT $document_root;
    fastcgi_param SERVER_PROTOCOL HTTP/1.0;
    fastcgi_param GATEWAY_INTERFACE CGI/1.1;
    fastcgi_param SERVER_SOFTWARE nginx;
    fastcgi_param REMOTE_ADDR $remote_addr;
    fastcgi_param REMOTE_PORT $remote_port;
    fastcgi_param SERVER_ADDR $server_addr;
    fastcgi_param SERVER_PORT $server_port;
    fastcgi_param SERVER_NAME $server_name;
}

Things tested:

  • Comment the alias sentence
    (alias + fastcgi)
  • Switch from “fastcgi_pass 127.0.0.1:49233;” to “fastcgi_pass
    127.0.0.1:3590;” (as netstat showed)

Additional info:

$ nginx -V
nginx version: nginx/0.7.65
TLS SNI support enabled
configure arguments: --conf-path=/etc/nginx/nginx.conf
–error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid
–lock-path=/var/lock/nginx.lock
–http-log-path=/var/log/nginx/access.log
–http-client-body-temp-path=/var/lib/nginx/body
–http-proxy-temp-path=/var/lib/nginx/proxy
–http-fastcgi-temp-path=/var/lib/nginx/fastcgi --with-debug
–with-http_stub_status_module --with-http_flv_module
–with-http_ssl_module --with-http_dav_module
–with-http_gzip_static_module --with-http_realip_module --with-mail
–with-mail_ssl_module --with-ipv6
–add-module=/build/buildd/nginx-0.7.65/modules/nginx-upstream-fair

$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.04
DISTRIB_CODENAME=lucid
DISTRIB_DESCRIPTION=“Ubuntu 10.04.3 LTS”

$ dpkg -l | grep cgi
ii fcgiwrap 1.0-1
simple server to run CGI applications over F
ii libfcgi0ldbl 2.4.0-8
Shared library of FastCGI
ii nagios3-cgi 3.2.0-4ubuntu2.2
cgi files for nagios3
ii php5-cgi 5.3.2-1ubuntu4.9
server-side, HTML-embedded scripting languag
ii spawn-fcgi 1.6.3-1
A fastcgi process spawner

$ sudo netstat -anp | grep cgi
tcp 0 0 127.0.0.1:9001 0.0.0.0:*
LISTEN 686/php-cgi
tcp 0 0 127.0.0.1:9002 0.0.0.0:*
LISTEN 692/php-cgi
unix 2 [ ACC ] STREAM LISTENING 3590 738/fcgiwrap
/var/run/fcgiwrap.socket

Thanks in advance.

Posted at Nginx Forum:

On Wed, Aug 3, 2011 at 3:51 PM, dekar411 [email protected] wrote:

Greetings

I’m trying to setup Nagios web interface to work with Nginx, but I’m
showed a 502 Bad Gateway just after login.

according to your netstat output:

unix 2 [ ACC ] STREAM LISTENING 3590 738/fcgiwrap
/var/run/fcgiwrap.socket

you should set fastcgi_pass to fcgiwrap socket path (3590 is “I-Node”
column, not listening port):

location ~ ^/cgi-bin/nagios3($|/.*) {
alias /usr/lib/cgi-bin/nagios3$1;

  •    fastcgi_pass 127.0.0.1:49233;
    
  •    fastcgi_pass unix:/var/run/fcgiwrap.socket;
    

Thanks for the fast and clear answer.

You are right, the change you sugested fixes the problem.

I am as amazed as grateful.

Thanks a lot.

Posted at Nginx Forum: