1. 支持websocket
同一个域名,多个端口,对应不同的服务。比如,9400是http服务,1900是websocket服务。
要点1:配置多个location
server{
listen 0.0.0.0:80;
server_name test.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_pass http://114.114.114.110:9400;
proxy_redirect off;
}
location /mqtt {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_pass http://114.114.114.110:1900;
proxy_redirect off;
}
}
要点2:将http升级为websocket
http {
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
......
修改上述 location /mqtt
location /mqtt {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_pass http://114.115.201.210:1900;
proxy_redirect off;
}
2.端口映射
比如,nginx配置端口5001服务。
server {
listen 5001;
client_max_body_size 100m;
proxy_send_timeout 180s;
proxy_read_timeout 600s;
location / { ......
}
location ^~ /manage {
proxy_pass http://127.0.0.1:5000;
......
}
location /node {
proxy_pass http://127.0.0.1:5000;
......
}
}
需要配置域名访问,映射到端口80,配置在同一个nginx.conf
server {
listen 80;
server_name test.cn;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass http://114.114.114.145:5001;
proxy_redirect off;
}
}