nginx websocket 代理配置

nginx websocket 代理配置,第1张

NGINX Open Source and NGINX Plus by default use HTTP/1.0 for upstream connections. To be proxied correctly, WebSocket connections require HTTP/1.1 along with some other configuration directives that set HTTP headers:

In the ‘http’ block

map $http_upgrade $connection_upgrade {
default upgrade;
‘’ close;
}

In the ‘server’ block for HTTPS traffic

location /wstunnel/ {
proxy_pass http://nodejs;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
Directive documentation: location, map, proxy_http_version, proxy_pass, proxy_set_header

The first proxy_set_header directive is needed because the Upgrade request header is hop-by-hop; that is, the HTTP specification explicitly forbids proxies from forwarding it. This directive overrides the prohibition.

The second proxy_set_header directive sets the Connection header to a value that depends on the test in the map block: if the request has an Upgrade header, the Connection header is set to upgrade; otherwise, it is set to close.

For more information about proxying WebSocket traffic, see WebSocket proxying and NGINX as a WebSocket Proxy.

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/791704.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-05
下一篇 2022-05-05

发表评论

登录后才能评论

评论列表(0条)

保存