• If you are still using CentOS 7.9, it's time to convert to Alma 8 with the free centos2alma tool by Plesk or Plesk Migrator. Please let us know your experiences or concerns in this thread:
    CentOS2Alma discussion
  • Inviting everyone to the UX test of a new security feature in the WP Toolkit
    For WordPress site owners, threats posed by hackers are ever-present. Because of this, we are developing a new security feature for the WP Toolkit. If the topic of WordPress website security is relevant to you, we would be grateful if you could share your experience and help us test the usability of this feature. We invite you to join us for a 1-hour online session via Google Meet. Select a convenient meeting time with our friendly UX staff here.

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