# Nginx + fastCGI = 502 bad gateway

**URL:** <https://www.ruby-forum.com/t/nginx-fastcgi-502-bad-gateway/210255>\
**Category:** NGINX\
**Created:** [August 3, 2011, 10:52am UTC](https://www.ruby-forum.com/t/nginx-fastcgi-502-bad-gateway/210255 "2011-08-03T10:52:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [August 3, 2011, 10:52am UTC](https://www.ruby-forum.com/t/nginx-fastcgi-502-bad-gateway/210255/1 "2011-08-03T10:52:26Z")

</div>

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](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](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](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](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](http://forum.nginx.org/read.php?2,212501,212501))
- 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:

> **[Nginx + fastCGI = 502 bad gateway](https://forum.nginx.org/read.php?2,213277,213277#msg-213277)**
>
> 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 \<==

---

<div class="post-metadata">

**Author:** ![dekar411](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@dekar411](https://www.ruby-forum.com/u/dekar411)\
**Post date:** [August 3, 2011, 11:03am UTC](https://www.ruby-forum.com/t/nginx-fastcgi-502-bad-gateway/210255/2 "2011-08-03T11:03:34Z")

</div>

On Wed, Aug 3, 2011 at 3:51 PM, dekar411 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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;

```

---

<div class="post-metadata">

**Author:** ![dekar411](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@dekar411](https://www.ruby-forum.com/u/dekar411)\
**Post date:** [August 3, 2011, 4:57pm UTC](https://www.ruby-forum.com/t/nginx-fastcgi-502-bad-gateway/210255/3 "2011-08-03T16:57:02Z")

</div>

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:

> **[Re: Nginx + fastCGI = 502 bad gateway](https://forum.nginx.org/read.php?2,213277,213315#msg-213315)**
>
> 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 \<==
