nginx 支持websocket

nginx 反向代理websocket 

nginx配置

请求地址及路径:ws://x.x.x.x/web/springws/websocket.ws

解析 map 指令

上面 nginx.conf 配置中的 map $http_upgrade $connection_upgrade 的作用,参考 http://www.ttlsa.com/nginx/using-nginx-map-method/

该作用主要是根据客户端请求中 $http_upgrade 的值,来构造改变 $connection_upgrade 的值,即根据变量 $http_upgrade 的值创建新的变量 $connection_upgrade,创建的规则就是 {} 里面的东西,请见配置:

复制代码
http{

    # websocket
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }
}
复制代码
复制代码
# websocket
upstream websocket {
    server xxx.xxx.xxx.xxx:8082;
    server xxx.xxx.xxx.xxx:8082;
}
server {

    # websocket
    location ^~ /web/springws/ {
    proxy_pass http://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;
    }
}
复制代码
 

map指令使用ngx_http_map_module模块提供的。默认情况下,nginx有加载这个模块,除非人为的 --without-http_map_module。
ngx_http_map_module模块可以创建变量,这些变量的值与另外的变量值相关联。允许分类或者同时映射多个值到多个不同值并储存到一个变量中,map指令用来创建变量,但是仅在变量被接受的时候执行视图映射操作,对于处理没有引用变量的请求时,这个模块并没有性能上的缺失。

一. ngx_http_map_module模块指令说明

map
语法: map $var1 $var2 { ... }
默认值: —
配置段: http
map为一个变量设置的映射表。映射表由两列组成,匹配模式和对应的值。
在 map 块里的参数指定了源变量值和结果值的对应关系。
匹配模式可以是一个简单的字符串或者正则表达式,使用正则表达式要用('~')。
一个正则表达式如果以 “~” 开头,表示这个正则表达式对大小写敏感。以 “~*”开头,表示这个正则表达式对大小写不敏感。

正则表达式里可以包含命名捕获和位置捕获,这些变量可以跟结果变量一起被其它指令使用。

[warning]不能在map块里面引用命名捕获或位置捕获变量。如~^/ttlsa_com/(.*)  /boy/$1; 这样会报错nginx: [emerg] unknown  variable。[/warning]如果源变量值包含特殊字符如‘~’,则要以‘’来转义。

结果变量可以是一个字符串也可以是另外一个变量。

map指令有三个参数:
default : 指定如果没有匹配结果将使用的默认值。当没有设置 default,将会用一个空的字符串作为默认的结果。
hostnames : 允许用前缀或者后缀掩码指定域名作为源变量值。这个参数必须写在值映射列表的最前面。
include : 包含一个或多个含有映射值的文件。

如果匹配到多个特定的变量,如掩码和正则同时匹配,那么会按照下面的顺序进行选择:
1. 没有掩码的字符串
2. 最长的带前缀的字符串,例如: “*.example.com”
3. 最长的带后缀的字符串,例如:“mail.*”
4. 按顺序第一个先匹配的正则表达式 (在配置文件中体现的顺序)
5. 默认值

map_hash_bucket_size
语法: map_hash_bucket_size size;
默认值: map_hash_bucket_size 32|64|128;
配置段: http
指定一个映射表中的变量在哈希表中的最大值,这个值取决于处理器的缓存。

map_hash_max_size
语法: map_hash_max_size size;
默认值: map_hash_max_size 2048;
配置段: http
设置映射表对应的哈希表的最大值。

二. 实例

map
map

map

如需转载请注明出处:http://www.ttlsa.com/html/3206.html

Nginx配置WebSocket反向代理
问题描述
目前项目中需要使用到WebSocket来进行通讯,所以就写了个Nginx反向代理WebSocket的配置文件.

很简单,但是很有效,能够横向扩展WebSocket服务端

先直接展示配置文件,如下(使用的话直接复制,然后改改ip和port即可)

map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}

upstream wsbackend{
server ip1:port1;
server ip2:port2;
keepalive 1000;
}

server {

listen 20038;

location /{

proxy_http_version 1.1;
proxy_pass http://wsbackend;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_read_timeout 3600s;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
会用了还需要理解(厚积薄发,不要让曾经的努力白费)

首先:

map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}

表示的是

1. 如果 $http_upgrade 不为 '' (空),则 $connection_upgrade 为 upgrade
2. 如果 $http_upgrade 为 '' (空),则 $connection_upgrade 为 close
1
2
3
4
5
6
7
8
9
其次:

upstream wsbackend{
server ip1:port1;
server ip2:port2;
keepalive 1000;
}

表示的是 nginx负载均衡
1. 两台服务器 (ip1:port1)和(ip2:port2)
2. keepalive 1000 表示的是每个nginx进程中上游服务器保持的空闲连接,当空闲连接过多时,会关闭最少使用的空闲连接.当然,这不是限制连接总数的,可以想象成空闲连接池的大小.设置的值应该是上游服务器能够承受的
1
2
3
4
5
6
7
8
9
最后:

server {

listen 20038;

location /{

proxy_http_version 1.1;
proxy_pass http://wsbackend;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_read_timeout 3600s;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}

表示的是监听的服务器的配置
1. listen 20038 表示 nginx 监听的端口
2. locations / 表示监听的路径(/表示所有路径,通用匹配,相当于default)
3. proxt_http_version 1.1 表示反向代理发送的HTTP协议的版本是1.1,HTTP1.1支持长连接
4. proxy_pass http://wsbackend; 表示反向代理的uri,这里可以使用负载均衡变量
5. proxy_redirect off; 表示不要替换路径,其实这里如果是/则有没有都没关系,因为default也是将路径替换到proxy_pass的后边
6. proxy_set_header Host $host; 表示传递时请求头不变, $host是nginx内置变量,表示的是当前的请求头,proxy_set_header表示设置请求头
7. proxy_set_header X-Real-IP $remote_addr; 表示传递时来源的ip还是现在的客户端的ip
8. proxy_read_timeout 3600s; 表的两次请求之间的间隔超过 3600s 后才关闭这个连接,默认的60s.自动关闭的元凶
9. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 表示X-Forwarded-For头不发生改变
10. proxy_set_header Upgrade $http_upgrade; 表示设置Upgrade不变
11. proxy_set_header Connection $connection_upgrade; 表示如果 $http_upgra
---------------------
作者:lhn1599085
来源:CSDN
原文:https://blog.csdn.net/lhn1599085/article/details/80612890
版权声明:本文为博主原创文章,转载请附上博文链接!

原文地址:https://www.cnblogs.com/zhengchunyuan/p/10843553.html