分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

015_NGINX作为WebSocket Proxy的设置

发布时间:2023-09-06 01:09责任编辑:董明明关键词:Web

产研那边有通过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 Connectionheaders 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/

015_NGINX作为WebSocket Proxy的设置

原文地址:http://www.cnblogs.com/itcomputer/p/7483721.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved