在默认的配置nginx.conf文件中做如下配置改动

一、http域的设置

http {
  include mime.types;
  default_type application/octet-stream;   #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  # '$status $body_bytes_sent "$http_referer" '
  # '"$http_user_agent" "$http_x_forwarded_for"';   #access_log logs/access.log main;   sendfile on;   #add for websocket
  map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
  } upstream websocket {
  #ip_hash; //路由规则之一,顾名思义
  server localhost:; //真正提供websocket服务的服务器地址和端口
  server localhost:; //真正提供websocket服务的服务器地址和端口
}

二、server域的设置

server {
listen ; //外部应用访问的端口
  server_name 172.18.4.114; //外部应用访问的地址   #charset koi8-r;   #access_log logs/host.access.log main;   location / {
    proxy_pass http://websocket; //这个配置指向http域的配置
    proxy_read_timeout 300s; //websocket空闲保持时长     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_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;     #root html;
    #index index.html index.htm;
  }

三、整体测试

1. 启动后端的websocket服务器,此例中是2个。

2. 打开浏览器访问http://172.18.4.114,发现链接建立到一个服务器上。

3. 再打开一个浏览器页签访问http://172.18.4.114,发现链接建立到另一个服务器上。

4. 分配成功。

5. 空闲超过5分钟后,会发现自动拆链。

nginx(二)支持websocket配置的更多相关文章

  1. Nginx反向代理websocket配置实例

    最近有一个需求,就是需要使用 nginx 反向代理 websocket,经过查找一番资料,目前已经测试通过,本文只做一个记录 复制代码 代码如下: 注: 看官方文档说 Nginx 在 1.3 以后的版 ...

  2. Nginx反向代理websocket配置实例(官网)

    https://www.nginx.com/blog/websocket-nginx/ Blog Tech Rick Nelson of NGINX, Inc.   May 16, 2014   NG ...

  3. 配置nginx+tomcat支持websocket

    问题情景:    最近开发新增加一个项目,需要支持https wss协议 访问https://test.aa.com  使用nginx反向代理到后端tomcat web应用 访问https://tes ...

  4. Django 3.x 原生支持websocket 配置

    websocket.py 1 # websocket.py 2 async def websocket_application(scope, receive, send): 3 while True: ...

  5. Nginx 不支持WebSocket TCP

    proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade";

  6. nginx支持websocket及websocket部分原理介绍

    nginx支持websocket及websocket部分原理介绍最近ipc通过websocket与server进行通行,经过无法通过nginx进行反向代理,只有直连nodejs端口.而且部署到阿里云用 ...

  7. Brophp Nginx 虚拟主机的配置

    默认情况下,Nginx 不支持 pathinfo 配置,通过查看了 Thinkphp Nginx 的配置情况,对虚拟主机配置修改如下: server {     listen 80;     list ...

  8. Nginx 支持websocket的配置

    Nginx 支持websocket的配置server { listen 80; server_name 域名; location / { proxy_pass http://127.0.0.1:808 ...

  9. nginx 支持websocket

    nginx 反向代理websocket nginx配置 请求地址及路径:ws://x.x.x.x/web/springws/websocket.ws 解析 map 指令 上面 nginx.conf 配 ...

随机推荐

  1. 开发环境之git:团队协作git工作流与常用命令

    此篇文章只是一篇傻瓜式的,记录工作中比较规范且常见的一个git工作流需要用到的命令,让你可以快速的开始工作.而不是一些长篇大论的理论知识,如果你有用过sourcetree或者其它图形化工具,结合你正在 ...

  2. 躲不掉的 lambda 表达式

    lambda 表达式是 Java8 的新特性,虽说都发布很久了,但是不到万不得已是肯定不会研究这个的,现在就是那不得不学习的时候了. 本文主要说一下什么 lambda 表达式.Java 中为什么要有 ...

  3. python中sqlite问题和坑

    import sqlite3 #导入模块 conn = sqlite3.connect('example.db') C=conn.cursor() #创建表 C.execute('''CREATE T ...

  4. PHP 对POST数据的处理

    // 获取body中json数据并初始化为数组$data = json_decode(file_get_contents('php://input'), true);

  5. 那些天使用AWS填过的坑和注意事项

    一直在找免费的GPU云端,在某乎上看到AWS提供免费的,就上去试了下,结果那个免费一年的只有CPU,并没有GPU,GPU还是需要付费的,相关背景就说这些,下面放几个相关教程,里面会说怎么使用,看了这几 ...

  6. .ftl文件介绍

    freemarker的文件一般以后缀ftl,ftl文件的头上要写<#assign ww=JspTaglibs["/WEB-INF/webwork.tld"] /这样才能在ft ...

  7. (网络数据交互)Android解析Internet的Json资源文件

    常用的Internet数据解析格式和工具: 网页显示的json数据如下: {"type":"FeatureCollection","metadata& ...

  8. [CF575B]Bribes

    [CF575B]Bribes 题目大意: 一棵\(n(n\le10^5)\)个结点的树,有些边有方向,对于每条边,如果第\(i\)次逆向走过这条边,就会产生\(2^{i-1}\)的代价.开始在\(1\ ...

  9. [USACO07NOV]牛栏Cow Hurdles

    OJ题号:洛谷2888 思路:修改Floyd,把边权和改为边权最大值.另外注意是有向图. #include<cstdio> #include<algorithm> using ...

  10. Mybatis批量插入返回自增主键(转)

    我们都知道Mybatis在插入单条数据的时候有两种方式返回自增主键: 1.对于支持生成自增主键的数据库:useGenerateKeys和keyProperty. 2.不支持生成自增主键的数据库:< ...