015_NGINX作为WebSocket Proxy的设置
产研那边有通过nginx代理进行长连接的需求,咱们都知道默认nginx只支持短连接的,使用长连接需要单独配置
一、
websocket协议提供创建一种支持在server和client之前双向通信的web应用。作为HTML5的一部分,websock使比它之前可用的方式中提供一个种更加简单的方法。大部分现在的浏览器都支持websock,包括chrome,Firefox,Internet浏览器,Opera,和Safari,和越来越多的服务器应用框架也开始支持websock了。
The WebSocket protocol is different from the HTTP protocol,但是websocket握手是兼容http的,用TTP Upgrade方式更新从HTTP到websock的连接。This allows WebSocket applications to more easily fit into existing infrastructures.例如,websock应用能使用标准的http端口80和443进行通信。
A WebSocket application keeps a long‑running connection open between the client and the server, facilitating the development of real‑time applications. 这个 HTTP Upgrade 机制通过使用" the Upgrade
and Connection
headers." 来更新从HTTP到websocket的连接。There are some challenges that a reverse proxy server faces in supporting WebSocket. 一个是websocket是一个中hop-by-hop(逐跳)协议, so when a proxy server intercepts an Upgrade request from a client it needs to send its own Upgrade request to the backend server, including the appropriate headers. 还有,websocket连接是长连接,相反使用http时典型的短连接,即反向代理server需要在它们在空闲的时候需要保持这些连接保持open,而不是关闭它们。
NGINX supports WebSocket by allowing a tunnel to be set up between a client and a backend server. 对于nginx发送upgrade请求从client到后端的server, the Upgrade
and Connection
headers must be set explicitly, as in this example:
location /wsapp/ { proxy_pass http://wsbackend; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; }
Once this is done, NGINX deals with this as a WebSocket connection.
二、举个栗子。
参考:https://www.nginx.com/blog/websocket-nginx/
相关文章
- 解决通过Nginx转发的服务请求头header中含有下划线的key,其值取不到的问题
- 1分钟完美安装最新CentOS+Nginx+PHP-FPM+MySQL
- Nginx 安装 SSL 配置 HTTPS 超详细完整全过程
- 阿里云负载不支持 WebSocket 协议与 WSS 和 Nginx 配置问题
- nginx的总结篇以及一台nginx代理多个资源服务
- 还在手写 Nginx 配置?试试这款可视化配置神器,太强了!
- web cache server方案比较:varnish、squid、nginx
- Nginx高并发性能优化 - 运维笔记
- Kubernetes 使用Nginx-Ingress实现蓝绿发布/金丝雀发布/AB测试
- Linux(centos7)安装nginx并配置代理
- [Nginx]用Nginx实现与应用结合的訪问控制 - 防盗链
- Nginx + Apache 反向代理
- window安装nginx(快速配置负载均衡)
- Nginx+uWSGI+django
- Nginx 变量漫谈(八)