nginx config for express app running on port 3000

24,555

Solution 1

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;
    }

}

Solution 2

server {
    listen   80;
    server_name  p3000;
    location / {
        proxy_pass http://0.0.0.0:3000;
        include /etc/nginx/proxy_params;
    }
}
Share:
24,555
wesbos
Author by

wesbos

Author of Sublime Text Book HTML5, JavaScript, Sublime Text, Node.js, CSS3 and all good things.

Updated on November 15, 2020

Comments

  • wesbos
    wesbos over 3 years

    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?

  • Steven Scaffidi
    Steven Scaffidi about 7 years
    Thank you for the above - that was very useful. I'm getting the error though: 14245#14245: *1 peer closed connection in SSL handshake while SSL handshaking to upstream Any ideas?
  • Steven Scaffidi
    Steven Scaffidi about 7 years
    Nevermind I got it figured out. Turns out my I just needed to create an https server with my express app. Thanks for the config above. It worked great.