wesbos wesbos - 1 month ago 12
Node.js Question

nginx config for epress app running on port 3000

I'm trying to setup nginx so that it pull the app running on port 3000.

When I visit mydomain.com:3000, the app works. I want it to run without the port.

I have nginx setup and it's working properly.
I have an SSL cert setup and it's working properly (I'm able to see the nginx start page with SSL)
I have the www redirect working properly.

The part I want to do now is take what is running on port 3000 and have it run on port 80.

Here is my config file:

upstream myapp {
server 127.0.0.1:3000;
}

server {
#listen 80 is default
server_name www.mydomain.com;
return 301 $scheme://mydomain.com$request_uri;
}

server {
listen 80;
listen [::]:80;
listen 443 default ssl;
ssl on;
ssl_certificate /root/certs/bundle.crt;
ssl_certificate_key /root/certs/mydomain.key;

server_name mydomain.com;

if ($ssl_protocol = "") {
rewrite ^ https://$server_name$request_uri? permanent;
}

location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
try_files @node $uri.html;
}

location @node {
proxy_pass https://myapp;
}

}


When I visit the page with this setup, I get a 500 internal server error. What am I doing wrong?

Answer

I figured it out, since I was using SSL, I needed to make sure it was https. Here is my final config:

upstream app_nodejs {
  server 127.0.0.1:3000;
}

server {
    #listen 80 is default
    server_name www.mydomain.com;
    return 301 $scheme://mydomain.com$request_uri;
}

server {
    listen 80;
    listen   [::]:80;
    listen   443 default ssl;
    ssl on;
    ssl_certificate    /root/certs/bundle.crt;
    ssl_certificate_key    /root/certs/mydomain.key;   

    server_name mydomain.com;

    if ($ssl_protocol = "") {
       rewrite ^   https://$server_name$request_uri? permanent;
    }

    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;

      proxy_pass https://app_nodejs;
      proxy_redirect off;
    }

}
Comments