Question

502 Bad Gateway when uploading files (NGINX and NodeJS)

Hey Guys. Please help me out, I am pulling out my hair in frustration trying to fix this. So here is the issue. I am developing an app where the user can upload files. While the backend call seems to work fine, the call when uploading files returns a 502 Bad Gateway message.

The files actually get uploaded but the error message comes up. I think it has something to do with my nginx config so I am hoping you guys can point out where I am going wrong.

Here is the nginx error log message

[error] 3807#3807: *138 connect() failed (111: Connection refused) while connecting to upstream, client: 189.62.45.133, server: api-dev.app.com.br, request: "GET /users/files/5218de9eec94a7bb4467c95219b37b41ec1920420c808d9e11244f8d2a234cd6 HTTP/1.1", upstream: "https://161.35.116.108:2000/users/files/5218de9eec94a7bb4467c95219b37b41ec1920420c808d9e11244f8d2a234cd6", host: "api-dev.app.com.br", referrer: "https://dev.app.com.br/profile"

Here is my nginx.conf file

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

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    client_max_body_size 100M;
    # 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; # 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+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

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

My conf.d folder is empty however I do have specific setting in sites-enabled. Here is my app.com.br.conf

proxy_cache_path  /etc/nginx/cache levels=1:2 keys_zone=nuxt-cache:25m max_size=1g inactive=60m use_temp_path=off;

server {
    listen 80;
    listen [::]:80;
    server_name dev.app.com.br api-dev.app.com.br;
    return 301 https://$server_name$request_uri;
}

server {
    server_name dev.app.com.br;
    index index.html;

    root /opt/siena/App-Web-Nuxt/dist;

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

    location / {
        try_files $uri $uri/index.html $uri.html;
    }

}


server {
    server_name api-dev.app.com.br;

    listen [::]:443 ssl;
    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/api-dev.app.com.br/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/api-dev.app.com.br/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        # more_set_headers 'Access-Control-Allow-Origin: *';
        # more_set_headers 'Access-Control-Allow-Methods: GET,POST,OPTIONS,DELETE,PUT';
        # more_set_headers 'Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';

        # Simple requests (simples to fix CORS)
        if ($request_method ~* "(GET|POST)") {
            more_set_headers "Access-Control-Allow-Origin:  *";
        }

        # Preflighted requests
        if ($request_method = OPTIONS ) {
            more_set_headers "Access-Control-Allow-Origin:  *";
            more_set_headers "Access-Control-Allow-Methods: GET, POST, OPTIONS, HEAD";
            more_set_headers "Access-Control-Allow-Headers: Authorization, Origin, X-Requested-With, Content-Type, Accept";
            return 204;
        }

        proxy_read_timeout 300s;
        proxy_connect_timeout 75s;
        proxy_redirect off;
        proxy_pass https://api-dev.app.com.br:2000/;
    }
}

I am a total noob when it comes to this stuff. Any help would be so much appreciated.

Any more question about the system and the configuration please let me know.

Thanks in Advance.


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.

Bobby Iliev
Site Moderator
Site Moderator badge
August 21, 2020
Accepted Answer

Hi there @lnbandeira,

What I could suggest starting with is to check your Nginx error log. There you will be able to see the actual error rather than the generic 502 error.

In order to check your error log you can run the following command:

  1. tail -100 /var/log/nginx/error.log

Feel free to share the errors here so that I could try to advise you further.

Also as a side note, here is also a quick video on how to deploy Node.js with PM2, Nginx as a reverse proxy, and Cloudflare for SSL termination:

Regards, Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

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

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.