Answers:
只是要注意,nginx现在在1.3.13版本上支持Websockets。使用示例:
location /websocket/ {
proxy_pass http://backend_host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;
}
您还可以查看nginx changelog和WebSocket代理文档。
proxy_read_timeout
有效,我编辑了答案。
location
指令放在server
另一个location
指令中(请参阅location docs)。backend_host
是一个upstream
(请参阅上游文档)-您将代理到的一个或一组服务器。
别担心,因为一群勇敢的Ops编程人员以崭新的nginx_tcp_proxy_module品牌解决了这种情况
写于2012年8月,因此,如果您是未来的人,应该做作业。
假设您正在使用CentOS:
init.d/nginx
脚本)yum install pcre pcre-devel openssl openssl-devel
以及用于构建NGINX的任何其他必要的库同样,假设CentOS:
cd /usr/local/
wget 'http://nginx.org/download/nginx-1.2.1.tar.gz'
tar -xzvf nginx-1.2.1.tar.gz
cd nginx-1.2.1/
patch -p1 < /path/to/nginx_tcp_proxy_module/tcp.patch
./configure --add-module=/path/to/nginx_tcp_proxy_module --with-http_ssl_module
(如果需要,您可以添加更多模块)make
make install
可选的:
sudo /sbin/chkconfig nginx on
如果要重复使用它们,请记住先复制旧的配置文件。
重要提示:您将需要tcp {}
在conf中的最高级别创建一个指令。确保它不在您的http {}
指令中。
下面的示例配置显示了一个上游websocket服务器,以及两个SSL和Non-SSL代理。
tcp {
upstream websockets {
## webbit websocket server in background
server 127.0.0.1:5501;
## server 127.0.0.1:5502; ## add another server if you like!
check interval=3000 rise=2 fall=5 timeout=1000;
}
server {
server_name _;
listen 7070;
timeout 43200000;
websocket_connect_timeout 43200000;
proxy_connect_timeout 43200000;
so_keepalive on;
tcp_nodelay on;
websocket_pass websockets;
websocket_buffer 1k;
}
server {
server_name _;
listen 7080;
ssl on;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.key;
timeout 43200000;
websocket_connect_timeout 43200000;
proxy_connect_timeout 43200000;
so_keepalive on;
tcp_nodelay on;
websocket_pass websockets;
websocket_buffer 1k;
}
}
这对我有用:
location / {
# redirect all HTTP traffic to localhost:8080
proxy_pass http://localhost:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# WebSocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
-从以下网站借来的:https : //github.com/nicokaiser/nginx-websocket-proxy/blob/df67cd92f71bfcb513b343beaa89cb33ab09fb05/simple-wss.conf
# WebSocket support
sn了我。我以前尝试转发端口400,但WSS的作品超过443 FYI未来的读者:)
用于带有SSL的.net core 2.0 Nginx
location / {
# redirect all HTTP traffic to localhost:8080
proxy_pass http://localhost:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# WebSocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
}
这对我有用
对我来说,它取决于proxy_pass
位置设置。我需要切换为使用HTTPS协议,并在节点服务器端设置有效的SSL证书。这样,当我引入一个外部节点服务器时,我只需要更改IP,其他所有配置都保持不变。
我希望这可以对整个过程有所帮助...我一直都在盯着这个问题...
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream nodeserver {
server 127.0.0.1:8080;
}
server {
listen 443 default_server ssl http2;
listen [::]:443 default_server ssl http2 ipv6only=on;
server_name mysite.com;
ssl_certificate ssl/site.crt;
ssl_certificate_key ssl/site.key;
location /websocket { #replace /websocket with the path required by your application
proxy_pass https://nodeserver;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_intercept_errors on;
proxy_redirect off;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-NginX-Proxy true;
proxy_ssl_session_reuse off;
}
}
localtion /horizon
,但是没有用。仅localtion /
或location /websockify
有效。不知道为什么...
Pankaj Malhotra撰写的一篇简洁明了的文章讨论了如何使用NGINX进行此操作,可在此处获得。
以下是NGINX的基本配置:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream appserver {
server 192.168.100.10:9222; # appserver_ip:ws_port
}
server {
listen 8888; // client_wss_port
ssl on;
ssl_certificate /path/to/crt;
ssl_certificate_key /path/to/key;
location / {
proxy_pass http://appserver;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
使用nginx / 1.14.0
我有一个运行在端口8097上的websocket服务器,用户从端口8098上的wss连接,nginx只是解密内容并将其转发到websocket服务器
所以我有这个配置文件(以我为例/etc/nginx/conf.d/default.conf
)
server {
listen 8098;
ssl on;
ssl_certificate /etc/ssl/certs/combined.pem;
ssl_certificate_key /root/domain.key;
location / {
proxy_pass http://hostname:8097;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;
}
}