Report this

What is the reason for this report?

nginx with nodejs : 502 bad gateway error

Posted on February 28, 2022

Hi, so I am getting the error as mentioned in the title only for one particular GET request. That GET requests processes data and renders a PDF file.

So, I started my research on this, tried a lot of configuration changes in my nginx config file like

  1. adding increased read timeouts, etc
  2. adding increased proxy buffering
  3. disabling gzip
  4. adding an upstream block and such

This is my first time using nginx. I followed a youtube tutorial to deploy my app on a domain and then add a SSL certificate as well (though have to admit I do understand now the basic concepts of how reverse proxy works and configuring, so I wonder what is it that is causing this issue).

Before this I had deployed the app on the free tier of heroku and I faced no issue with this GET request on this particular URL.

Following is my current configuration file:

upstream localhost:3000 {
	zone upstreams 64K;
	server 127.0.0.1:3000 max_fails=0 fail_timeout=2s;
	keepalive 8;
}

server {
	root /var/www/html;
	index index.html index.htm index.nginx-debian.html;

	server_name digitalawb.in www.digitalawb.in;

	location / {
		proxy_set_header Host $host;
		proxy_set_header X-Real-Ip $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Proto $scheme;

		proxy_pass http://localhost:3000/;
		proxy_read_timeout 90;
		proxy_buffering on;
		proxy_buffer_size 4k;
		proxy_buffers 24 4k;
		proxy_busy_buffers_size 8k;
		proxy_max_temp_file_size 2048m;
		proxy_temp_file_write_size 32k;
		proxy_redirect http://localhost:3000/ https://digitalawb.in/;
	}
	
	listen [::]:443 ssl ipv6only=on; # managed by Certbot
	listen 443 ssl; # managed by Certbot
	ssl_certificate /etc/letsencrypt/live/digitalawb.in/fullchain.pem; 
	ssl_certificate_key /etc/letsencrypt/live/digitalawb.in/privkey.pem;
	include /etc/letsencrypt/options-ssl-nginx.conf;
	ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}

server {
	if ($host = www.digitalawb.in){
		return 301 https://$host$request_uri;
	}

	if ($host = digitalawb.in){
		return 301 https://$host$request_uri;
	}

	listen 80 default_server;
	listen [::]:80 default_server;

	server_name digitalawb.in www.digitalawb.in;
	return 404;
}


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

The 502 error indicates that this is a problem with your Node.js backend service rather than the Nginx configuration itself.

I would recommend checking your Nginx error log to see the actual error:

tail -f /var/log/nginx/error.log

Then after that I would also suggest checking the logs of your Node.js application that is running on port 3000. The issue might be related to the application itself, and the logs could provide more information about the error or potential bottlenecks.

Another thing that you could try out is to make sure that the GET request is sent over HTTPS rather than HTTP, as currently you have a redirect from HTTP to HTTPS which might cause an issue depending on the specifics of the GET request.

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

Dark mode is coming soon.