nginx websocket反向代理配置 [英] nginx websocket reverse proxy configuration
问题描述
我正在尝试将nginx配置为Websocket的反向代理.我将服务器配置如下:
Hi I am trying to configure nginx as reverse proxy for websockets. I configure my server as following:
server {
listen 80;
server_name www.mydomain.com;
access_log off;
#error_log off;
location / {
proxy_pass http://127.0.0.1:8765;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $http_host;
proxy_buffering off;
}
}
但是我从客户端收到一个错误,如下所示:
but I get an error from client like following
与'ws://www.application.com/ws'的WebSocket连接失败:WebSocket握手期间出错:'连接'标头值不是'升级'
WebSocket connection to 'ws://www.application.com/ws' failed: Error during WebSocket handshake: 'Connection' header value is not 'Upgrade'
我可能在做一些配置错误,但看不到.
I am probably doing some configuration wrong but I could not see it.
客户端的请求标头在后面
Request headers for client is following
GET ws://www.talkybee.com/ws HTTP/1.1
Pragma: no-cache
Origin: http://www.talkybee.com
Host: www.talkybee.com
Sec-WebSocket-Key: Ol+O1IdaLEsHxxWRBt2oqg==
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
Upgrade: websocket
Sec-WebSocket-Extensions: x-webkit-deflate-frame
Cache-Control: no-cache
Connection: Upgrade
Sec-WebSocket-Version: 13
当我进行普通的直接连接时,我的连接就可以了.这是工作请求标头.
When I do a normal direct connection, My connection just works. Here is the working request header.
Cache-Control:no-cache
Connection:Upgrade
Host:www.talkybee.com:8765
Origin:http://www.talkybee.com:8765
Pragma:no-cache
Sec-WebSocket-Extensions:x-webkit-deflate-frame
Sec-WebSocket-Key:Y026b/84aUkMxVb0MaKE2A==
Sec-WebSocket-Version:13
Upgrade:websocket
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
推荐答案
此问题与nginx版本有关.请检查nginx -v,以检查您的版本. 1.4 版本之后支持以下参数.
This issue is related with the nginx version. Pleas check nginx -v, to check your version. The followings params are supported after the 1.4 version.
# WebSocket support (nginx 1.4)
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
如果您使用的是ubuntu,则可以按照以下步骤安装较新的版本:
If you are using ubuntu you can install a newer version with this steps:
首先删除旧版本( https://askubuntu.com /questions/235347/什么是卸载nginx的最佳途径):
sudo apt-get remove nginx
sudo apt-get purge nginx
sudo apt-get autoremove
然后安装新版本( https://launchpad.net/~nginx/+archive /development ):
sudo add-apt-repository ppa:nginx/development
sudo apt-get update
sudo apt-get install nginx
这篇关于nginx websocket反向代理配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!