• Please be aware: Kaspersky Anti-Virus has been deprecated
    With the upgrade to Plesk Obsidian 18.0.64, "Kaspersky Anti-Virus for Servers" will be automatically removed from the servers it is installed on. We recommend that you migrate to Sophos Anti-Virus for Servers.
  • The Horde webmail has been deprecated. Its complete removal is scheduled for April 2025. For details and recommended actions, see the Feature and Deprecation Plan.
  • We’re working on enhancing the Monitoring feature in Plesk, and we could really use your expertise! If you’re open to sharing your experiences with server and website monitoring or providing feedback, we’d love to have a one-hour online meeting with you.

Question Bad Gateway error (502) on nginx

MartinH

New Pleskian
I followed 502 Bad Gateway nginx : How to solve? but can not resolve my issue on Obsidian:
My localhost is listening on port 5000 - netstat -aon shows
Code:
tcp  0  0 127.0.0.1:5000  0.0.0.0:*  LISTEN
and Apache is up and running - (checked with service apache2 status), Fail2Ban is not installed
nginx Proxy Mode is turned off, and an additional nginx directive is set:
Code:
location /api/ {
    proxy_pass http://127.0.0.1:5000;
}
When trying to post (e.g.) example.com/api/login the server responds with Bad Gateway 502.
The log (Websites & domains > Logs) shows
Code:
502    GET /api/login HTTP/1.1
261955#0: *1686 upstream prematurely closed connection while reading response header from upstream

Any help in resolving the issue is greatly appreciated.

Further info:
The project is a MERN stack app, consisting of frontend and backend. Both parts are in separate folders under the same vhost-name. The frontend is reachable without any problem. The backend is a node-server (up and running). I tested to open the port 5000 directly and was able to work with it with a post to example.com:5000/api/login. I chose to setup node directly in the backend (and not via the Plesk interface), as I wanted to keep the separation between front- and backend.
 
Last edited:
Maybe this will help whoever is faced with the same problem:
The upstream error means that there is something wrong in the communication from nginx up to the receiver (in my case nodejs running on localhost).
I was under the (wrong) impression, that the whole site needed to employ SSL and set up node accordingly. That is why I was able to post a https://example.com:5000/api/login from Postman (with the appropiate port opened).
In the production line however I passed all requests through nginx, which was set by my nginx directive to redirect to localhost with http:
Code:
location /api/ {
    proxy_pass http://127.0.0.1:5000;
}
Since node was expecting https the two did not work together properly. I changed node to receive http only (require("http") instead of require("https")) and then everything worked smoothly.
 
Last edited:
Back
Top