Nginx反向代理配置websocket
nginx.org 官网推荐如下的配置,也可以直接看官网:http://nginx.org/en/docs/http/websocket.html
http { ... map $http_upgrade $connection_upgrade { default upgrade; '' close; ... } upstream websocket { #假如我们的upstream是127.0.0.1:8010, server 127.0.0.1:8010; } server { listen 80;
server_name test.testabc.com; access_log /var/log/nginx/test_websocket.access.log main; location / { proxy_pass http://websocket; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; } }
map指令的作用:
根据客户端请求中$http_upgrade 的值,来构造改变$connection_upgrade的值
即根据变量$http_upgrade的值创建新的变量$connection_upgrade,
创建的规则就是{}里面的东西。其中的规则没有做匹配,因此使用默认的.
即 $connection_upgrade 的值会一直是 upgrade。然后如果 $http_upgrade为空字符串的话,
那值会是 close。