首页 > 代码库 > nginx websocket 代理
nginx websocket 代理
websocket在线测试:http://www.blue-zero.com/WebSocket/,可以使用这个工具测试:
需求:大数据项目开开放2个端口8080和7398,一个走http协议,一个走tcp,也就是访问socket。当client访问这个项目socket会把client的信息抛出到web页面。现在要用nginx做代理,把这2个端口映射到内网,提供内网访问。
nginx代理http协议当然很好解决,
server {
listen 80;
server_name z.exam.com;
access_log /var/log/nginx/rdp.elenet.me_access.log main;
error_log /var/log/nginx/rdp.elenet.me_error.log;
location / {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream http_502 http_504 http_404 http_503 error timeout invalid_header;
proxy_pass http://11.1.250.241:8080;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
tcp端口的代理用nginx,上面这段肯定是不行的。nginx官网参考文档:http://nginx.org/en/docs/http/websocket.html
其实只需要在nginx的http块和server分别加几行就可以了:
http块加以下几行:
map $http_upgrade $connection_upgrade {
default upgrade;
‘‘ close;
}
server块加以下2行:
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
配置如下,http块文件较大,就不展示了:
server {
listen 7398;
server_name z.exam.com;
access_log /var/log/nginx/rdp.elenet.me_access.log main;
error_log /var/log/nginx/rdp.elenet.me_error.log;
location / {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream http_502 http_504 http_404 http_503 error timeout invalid_header;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://11.1.250.241:7398;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
以上配置可以贴在同一个conf配置文件中。
本文出自 “LINUX Super梦” 博客,请务必保留此出处http://215687833.blog.51cto.com/6724358/1854040
nginx websocket 代理