Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx config for express 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?

like image 562
wesbos Avatar asked Sep 15 '14 21:09

wesbos


People also ask

Can Nginx listen on any port?

By default, the Nginx HTTP server listens for inbound connections and connects to port 80, which is the default web port. However, the TLS configuration, which is not supported in Nginx by default, listens to port 443 for secure connections.


2 Answers

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

}
like image 100
wesbos Avatar answered Nov 03 '22 00:11

wesbos


server {
    listen   80;
    server_name  p3000;
    location / {
        proxy_pass http://0.0.0.0:3000;
        include /etc/nginx/proxy_params;
    }
}
like image 34
Daniel Garmoshka Avatar answered Nov 03 '22 01:11

Daniel Garmoshka