5

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;
}
user2023861
  • 8,030
  • 9
  • 57
  • 86
  • What's the stack for the Python service? Both the framework on the Python side of things and the app server running it? – AKX Apr 17 '20 at 20:03
  • It's Python 3.6 on CentOS Linux. I'm importing just the standard websockets library. I'm actually running the command manually, though I usually run it as a daemon. Is this what you mean? – user2023861 Apr 17 '20 at 20:18
  • There is no module in the Python standard library that provides websockets. You will have to provide some (preferably minimal) code here. – AKX Apr 17 '20 at 20:39
  • @AKX I have this line in my requirements.txt file `websockets==8.0.1` I'll include my nginx code as an edit to my question – user2023861 Apr 20 '20 at 13:09
  • Which app server is running your app? (The process that's listening on port 8765.) – AKX Apr 21 '20 at 07:53
  • I have python code that's listening to port 8765. I'm running it manually in order to watch is closely. I'll usually run it in systemd. – user2023861 Apr 21 '20 at 13:44
  • ... And what do you do in that Python code to have it listen on port 8765? – AKX Apr 21 '20 at 13:45

1 Answers1

3

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;
...
Dominik Ehrenberg
  • 1,533
  • 1
  • 15
  • 12
  • 1
    I had exactly the same problem and tried your solution. In the process, I noticed another error that caused the container to restart itself relatively frequently. After fixing this other problem, I switched back from HTTPS to HTTP for fun. Lo and behold: the problem is gone. – qräbnö Jan 19 '21 at 21:47
  • Fixed it, thx. In my case, nodejs app was started as a SSL app, and nginx sent to http instead of https. – mFlorin Jul 21 '22 at 20:45