Nginx配置之WebSocket配置

引言


WebSocket在云服务器上的配置,会出现一些异常情况,例如:WebSocket无法正常访问到,WebSocket断开连接等。

本文意在解决上述出现的一些故障现象。

1. Nginx 支持WebSocket的配置


server {
      listen   80;
      #域名
      server_name localhost;
      location /sell {
        proxy_pass   http://127.0.0.1:8080/; // 代理转发地址
        proxy_http_version 1.1;
        proxy_read_timeout   3600s; // 超时设置
        // 启用支持websocket连接
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
      }
}

重要的是这两行,它表明是websocket连接进入的时候,进行一个连接升级将http连接变成websocket的连接。

proxy_set_header Upgrade $http_upgrade;

proxy_set_header Connection "upgrade";

proxy_read_timeout; 表明连接成功以后等待服务器响应的时候,如果不配置默认为60s;

proxy_http_version 1.1; 表明使用http版本为1.1 

据参考资料 : nginx配置websocket(官方)来编写一份标准配置文件。

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;
    }
}
map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

表示的是
1. 如果 $http_upgrade 不为 '' (空),则 $connection_upgrade 为 upgrade
2. 如果 $http_upgrade 为 '' (空),则 $connection_upgrade 为 close
upstream wsbackend{
    server ip1:port1;
    server ip2:port2;
    keepalive 1000;
}

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

    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 80                                    表示 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_upgrade为upgrade,则请求为upgrade(websocket),如果不是,就关闭连接

简要版本:

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

    server {
        ...

        location /chat/ {
            proxy_pass http://backend;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }
    }
}

2. Nginx代理webSocket经常出现中断的解决方案, 如何保持长连接


原因是: nginx等待客户端第一次通讯和第二次通讯的时间差,超过了它设定的最大等待时间,简单来说就是,超时,所以就断了webSocket连接,开始上解决步骤。

解决方案一

其实只要配置nginx.conf的对应localhost里面的这几个参数就好

  1. proxy_connect_timeout
  2. proxy_read_timeout
  3. proxy_send_timeout
http {
    server {
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://webscoket; 
            proxy_http_version 1.1; 
            proxy_connect_timeout 4s; #配置点1
            proxy_read_timeout 60s; #配置点2,如果没效,可以考虑这个时间配置长一点
            proxy_send_timeout 12s; #配置点3
            proxy_set_header Upgrade $http_upgrade; #这是webSocket的配置,与此解决方案无关
            proxy_set_header Connection "Upgrade"; #这是webSocket的配置,与此解决方案无关
        }
    }
}

关于配置点2的解释
备注:这个是服务器对客户端等待最大的时间,也就是说,当你webSocket使用nginx转发的时候,对于上面的配置点2来说,如果60秒内没有通讯,依然是会断开的,所以,你可以按照你的需求来设定。

举例:
比如说,我设置了10分钟,那么如果我10分钟内有通讯,或者10分钟内有做心跳的话,是可以保持连接不中断的,详细看个人需求

解决方案二

发心跳包,原理就是在有效的再读时间内进行通讯,重新刷新再读时间

3. Nginx超时设置详解

上面所讲的超时设置,显得有些模糊,索性将涉及到的超时设置一并归拢了吧。

Nginx处理的每个请求均有相应的超时设置。如果做好这些超时时间的限定,判定超时后资源被释放,用来处理其他的请求,以此提升 Nginx 的性能。

1. client_header_timeout

客户端向服务端发送一个完整的 request header 的超时时间。如果客户端在指定时间内没有发送一个完整的 request headerNginx 返回 HTTP 408(Request Timed Out)。

语法
client_header_timeout time

默认值
60s

上下文
http, server

说明
指定等待client发送一个请求头的超时时间(例如:GET / HTTP/1.1).仅当在一次read中,没有收到请求头,才会算成超时。如果在超时时间内,client没发送任何东西,nginx返回HTTP状态码408(“Request timed out”)

