nginx配置socket连接
author: headsen chen
date: 2019-08-08 18:52:36
notice :个人原创
Nginx通过在客户端和后端服务器之间建立隧道来支持WebSockets通信。为了让Nginx可以将来自客户端的Upgrade请求发送到后端服务器,Upgrade和Connection的头信息必须被显式的设置。如下所示:
location /wsapp/ {
proxy_pass http://wsbackend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
一旦我们完成以上设置,Nginx就可以处理WebSocket连接了。
生产配置实例:
[root@beta-aaa01:/usr/local/openresty/nginx/conf/locations]$cat location aaa.bbbb.cn.conf location / { proxy_pass http://10.0.0.3:8702/; proxy_http_version 1.1; proxy_redirect off; proxy_buffering off; proxy_set_header X-NginX-Proxy true; proxy_set_header Host $host; proxy_set_header Http-Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; }