• Our team is looking to connect with folks who use email services provided by Plesk, or a premium service. If you'd like to be part of the discovery process and share your experiences, we invite you to complete this short screening survey. If your responses match the persona we are looking for, you'll receive a link to schedule a call at your convenience. We look forward to hearing from you!
  • We are looking for U.S.-based freelancer or agency working with SEO or WordPress for a quick 30-min interviews to gather feedback on XOVI, a successful German SEO tool we’re looking to launch in the U.S.
    If you qualify and participate, you’ll receive a $30 Amazon gift card as a thank-you. Please apply here. Thanks for helping shape a better SEO product for agencies!
  • The BIND DNS server has already been deprecated and removed from Plesk for Windows.
    If a Plesk for Windows server is still using BIND, the upgrade to Plesk Obsidian 18.0.70 will be unavailable until the administrator switches the DNS server to Microsoft DNS. We strongly recommend transitioning to Microsoft DNS within the next 6 weeks, before the Plesk 18.0.70 release.
  • The Horde component is removed from Plesk Installer. We recommend switching to another webmail software supported in Plesk.

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