I'm using LEMP stack and Node JS on my debian server. Nginx works on port 80 and Node JS on 8080. I created new subdomain: cdn.domain.com for nodejs app. Currently I can access to Node JS application only like cdn.domain.com:8080/. What I want to do is to configure Nginx so that, when I enter to cdn.domain.com I can get app working on port 80. I think it can be done using nginx upstream. But I can't figure out how.
7 Answers
As simple as like this,
make sure to change example.com to your domain (or IP), and 8080 to your Node.js application port:
server {
listen 80;
server_name example.com;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://127.0.0.1:8080";
}
}
Source: https://eladnava.com/binding-nodejs-port-80-using-nginx/

- 916
- 7
- 13
-
Hi, with this solution, would I need to open port 8080 to outside or it is enough with port 80 open. Thanks – dhalfageme Jan 20 '20 at 14:38
-
1No need to open port 8080 or any other port. opening 80 only is enough. – Nyi Nyi Jan 21 '20 at 06:53
-
NGINX supports WebSockets by allowing a tunnel to be setup between a client and a backend server. In order for NGINX to send the Upgrade request from the client to the backend server, Upgrade and Connection headers must be set explicitly. For example:
# WebSocket proxying
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
# The host name to respond to
server_name cdn.domain.com;
location / {
# Backend nodejs server
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}

- 6,671
- 2
- 27
- 25
you can do this very easy by using following in sudo vi /etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _ your_domain;
location /health {
access_log off;
return 200 "healthy\n";
}
location / {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
}

- 154
- 1
- 6
Simple is:
server {
listen 80;
server_name p3000;
location / {
proxy_pass http://0.0.0.0:3000;
include /etc/nginx/proxy_params;
}
}

- 5,849
- 39
- 40
-
5Upvote for including `/etc/nginx/proxy_params` that is obviously meant to be used. You could make this answer more clear by using `cdn.domain.com` instead of `p3000`. But this works great in my case, I wonder why all the other answers suggest the use of `upstream` (will have to check that out now…). – sylbru Jun 09 '18 at 08:22
-
@sylbru because with an upstream, you can easily load-balance the traffic – dystopiandev Jun 21 '22 at 20:59
This is how you can achieve this.
upstream {
nodeapp 127.0.0.1:8080;
}
server {
listen 80;
# The host name to respond to
server_name cdn.domain.com;
location /(.*) {
proxy_pass http://nodeapp/$1$is_args$args;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Real-Port $server_port;
proxy_set_header X-Real-Scheme $scheme;
}
}
You can also use this configuration to load balance amongst multiple Node processes like so:
upstream {
nodeapp 127.0.0.1:8081;
nodeapp 127.0.0.1:8082;
nodeapp 127.0.0.1:8083;
}
Where you are running your node server on ports 8081, 8082 and 8083 in separate processes. Nginx will easily load balance your traffic amongst these server processes.

- 2,191
- 1
- 16
- 22
You can define an upstream and use it in proxy_pass
http://rohanambasta.blogspot.com/2016/02/redirect-nginx-request-to-upstream.html
server {
listen 8082;
location ~ /(.*) {
proxy_pass test_server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
}
}
upstream test_server
{
server test-server:8989
}
-
4Please elaborate on your answer. Users should be able to understand your suggestion without following a link offsite that may not work in the future. – greyfade Feb 22 '16 at 17:45
This worked for me:
server {
listen 80;
server_name example.com www.example.com;
location / {
proxy_pass http://127.0.0.1:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
If it does not work for you look at the logs at sudo tail -f /var/log/nginx/error.log

- 15,456
- 11
- 71
- 120