Nginx作为websocket的反向代理

使用nginx做websocket的反向代理在网上一找一大推,然后也按照这种方式进行了配置(其它配置忽略)
http {

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

    server{

                location /ws/ {
                proxy_pass http://192.168.3.1:9000/;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection $connection_upgrade;

                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             }
}

然而调试的时候得到了以下的响应

Handshake status 403 None
### closed ###

起初一直以为是后端服务器的问题,经过几番周转,最后发现还是nginx配置的问题,可以在location中添加以下的配置来解决这问题

proxy_set_header Origin ‘‘;

最终的正确配置为

http {

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

    server{

                location /ws/ {
                proxy_pass http://192.168.3.1:9000/;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection $connection_upgrade;
                proxy_set_header Origin ‘‘;

                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             }
}

Nginx作为websocket的反向代理

上一篇:jQuery获取省、市、区县、乡镇


下一篇:php 过滤掉emoji表情