接收客户端header超时, 默认60s, 如果60s内没有收到完整的http包头, 返回408

Syntax: client_header_timeout time;

Default: client_header_timeout 60s;

Context:  http, server

Defines a timeout for reading client request header. If a client does not transmit the entire header within this time,

the 408 (Request Time-out) error is returned to the client.
2. client_body_timeout

指定客户端与服务端建立连接后发送 request body 的超时时间。如果客户端在指定时间内没有发送任何内容,Nginx 返回 HTTP 408(Request Timed Out)。

语法
client_body_timeout time

默认值
60s

上下文
http, server, location

说明
该指令设置请求体(request body)的读超时时间。仅当在一次readstep中,没有得到请求体,就会设为超时。超时后,nginx返回HTTP状态码408(“Request timed out”)

接收客户端body超时, 默认60s, 如果连续的60s内没有收到客户端的1个字节, 返回408

Syntax: client_body_timeout time;

Default: 

client_body_timeout 60s;

Context:  http, server, location

Defines a timeout for reading client request body. The timeout is set only for a period between two successive read operations, not for the transmission of the whole request body.

If a client does not transmit anything within this time,

the 408 (Request Time-out) error is returned to the client.
3. keepalive_timeout

HTTP 是一种无状态协议,客户端向服务器发送一个 TCP 请求,服务端响应完毕后断开连接。

如果客户端向服务器发送多个请求,每个请求都要建立各自独立的连接以传输数据。

HTTP 有一个 KeepAlive 模式,它告诉 webserver 在处理完一个请求后保持这个 TCP 连接的打开状态。若接收到来自客户端的其它请求,服务端会利用这个未被关闭的连接,而不需要再建立一个连接。

KeepAlive 在一段时间内保持打开状态,它们会在这段时间内占用资源。占用过多就会影响性能。

Nginx 使用 keepalive_timeout 来指定 KeepAlive 的超时时间(timeout)。指定每个 TCP 连接最多可以保持多长时间。Nginx 的默认值是 75 秒,有些浏览器最多只保持 60 秒,所以可以设定为 60 秒。若将它设置为 0,就禁止了 keepalive 连接。

语法
keepalive_timeout timeout [ header_timeout ]

默认值
75s

上下文
http, server, location

说明
第一个参数指定了与clientkeep-alive连接超时时间。服务器将会在这个时间后关闭连接。
可选的第二个参数指定了在响应头Keep-Alive: timeout=time中的time值。这个头能够让一些浏览器主动关闭连接,这样服务器就不必要去关闭连接了。没有这个参数,nginx不会发送Keep-Alive响应头(尽管并不是由这个头来决定连接是否“keep-alive”)
两个参数的值可不相同

keepalive时间,默认75s,通常keepalive_timeout应该比client_body_timeout

Syntax: keepalive_timeout timeout [header_timeout];

Default: 

keepalive_timeout 75s;

Context:  http, server, location

The first parameter sets a timeout during which a keep-alive client connection will stay open on the server side. The zero value disables keep-alive client connections.

The optional second parameter sets a value in the “Keep-Alive: timeout=time” response header field. Two parameters may differ.
The “Keep-Alive: timeout=time” header field is recognized by Mozilla and Konqueror. 
MSIE closes keep-alive connections by itself in about 60 seconds.
4. lingering_timeout

语法
lingering_timeout time

默认值
5s

上下文
http server location

说明
lingering_close生效后,在关闭连接前,会检测是否有用户发送的数据到达服务器,如果超过lingering_timeout时间后还没有数据可读,就直接关闭连接;否则,必须在读取完连接缓冲区上的数据并丢弃掉后才会关闭连接。

Syntax: lingering_timeout time;

Default: 

lingering_timeout 5s;

Context:  http, server, location

When lingering_close is in effect, this directive specifies the maximum waiting time for more client data to arrive. If data are not received during this time,

the connection is closed. Otherwise, the data are read and ignored, and nginx starts waiting for more data again.

