Nginx Node Express Multer - 502 bad gateway when uploading image to Cloudinary

Posted May 2, 2019 4.6k views
NginxNode.jsUbuntu 16.04

When i try to upload an image through my node express application running on Ubuntu 16.04, nginx directly gives me a 502 bad gateway. It doesn’t even take a second before it redirects to the 502.

It is supposed to upload the image to Cloudinary where i host my images and videos for my web application, and it is working perfectly on my local machine (localhost). But as soon as i tried from my droplet the error appears.

I don’t think it can be a problem with my ufw since i can easily create customers to stripe api, and everything else seems to work great.

Here is the error log from Nginx describing that the connection is closing prematurely:

2019/05/01 17:02:15 [error] 2063#2063: *6 upstream prematurely closed connection while reading response header from upstream, client: (IP LOCAL MACHINE), server: (DOMAIN), request: "POST /profile/5cc4$.........

2019/05/01 17:02:15 [error] 2063#2063: *6 connect() to [(IPV6]:8080 failed (101: Network is unreachable) while connecting to upstream, client: (IP LOCAL MACHINE), server: (DOMAIN), r$.....

And this is my nginx setup:

server {

        # SSL configuration
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        # Note: You should disable gzip for SSL traffic.
        # See:
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See:
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;


        location / {
          proxy_pass http://localhost:8080;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection 'upgrade';
          proxy_set_header Host $host;
          proxy_cache_bypass $http_upgrade;
          proxy_buffer_size 1024k;
          proxy_buffers 4 1024k;
          proxy_busy_buffers_size 1024k;

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

When i try to upload an image, it gives me the 502 bad gateway, and when i try to click “back” in the browser, it seems like it has restarted the pm2, cause i need to log in again. I have a middleware checking if you are logged in or not. I can’t seem to figure this one out myself, so i hope one of you have the knowledge. Everything works great on localhost to cloudinary but not from the droplet.

1 comment
  • @lefevreteam

    I am not sure if this can help but I think you need to put a / at the end of proxy_pass directive so it becomes like this:

     proxy_pass http://localhost:8080/;

    I sometimes had problems such because this / was missing.

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.

Submit an Answer
2 answers

The issue may be caused by PM2. If you’re enabled watching, the app will restart on every single file change(and new uploads too). The solution could be disabling watching completely or adding the uploads folder to ignore list. More:

  • Hello, I agree with the person above.
    I went through the same problem, I search many forums and the problem was in PM2, specifically in –watch.
    I was doing it in the following way:

    sudo pm2 start server.js --name web --watch

    Fix it by removing the –watch:

    sudo pm2 start server.js --name web

@Mohsen47 - I have just tried it out, but it still gives the same error.