`proxy_pass https://127.0.0.1:3000` led to `502 bad gateway`

by Thomas   Last Updated July 01, 2018 06:00 AM

I have the following nginx blocks in /etc/nginx/sites-available/default. As a result, my website http://www.funfun.ink/1/#/home can be redirected to https://www.funfun.ink/1/#/home and loads well.

But I'm not sure if proxy_pass http://localhost:3000 is the right configuration, note that I want the whole website to always use SSL. I tried proxy_pass https://localhost:3000 and proxy_pass https://127.0.0.1:3000 (which worked in another website and server of mine), but http://www.funfun.ink/1/#/home always led to 502 bad gateway.

Could anyone tell me 1) why proxy_pass https://127.0.0.1:3000 led to 502 bad gateway; 2) is proxy_pass http://localhost:3000 the right configuration with regards to SSL, though the result looks good?

server {
    listen 80;
    server_name funfun.ink www.funfun.ink;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;

    server_name funfun.ink www.funfun.ink;

    ssl_certificate /etc/nginx/cert/1530230026231.pem;
    ssl_certificate_key /etc/nginx/cert/1530230026231.key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_session_timeout 1d;
    ssl_stapling on;
    ssl_stapling_verify on;
    add_header Strict-Transport-Security max-age=15768000;

    location ~* ^/\b(?!1|js|socket.io|monaco-editor|libs|dist|fonts|javascripts|formatter|ui|css|stylesheets|htmls|imgs|static|httpOnly|tmp|uploads)\w+\b/? {
        rewrite .* /1/#$request_uri redirect;
    }

    location ~* ^/1/\b(?!#|auth)\w+\b/? {
        rewrite ^/1/(.*[.]js)$ https://www.funfun.ink/dist/$1 redirect;
        rewrite ^/1/(.*[^.][^j][^s])$ https://www.funfun.ink/1/#/$1 redirect;
    }

    location = / {
        return 301 /home;
    }

    location ~ /.well-known {
        allow all;
    }

    location / {
        proxy_set_header    Host                $host;
        proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto   $scheme;
        proxy_set_header    Accept-Encoding     "";
        proxy_set_header    Proxy               "";
        # proxy_pass          https://127.0.0.1:3000;
        # proxy_pass          https://localhost:3000;
        proxy_pass          http://localhost:3000;

        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection "upgrade";
    }
}


Answers 1


But I'm not sure if proxy_pass http://localhost:3000 is the right configuration, note that I want the whole website to always use SSL.

If the localhost:3000 has a service on HTTP protocol, it can't magically answer using HTTPS. That's exactly why you get 502 bad gateway. This is not a problem:

  1. listen 443 ssl; is what makes your site to be served using TLS.
  2. Connections to localhost are using local loopback instead of the internet.

Just make sure the port :3000 is only bind to interface 127.0.0.1 i.e. doesn't answer on http://funfun.ink:3000. (Seems ok from here.)

Esa Jokinen
Esa Jokinen
July 01, 2018 05:39 AM

Related Questions


Local web app not resolving localhost

Updated July 16, 2016 08:00 AM

Running 2 NodeJS apps in nginx in localhost

Updated July 25, 2016 08:01 AM


500 Internal Server Error with nginx local host

Updated February 11, 2018 18:00 PM