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.
问题:
回答1:
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;
}
}
Source: http://nginx.com/blog/websocket-nginx/
回答2:
As simple as like this,
make sure to change example.com to your domain (or IP), and 1337 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:1337";
}
}
Source: https://eladnava.com/binding-nodejs-port-80-using-nginx/
回答3:
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.
回答4:
Simple is:
server {
listen 80;
server_name p3000;
location / {
proxy_pass http://0.0.0.0:3000;
include /etc/nginx/proxy_params;
}
}
回答5:
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
}
回答6:
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;
}
}
回答7:
You can use Kong API gateway. Kong API gateway is built on top of NGINX. It exposes API which makes a reverse proxy server. You can specify your upstream URL. It also provides various useful plugins such as:
- oAuth2
- jwt
- authentication
- HMAC Authentication
- LDAP AUthentication
- IP Restriction
- CORS
- ACL
- Dynamic SSL
- Bot Detection
- Rate Limiting
- Response Rate Limiting
- Request size limiting
- Request Termination
- Request Transformer
- Response Transformer
- Correlation ID
- Logging