Nginx与websocket和https内容位于相同的url上 [英] nginx with websocket and https content on same url
问题描述
我的服务器在https:
My server provides on a root url, in https:
- 文件,其余资源
- websocket
我希望我的配置支持websocket,但是它不起作用.
I would like my configuration to support websocket but it does not work.
我使用支持websocket代理的nginx 1.3.16.
I use nginx 1.3.16 which supports websocket proxy.
这是我的nginx配置的一部分:
Here is part of my nginx configuration:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 443 default ssl;
server_name localhost;
ssl on;
ssl_certificate ssl/server.crt;
ssl_certificate_key ssl/server.key;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers RC4:HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
keepalive_timeout 60;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
### We want full access to SSL via backend ###
location / {
proxy_pass http://localhost:8080;
### force timeouts if one of backend is died ##
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
### Set headers ####
proxy_set_header Accept-Encoding "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
### Most PHP, Python, Rails, Java App can use this header ###
#proxy_set_header X-Forwarded-Proto https;##
#This is better##
proxy_set_header X-Forwarded-Proto $scheme;
add_header Front-End-Https on;
### By default we don't want to redirect it ####
proxy_redirect off;
}
location /writever/chat {
proxy_pass http://localhost:8080/writever/chat;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
这是我在Nginx中看到的错误:
Here is the error I see in nginx:
2013/04/23 22:41:52 [error] 17011#0: *2093 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /writever/chat?X-Atmosphere-tracking-id=0&X-Atmosphere-Framework=1.0.12&X-Atmosphere-Transport=websocket&X-Cache-Date=0&Content-Type=application/json&X-atmo-protocol=true HTTP/1.1", upstream: "http://127.0.0.1:8080/writever/chat?X-Atmosphere-tracking-id=0&X-Atmosphere-Framework=1.0.12&X-Atmosphere-Transport=websocket&X-Cache-Date=0&Content-Type=application/json&X-atmo-protocol=true", host: "localhost"
我不确定两个位置是否可以一起工作,顺序是否很重要,因为'/'包含/writever/chat.
I am not sure if the two locations can work together, if the order is important, as '/' contains /writever/chat.
任何帮助将不胜感激.
更新:我保留了以前的nginx版本.清理后,一切似乎正常.因此,我将保留此配置,直到发现问题或更好的解决方案为止.我会让你知道.
update: I had remains of a previous nginx version. After cleanning, everything seems to work fine. So I'll keep this config until I find an issue or something better. I will let you know.
推荐答案
如上所述,我的问题是nginx的安装不正确.
As indicated above, my problem was with an incorrect installation of nginx.
大致卸载任何版本< 1.3.13-(执行sudo Updatedb;找到nginx进行确认),然后安装新版本.
Uninstall toroughly any version < 1.3.13- (do a sudo updatedb;locate nginx to make sure) and then install the new version.
目前,您必须手动执行此操作(尚无法获得):
For the moment you have to do it manually (no apt-get yet):
sudo apt-get install build-essential libssl-dev zlib1g-dev
pcre
cd ~/src #make this if it doesn't exist
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.30.tar.gz
tar -xzvf pcre-8.30.tar.gz
cd pcre-8.3X/
./configure # /usr/local is the default so no need to prefix
make
sudo make install
sudo ldconfig # this is important otherwise nginx will compile but fail to load
nginx
wget http://nginx.org/download/nginx-1.4.0.tar.gz -> check for latest version on nginx website
tar -xvzf nginx-1.4.0.tar.gz
cd nginx-1.4.0
./configure --with-http_flv_module \
--with-http_ssl_module \
--with-http_gzip_static_module
make
sudo make install (uninstall previous version if nginx before)
这篇关于Nginx与websocket和https内容位于相同的url上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!