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

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.

Show comments

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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:

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