The “wait-read-ignore” cycle is repeated, but no longer than specified by the lingering_time directive.
5. resolver_timeout

语法
resolver_timeout time

默认值
30s

上下文
http server location

说明
该指令设置DNS解析超时时间

域名解析超时,默认30s

Syntax: resolver_timeout time;

Default: 

resolver_timeout 30s;

Context:  http, server, location

Sets a timeout for name resolution, for example:

resolver_timeout 5s;
6. proxy_connect_timeout

语法
proxy_connect_timeout time

默认值
60s

上下文
http server location

说明
该指令设置与upstream server的连接超时时间,有必要记住,这个超时不能超过75秒。
这个不是等待后端返回页面的时间,那是由proxy_read_timeout声明的。如果你的upstream服务器起来了,但是hanging住了(例如,没有足够的线程处理请求,所以把你的请求放到请求池里稍后处理),那么这个声明是没有用的,由于与upstream服务器的连接已经建立了。

nginxupstream server的连接超时时间

Syntax: proxy_connect_timeout time;

Default: 

proxy_connect_timeout 60s;

Context:  http, server, location

Defines a timeout for establishing a connection with a proxied server. It should be noted that this timeout cannot usually exceed 75 seconds.
7. proxy_read_timeout

语法
proxy_read_timeout time

默认值
60s

上下文
http server location

说明
该指令设置与代理服务器的读超时时间。它决定了nginx会等待多长时间来获得请求的响应。这个时间不是获得整个response的时间,而是两次reading操作的时间。

nginx接收upstream server数据超时, 默认60s, 如果连续的60s内没有收到1个字节, 连接关闭

Syntax: proxy_read_timeout time;

Default: 

proxy_read_timeout 60s;

Context:  http, server, location

Defines a timeout for reading a response from the proxied server. The timeout is set only between two successive read operations,

not for the transmission of the whole response. If the proxied server does not transmit anything within this time, the connection is closed.
8. proxy_send_timeout

语法
proxy_send_timeout time

默认值
60s

上下文
http server location

说明
这个指定设置了发送请求给upstream服务器的超时时间。超时设置不是为了整个发送期间,而是在两次write操作期间。如果超时后,upstream没有收到新的数据,nginx会关闭连接

nginx发送数据至upstream server超时, 默认60s, 如果连续的60s内没有发送1个字节, 连接关闭

Syntax: proxy_send_timeout time;

Default: 

proxy_send_timeout 60s;

Context:  http, server, location

Sets a timeout for transmitting a request to the proxied server. The timeout is set only between two successive write operations,

not for the transmission of the whole request. If the proxied server does not receive anything within this time, the connection is closed.
9. proxy_upstream_fail_timeout(fail_timeout)

语法
server address [fail_timeout=30s]

默认值
10s

上下文
upstream

说明
Upstream模块下 server指令的参数,设置了某一个upstream后端失败了指定次数(max_fails)后,该后端不可操作的时间,默认为10秒

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容

  • Nginx简介 解决基于进程模型产生的C10K问题,请求时即使无状态连接如web服务都无法达到并发响应量级一万的现...
    魏镇坪阅读 1,958评论 0 9
  • 1.简介:  Nginx:engine X ,2002年,开源,商业版 http协议:web服务器(类似于ht...
    尛尛大尹阅读 1,828评论 0 3
  • I/O模型Nginx介绍Nginx的安装和目录结构Nginx的配置Nginx的编译安装 一、I/O模型 (一)I/...
    哈喽别样阅读 865评论 0 4
  • 前两天做一个文件上传,因功能逻辑特殊,需要上传文件并导入十几万条的数据,excel文件过大的话可以采用分段读取数据...
    狮子座鲸鱼阅读 3,406评论 0 0
  • 以下内容来自官方文档 http://nginx.org/en/docs/http/ngx_http_upstrea...
    再回到从前阅读 2,581评论 0 1