nginx 502 gateway error

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.

When i checked the nginx error logs, I see three lines of error throw up each time this URL is hit. Following are the errors

  • upstream prematurely closed connection while reading response header from upstream
  • connect() failed (111 connection refused) while connecting to upstream
  • no live upstreams while connecting to upstream

So, I started my research on these errors, and as suggested tried a lot of configuration changes in my nginx config file like

  • adding increased read timeouts, etc
  • adding increased proxy buffering
  • disabling gzip
  • 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 after spending two full days trying to troubleshoot this problem, 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. It is at this path


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

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


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/;

listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/;
ssl_certificate_key /etc/letsencrypt/live/;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

server {
if ($host ={
return 301 https://$host$request_uri;

if ($host ={
return 301 https://$host$request_uri;

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

return 404;

Also following is the content of the nginx.conf file which is at the following location


user www-data;
worker_processes auto;
pid /run/;
include /etc/nginx/modules-enabled/*.conf;

events {
worker_connections 768;
#multi_accept on;

http {
# Basic Settings

sendfile on;
top_nopush on;
top_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

#SSL settings

ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;

#Logging settings

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

#Gzip settings

gzip on;

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+res text/javascript;

#virtual host configs

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

Following are my nodejs code details

  1. Main function
exports.boxSticker = async(req, res, next) => {
let orderId = req.params.orderId
let userId =

let order = await Order.findById(orderId).populate('client').exec()
let user = await User.findById(userId)

const doc = new PDFdocument({
autoFirstPage: false

if(order.boxDetails.length == 0){
return res.render('error', {message: `No Box Details added. Please add Box Details first before generating AWB`, statusCode: '400'})

const canvas = createCanvas()
const context = canvas.getContext('2d')

JsBarcode(canvas, order.awbNumber)

canvas.toBuffer((err, buffer) => {
if(err) next(err)
fsPromises.writeFile(`box_${order.awbNumber}.png`, buffer)
.then(() => {
for(let i = 0; i next(err))

res.setHeader('Content-type', 'application/pdf')
res.set({ 'Content-Disposition': `inline; filename=boxsticker_${order.awbNumber}.pdf` })

stream = doc.pipe(res)

stream.on('finish', () => {
fs.unlink(`box_${order.awbNumber}.png`, (err) => {
if(err) next(err)

  1. boxstickergenerate function
exports.boxstickergenerate = (current, doc, order, user) => {['Title'] = `boxsticker${order.awbNumber}`

.rect(40, 75, 150, 30).fill()

.moveTo(40, 120)
.lineTo(560, 120).stroke()

.text(order.service, 40, 80)

.text('FROM', 40, 140)
.text(order.consignor, 40, 155)
.text(`${order.consignorAddress1}, ${order.consignorAddress2}, ${order.consignorCity}, ${order.consignorState}, ${order.consignorPincode}`, 40, 170, {width: 350, align:'left'})
.text(order.origin, 40, 215)
.text(`TEL NO: ${order.consignorContactNumber}`, 40, 230)

.moveTo(40, 260)
.lineTo(560, 260).stroke()

.text(`BOX NO ${current + 1}/${order.numberOfBoxes}`, 465, 140, {width: 100, align:'left'})
.rect(440, 160, 115, 30).fill()
.rect(440, 210, 115, 30).fill()
.text(order.client.username, 445, 165)
.text(order.service, 445, 215)

.text('TO', 40, 270)
.text(order.consignee, 40, 290)
.text(`${order.consigneeAddress1}, ${order.consigneeAddress2}, ${order.consigneeCity}, ${order.consigneeState}, ${order.consigneePincode}`, 40, 310, {width: 450, align:'left'})
.text(order.destination, 40, 370)
.text(`TEL NO: ${order.consigneeContactNumber}`, 40, 390)

.moveTo(40, 410)
.lineTo(560, 410).stroke()

.text(`SHIPMENT DATE:`, 40, 430)
.text(moment(order.bookingDate).format(shortDateFormat), 160, 430)
.text(`SHIPMENT WEIGHT: ${order.chargeableWeight}`, 40, 450)
.text(`NO OF BOX: ${order.numberOfBoxes}`, 40, 470)
.text(`WAYBILL NO: ${order.awbNumber}`, 300, 450)

//.image(`box_${order.awbNumber}.png`, 265, 490, {width: 80, align:'center'})

.moveTo(40, 550)
.lineTo(560, 550).stroke()

.text('Office Purpose Only', 230, 570, {width: 150, align:'center'})
//.image(`box_${order.awbNumber}.png`, 265, 590, {width: 80, align:'center'})

.moveTo(40, 650)
.lineTo(560, 650).stroke()

.text(user.username, 230, 670, {width: 150, align:'center'})


Submit an answer

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!

Sign In or Sign Up to Answer

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,

There has been a similar question asked here:

The problem usually happens when your backend service is either not running or is listening on a different port.

You can start by checking your Nginx error log for some more information:

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

And then also make sure that the Node.js service is actually listening on port 3000:

netstat -plant | grep 3000

If this is not the case, make sure to change that so the port that the Node.js service is listening on, matches the one that you’ve specified in your Nginx server block.