Question

502 Bad Gateway

Posted March 17, 2020 146 views
Nginx
server {  
    listen 80;
    server_name example1.com;
    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}


server {  
    listen 80;
    server_name example2.com;
    location / {
        proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}
Error Log :
2020/03/17 14:13:44 [error] 1880#1880: *88 connect() failed (111: Connection refused) while connecting to upstream, client: 157.37.238.120, server: example1.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:3000/", host: "www.example1.com"
2020/03/17 14:13:44 [error] 1880#1880: *88 connect() failed (111: Connection refused) while connecting to upstream, client: 157.37.238.120, server: example.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:3000/favicon.ico", host: "www.example1.com", referrer: "http://www.example1.com/"
edited by MattIPv4

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
2 answers

First observation. Based on your config, I assume you’re trying to proxy WebSocket traffic. Your logs look like you’re testing from a browser.For testing, try using wscat.

To fix the connection issue, you should ensure that your servers are running and bound to the correct ports. You can check with ‘lsof -i -n’

You can refer to the solution provided in our Community:

https://www.digitalocean.com/community/questions/502-bad-gateway-nginx-2

Cheers,
Sri Charan

Submit an Answer