我在我的debian服务器上使用LEMP堆栈和Node JS。 Nginx在8080上运行端口80和Node JS。我为nodejs app创建了新的子域:cdn.domain.com。目前我只能像cdn.domain.com:8080/一样访问Node JS应用程序。我想要做的是配置Nginx,这样,当我进入cdn.domain.com时,我可以让应用程序在端口80上工作。我认为可以使用nginx上游完成。但我无法弄清楚如何。
答案 0 :(得分:17)
NGINX支持WebSockets,允许在客户端和后端服务器之间建立隧道。为了让NGINX将升级请求从客户端发送到后端服务器,必须明确设置升级和连接头。例如:
# 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;
}
}
答案 1 :(得分:13)
就像这样简单,
确保将example.com更改为您的域(或IP),并将8080更改为您的Node.js应用程序端口:
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";
}
}
答案 2 :(得分:7)
这就是你如何实现这一目标的。
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;
}
}
您还可以使用此配置在多个节点进程之间进行负载平衡,如下所示:
upstream {
nodeapp 127.0.0.1:8081;
nodeapp 127.0.0.1:8082;
nodeapp 127.0.0.1:8083;
}
在不同进程中的端口8081,8082和8083上运行节点服务器的位置。 Nginx可以轻松地在这些服务器进程之间平衡您的流量。
答案 3 :(得分:6)
简单就是:
server {
listen 80;
server_name p3000;
location / {
proxy_pass http://0.0.0.0:3000;
include /etc/nginx/proxy_params;
}
}
答案 4 :(得分:3)
您可以定义上游并在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
}
答案 5 :(得分:2)
您可以通过在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;
}
}
答案 6 :(得分:1)
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";
} }
答案 7 :(得分:0)
这对我有用:
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;
}
}
如果它对您不起作用,请查看sudo tail -f /var/log/nginx/error.log
上的日志
答案 8 :(得分:-5)
您可以使用Kong API网关。 Kong API网关建立在NGINX之上。它公开了生成反向代理服务器的API。您可以指定上游URL。 它还提供各种有用的插件,例如: