Nginx做爲websocket的反向代理

使用nginx作websocket的反向代理在網上一找一大推,而後也按照這種方式進行了配置(其它配置忽略)nginx

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;
             }
}

然而調試的時候獲得瞭如下的響應web

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;
             }
}
相關文章
相關標籤/搜索