Ngx中websocket服务的配置

websocket的连接有怎样的要求?

websocket需要请求头和响应头都设置 Upgrade: WebSocketConnection: Upgrade

# 例如
location /chat/ {
    proxy_pass http://backend;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

对代理服务器的请求中“Connection”标头字段的值取决于客户端请求标头中“Upgrade”字段的存在:

http {
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }
    server {
        ...

        location /chat/ {
            proxy_pass http://backend;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }
    }

Last updated