我正在使用Nginx作为web主机,并运行在监听端口8888的同一设备上的websocket的代理。试图找到一种方法让nginx监听80并转发websocket请求到内部港口。不要将新的端口暴露给外部。这甚至有可能吗?NGinx向前websocket从80到websocket端口
UPDATE:
这是我目前的配置:
error_log /var/log/nginx/error_log.log warn;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server localhost:8888;
}
server {
listen 80;
#listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html/EncoderAdmin;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location/{
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;
}
location /ws {
proxy_pass http://localhost:8888;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
当我尝试使用WS连接到它:// [地址]/WS我得到:
WebSocket连接到'ws:// [address]/ws'失败:在WebSocket握手期间出错:意外的响应代码:400
您是否找到答案? – Taurus
你解决了吗?这个配置是否有效?我有相同的场景 – Jocket