Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

377
Views
How do I fix this NGINX 502 Bad Gateway error?

I'm working on web sockets in an angular app. I have it connect to a python back-end through nginx. I'm find that I'm getting 502 "Bad Gateway" errors about 90% of the time. I'll do this:

  1. Load page in browser and connect web socket
  2. Python back-end sends data to angular front-end
  3. Disconnect web socket
  4. Attempt to re-connect web socket <-- 502 Bad Gateway error
  5. Hard-reload in Chrome
  6. Load page in browser and connect web socket <-- No 502 error

I can't figure out why this is happening. I can't tell why I'm getting a 502 error. Nor can I figure out why doing a hard-reload fixes the problem. Things I've tried:

  • Increase nginx log-level to debug. Still the logs don't have any useful information.
  • I don't keep any web socket objects in state. I do this in case something is being cached somewhere.
  • I always close the web socket with close code 1000
  • I manually run the python service on the server so that I can watch it. When the 502 error happens, the service doesn't show anything unusual.
  • Setting the nginx max_fails to 0. Setting the fail_timeout to 0. Neither of these changes seems to have any effect. (I found this suggestion in other SO answers)

What should I be looking for that will help me fix this problem?

EDIT: Here's my nginx conf.d file:

server {
  listen  80;

  index index.html;
  root /var/www/mysite;

  location / {
    access_log  /var/log/nginx/mysite/ui.access.log;
    error_log /var/log/nginx/mysite/ui.error.log;
    try_files $uri $uri/ /index.html;
  }

  location /ws/ {
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_http_version 1.1;
    proxy_set_header Host $proxy_host;
    proxy_pass http://WEBSOCKET/;
    access_log  /var/log/nginx/mysite/ws_services.access.log;
    error_log /var/log/nginx/mysite/ws_services.error.log;
    proxy_read_timeout 300s;
  }
}

upstream WEBSOCKET {
    ip_hash;
    server 127.0.0.1:8765;
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Not the same problem the OP had, but just in case anyone comes across this and has the same setup as I had:

I was using WebSockets over SSL (so wss:// protocol) and had 502 popping up, even though the config had worked before. The config was as follows:

...
proxy_pass http://127.0.0.1:8080;
...

In the backend I was using a Node with the ws package to create a websocket server

As I said: It was working before, but suddenly stopped working. Additionally nginx wrote upstream prematurely closed connection while reading response header from upstream errors into the error log. I suppose that either nginx or node closed some kind of security issue, that lead to the setup not working anymore.

What I had to do in order to make it work, was to use https instead of http for the proxy_pass config

...
proxy_pass https://127.0.0.1:8080;
...
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!