# Nginx/0.8.54 - 502 Bad Gateway

**URL:** <https://www.ruby-forum.com/t/nginx-0-8-54-502-bad-gateway/209691>\
**Category:** NGINX\
**Created:** [July 20, 2011, 3:43pm UTC](https://www.ruby-forum.com/t/nginx-0-8-54-502-bad-gateway/209691 "2011-07-20T15:43:40Z")\
**Posts on this page:** 12\
**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:** [July 20, 2011, 3:43pm UTC](https://www.ruby-forum.com/t/nginx-0-8-54-502-bad-gateway/209691/1 "2011-07-20T15:43:40Z")

</div>

Hi,

I’m running nginx as a reverse proxy for a jetty. I host a Liftweb App  
on localhost:8080. Nginx is listening on Port 81.

Here is my nginx.conf:

server {  
listen 81;  
server\_name [mysite.com](http://mysite.com) [www.mysite.com](http://www.mysite.com);

```
            location / {
                    proxy_pass http://127.0.0.1:8080;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_read_timeout 700;
                    proxy_set_header Host $http_host;
                    proxy_set_header X-Forwarded-For

```

$proxy\_add\_x\_forwarded\_for;  
}  
}

When I try to point my browser to [mysite.com:81](http://mysite.com:81) I always get a 502 Bad  
Gateway message. I checked the log and found this:

connect() failed (111: Connection refused) while connecting to upstream,  
client: 12.34.567.890, server: [mysite.com](http://mysite.com), request: “GET /favicon.ico  
HTTP/1.1”, upstream: “[http://127.0.0.1:8080/favicon.ico](http://127.0.0.1:8080/favicon.ico)”, host:  
“[mysite.com:81](http://mysite.com:81)”

Does this means this port is blocked? I tried other but the result is  
still the same…

My hosting company gave me the advice better not to use port 80 (i’m in  
china) and use some socket instead. I don’t know what they mean by that.  
I use port 81 for nginx now. Anyone an idea what they could mean by “use  
sockets”? I read somewhere that i can later forward all traffic from 81  
to port 80.

Any hints a very much appreciated.  
Thanks.

Posted at Nginx Forum:

> **[nginx/0.8.54 - 502 Bad Gateway](https://forum.nginx.org/read.php?2,212680,212680#msg-212680)**
>
> Hi, I'm running nginx as a reverse proxy for a jetty. I host a Liftweb App on localhost:8080. Nginx is listening on Port 81. Here is my nginx.conf: server { listen 81; server\_name mysite.com www.mysite.com;

---

<div class="post-metadata">

**Author:** ![nginxn00b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f4b2a3/32.png) [@nginxn00b](https://www.ruby-forum.com/u/nginxn00b)\
**Post date:** [July 20, 2011, 6:00pm UTC](https://www.ruby-forum.com/t/nginx-0-8-54-502-bad-gateway/209691/2 "2011-07-20T18:00:30Z")

</div>

On 20 Jul 2011 14h43 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> When I try to point my browser to mysite.com:81 I always get a 502  
> Bad Gateway message. I checked the log and found this:

There’s a problem in your upstream. Its refusing the connection? Do  
you have some sort of access controls on it?

Try:

curl -I [http://localhost:8080](http://localhost:8080) and see if you can connect.

> connect() failed (111: Connection refused) while connecting to  
> upstream, client: 12.34.567.890, server: [mysite.com](http://mysite.com), request: “GET  
> /favicon.ico HTTP/1.1”, upstream:  
> “[http://127.0.0.1:8080/favicon.ico](http://127.0.0.1:8080/favicon.ico)”, host: “mysite.com:81”

> Does this means this port is blocked? I tried other but the result  
> is still the same…
> 
> My hosting company gave me the advice better not to use port 80 (i’m  
> in china) and use some socket instead. I don’t know what they mean  
> by that. I use port 81 for nginx now. Anyone an idea what they  
> could mean by “use sockets”? I read somewhere that i can later  
> forward all traffic from 81 to port 80.

If you want it to work transparently use port 80 for your frontend.

— appa

---

<div class="post-metadata">

**Author:** ![nginxn00b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f4b2a3/32.png) [@nginxn00b](https://www.ruby-forum.com/u/nginxn00b)\
**Post date:** [July 20, 2011, 6:19pm UTC](https://www.ruby-forum.com/t/nginx-0-8-54-502-bad-gateway/209691/3 "2011-07-20T18:19:03Z")

</div>

Access control? Not that I know of. I didn’t touch the firewall yet, so  
I guess there is none enable right now.

> > curl -I [http://localhost:8080](http://localhost:8080) and see if you can connect.

I did this on the server and I got:  
curl -I [http://localhost:8080](http://localhost:8080)  
curl: (7) couldn’t connect to host

And I tried from a remote client to connect via curl and got:

curl -I [http://myside.com:81](http://myside.com:81)  
HTTP/1.1 502 Bad Gateway  
Server: nginx/0.8.54  
Date: Wed, 20 Jul 2011 16:14:04 GMT  
Content-Type: text/html  
Content-Length: 172  
Connection: keep-alive

Any clue?

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,212680,212691#msg-212691](http://forum.nginx.org/read.php?2,212680,212691#msg-212691)

---

<div class="post-metadata">

**Author:** ![nginxn00b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f4b2a3/32.png) [@nginxn00b](https://www.ruby-forum.com/u/nginxn00b)\
**Post date:** [July 20, 2011, 6:27pm UTC](https://www.ruby-forum.com/t/nginx-0-8-54-502-bad-gateway/209691/4 "2011-07-20T18:27:45Z")

</div>

> > curl -I [http://localhost:8080](http://localhost:8080) and see if you can connect.

> I did this on the server and I got:  
> curl -I [http://localhost:8080](http://localhost:8080)  
> curl: (7) couldn’t connect to host

Your backend / application isn’t working that’s why nginx can’t connect  
to it and has to return 502.

rr

---

<div class="post-metadata">

**Author:** ![nginxn00b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f4b2a3/32.png) [@nginxn00b](https://www.ruby-forum.com/u/nginxn00b)\
**Post date:** [July 20, 2011, 6:44pm UTC](https://www.ruby-forum.com/t/nginx-0-8-54-502-bad-gateway/209691/5 "2011-07-20T18:44:30Z")

</div>

Er, wrong email. Sorry!

---

<div class="post-metadata">

**Author:** ![nginxn00b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f4b2a3/32.png) [@nginxn00b](https://www.ruby-forum.com/u/nginxn00b)\
**Post date:** [July 20, 2011, 6:40pm UTC](https://www.ruby-forum.com/t/nginx-0-8-54-502-bad-gateway/209691/6 "2011-07-20T18:40:08Z")

</div>

all set, my fault.

---

<div class="post-metadata">

**Author:** ![nginxn00b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f4b2a3/32.png) [@nginxn00b](https://www.ruby-forum.com/u/nginxn00b)\
**Post date:** [July 21, 2011, 9:07am UTC](https://www.ruby-forum.com/t/nginx-0-8-54-502-bad-gateway/209691/7 "2011-07-21T09:07:41Z")

</div>

Hello,

First replace  
proxy\_pass [http://127.0.0.1:8080](http://127.0.0.1:8080);  
by  
proxy\_pass [http://monsite.com:8080](http://monsite.com:8080);

If that doesn’t work, look at firewall settings, yes. Port 8080 on the  
upstream must be accessible from the machine running nginx (even if  
they are the same physical machine).

A.

---

<div class="post-metadata">

**Author:** ![nginxn00b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f4b2a3/32.png) [@nginxn00b](https://www.ruby-forum.com/u/nginxn00b)\
**Post date:** [July 21, 2011, 6:41am UTC](https://www.ruby-forum.com/t/nginx-0-8-54-502-bad-gateway/209691/8 "2011-07-21T06:41:09Z")

</div>

> > Your backend / application isn’t working that’s why nginx can’t  
> > connect to it and has to return 502.

The app is up and running on port 8080. I can access the app directly  
using mysite.com:8080. Do I have to configure something else, maybe the  
firewall?

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,212680,212719#msg-212719](http://forum.nginx.org/read.php?2,212680,212719#msg-212719)

---

<div class="post-metadata">

**Author:** ![nginxn00b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f4b2a3/32.png) [@nginxn00b](https://www.ruby-forum.com/u/nginxn00b)\
**Post date:** [July 21, 2011, 12:01pm UTC](https://www.ruby-forum.com/t/nginx-0-8-54-502-bad-gateway/209691/9 "2011-07-21T12:01:46Z")

</div>

Did you try to just have your nginx listen on port 80 and see if it  
works ?

A.

---

<div class="post-metadata">

**Author:** ![nginxn00b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f4b2a3/32.png) [@nginxn00b](https://www.ruby-forum.com/u/nginxn00b)\
**Post date:** [July 21, 2011, 11:10am UTC](https://www.ruby-forum.com/t/nginx-0-8-54-502-bad-gateway/209691/10 "2011-07-21T11:10:57Z")

</div>

> > First replace  
> > proxy\_pass [http://127.0.0.1:8080](http://127.0.0.1:8080);  
> > by  
> > proxy\_pass [http://monsite.com:8080](http://monsite.com:8080);

Gee, so simple! Yes, that worked. Thank you very much!! To finish  
everything I need to forward port 81 to port 80. I guess a firewall rule  
will help me out here.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,212680,212734#msg-212734](http://forum.nginx.org/read.php?2,212680,212734#msg-212734)

---

<div class="post-metadata">

**Author:** ![nginxn00b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f4b2a3/32.png) [@nginxn00b](https://www.ruby-forum.com/u/nginxn00b)\
**Post date:** [July 21, 2011, 2:06pm UTC](https://www.ruby-forum.com/t/nginx-0-8-54-502-bad-gateway/209691/11 "2011-07-21T14:06:57Z")

</div>

> > Did you try to just have your nginx listen on port 80 and see if it  
> > works ?

Yes, I did. It is not working. I need to whitelist my IP and for that I  
have to do some more paperwork. I’m in China. But my Chinese hosting  
company said I should use Socket (Socks?), but I don’t know what they  
mean by that. My solution was to use another port (like port 81) and  
forward the traffic then. Any other solutions possible?

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,212680,212752#msg-212752](http://forum.nginx.org/read.php?2,212680,212752#msg-212752)

---

<div class="post-metadata">

**Author:** ![nginxn00b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f4b2a3/32.png) [@nginxn00b](https://www.ruby-forum.com/u/nginxn00b)\
**Post date:** [July 22, 2011, 9:25am UTC](https://www.ruby-forum.com/t/nginx-0-8-54-502-bad-gateway/209691/12 "2011-07-22T09:25:34Z")

</div>

I have no clue what they mean when they say you should use Socks. If  
they don’t let TCP traffic go to port 80 on your machine, there is  
nothing you can do but publish your URLs as [http://monsite.com:81/](http://monsite.com:81/)…

Of course, you could also do the paperwork.

A.
