在默认的配置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. Petya and Array CodeForces - 1042D (树状数组)

    D. Petya and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. 服务端线程模型-NIO服务模型

    上接<服务端线程模型-线程池服务模型>(https://www.cnblogs.com/fudashi233/p/10549221.html). 这篇分享从最初的单线程服务模型一直演进到线 ...

  3. tar 打包压缩

    tar命令详解 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用 ...

  4. loading加载动画效果js实现

    <style>.box { width: 400px; padding: 20px; border: 40px solid #a0b3d6; background-color: #eee; ...

  5. SQL总结——存储过程

    SQL总结(五)存储过程 概念 存储过程(Stored Procedure):已预编译为一个可执行过程的一个或多个SQL语句. 创建存储过程语法 CREATE proc | procedure pro ...

  6. IDEA安装使用Lombok插件

    项目中经常使用bean,entity等类,绝大部分数据类类中都需要get.set.toString.equals和hashCode方法,虽然IDEA开发环境下都有自动生成的快捷方式,但自动生成这些代码 ...

  7. python基础一 ------可迭代对象和迭代器对象

    可迭代对象和迭代器对象:前者生成后者 比喻:10个硬币都可以一一数(迭代),放入到存钱罐(可以取钱的那种),那这个存钱罐就是一个迭代器对象 需求:从网络抓取各个城市气温信息,并依次显示若依次抓取较多的 ...

  8. [P1396]营救 (并查集)

    大佬都是用最短路做的 我用最小生成树 #include<bits/stdc++.h> #include<algorithm> using namespace std; stru ...

  9. input的一些使用方法

  10. HBase Python API

    HBase Python API HBase通过thrift机制可以实现多语言编程,信息通过端口传递,因此Python是个不错的选择 吐槽 博主在Mac上配置HBase,奈何Zoomkeeper一直报 ...