在默认的配置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. Linux学习笔记 11

    移除文件 #rm -i file 有信息确认的文件删除 #rm file1 file2 有信息确认的文件删除 #rm -rf file 强制删除file文件

  2. linux 学习笔记 APACHE安装总结

    #cd /usr/local #mkdir APACHE #tar zxvf /usr/etc/DEV/httpd-2.2.9.tar.gz #mv httpd-2.2.9/* . #rm -rf h ...

  3. Python3基础-代码阅读系列—素数

    生成素数代码展示 质数(prime number)又称素数,有无限个. 质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数. primenumber = [] upperlimit = 2 ...

  4. 3466 ACM Proud Merchants 变形的01背包

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3466 题意:假设你有M元,已经Pi,Qi,Vi(i为角标,1<i<N),当M>Qi,时才 ...

  5. 流畅的Python读书笔记(二)

    2.1 可变序列与不可变序列 可变序列 list. bytearray. array.array. collections.deque 和 memoryview. 不可变序列 tuple. str 和 ...

  6. 面试题fugui

    1.什么是栈溢出,在什么情况下可能会出现 栈溢出是由于C语言系列没有内置检查机制来确保复制到缓冲区的数据不得大于缓冲区的大小,因此当数据足够大时,将会溢出缓冲区的范围. python函数调用是通过栈这 ...

  7. Vue.js的初步使用

    1.声明式渲染 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  8. [HDU1693]Eat the Trees

    Description: 给出n*m的方格,有些格子不能铺线,其它格子必须铺,可以形成多个闭合回路.问有多少种铺法? Hint: \(n,m<=12\) Solution: 与原来单回路那题转移 ...

  9. Bridges: The Final Battle

    对修改操作按时间分治,设$solve(l,r,n,m)$为考虑时间在$[l,r]$的修改操作,作用范围是$n$个点,$m$条边的图. 若$l=r$,则暴力Tarjan统计桥边个数即可. 否则提取出$[ ...

  10. C++学习笔记46:模板与群体数据

    函数模板 创建一个通用功能的函数,支持多种不同的形参:简化重载函数的函数体设计: 语法形式 template <模板参数表> 函数定义:模板参数表的内容:类型参数:class(或typen ...