WebSocket配置信息
1. 基础介绍:
Nginx1.3.13版本以上,开始支持WebSocket协议。
WebSocket协议是基于TCP的一种网络协议,实现浏览器和服务器全双工(full-duplex)通信 — 允许服务器主动发送信息给客户端。
WebSocket协议中,实现即时任务两大好处:1.Header很小(约2Bytes); 2.Server Push 服务器主动推送给浏览器。
WebSocket工作在HTTP的80和443端口并使用前缀ws://或者wss://进行协议标注,在建立连接时使用HTTP/1.1的101状态码进行协议切换,当前标准不支持两个客户端之间不借助HTTP直接建立Websocket连接。
2. Nginx配置WebSocket注意事项:
- 是否Nginx配置中,同时存在WebSocket 和 Http
- 默认情况下,
proxy_read_timeout是60s;两次传输时长超过这个,就会断开连接! —解决— 代理服务器配置为定期发送WebSocket ping帧以重置超时
3. 客户端和服务端的报文
<!--客户端的连接报文-->GET /webfin/websocket/ HTTP/1.1Host: localhostUpgrade: websocketConnection: UpgradeSec-WebSocket-Key: xqBt3ImNzJbYqRINxEFlkg==Origin: http://localhost:8080Sec-WebSocket-Version: 13
<!--服务端的相应报文-->HTTP/1.1 101 Switching ProtocolsUpgrade: websocketConnection: UpgradeSec-WebSocket-Accept: K7DJLdLooIwIG/MOpvWFB3y3FE8=
4. Nginx的配置参数
# HTTP配置中,添加server关于websocket的配置# 为测试websocket添加# 1. 如果 $http_upgrade 不为 '' (空),则 $connection_upgrade 为 upgrade# 2. 如果 $http_upgrade 为 '' (空),则 $connection_upgrade 为 closemap $http_upgrade $connection_upgrade {# default upgrade;# '' close;default keep-alive; #默认为keep-alive 可以支持 一般http请求'websocket' upgrade; #如果为websocket 则为 upgrade 可升级的。}# 表示的是 nginx负载均衡# 1. 两台服务器 (ip1:port1)和(ip2:port2)# 2. keepalive 1000 表示的是每个nginx进程中上游服务器保持的空闲连接,当空闲连接过多时,会关闭最少使用的空闲连接.当然,这不是限制连接总数的,可以想象成空闲连接池的大小.设置的值应该是上游服务器能够承受的upstream wsbackend{server ip1:port1;server ip2:port2;keepalive 1000;}# 添加测试websocket的代理# 表示的是监听的服务器的配置# 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_upgrade为upgrade,则请求为upgrade(websocket),如果不是,就关闭连接server {listen 12443;location /socketServer {# 反向代理到 EMQ 非加密 WebSocket ws# proxy_pass http://127.0.0.1:58080;proxy_pass http://wsbackend;# 反向代理保留客户端地址proxy_set_header X-Real_IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# WebSocket 额外请求头proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection $connection_upgrade;# WebSocket 设置断开连接的时间proxy_read_timeout 1800s;}}
4. Nginx配置模板:
######## Nginx的main(全局配置)文件#指定nginx运行的用户及用户组,默认为nobody#user nobody;#开启的线程数,一般跟逻辑CPU核数一致worker_processes 1;#定位全局错误日志文件,级别以notice显示,还有debug,info,warn,error,crit模式,debug输出最多,crir输出最少,根据实际环境而定#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#指定进程id的存储文件位置#pid logs/nginx.pid;#指定一个nginx进程打开的最多文件描述符数目,受系统进程的最大打开文件数量限制#worker_rlimit_nofile 65535events {#设置工作模式为epoll,除此之外还有select,poll,kqueue,rtsig和/dev/poll模式#use epoll;#定义每个进程的最大连接数,受系统进程的最大打开文件数量限制。worker_connections 1024;}#######Nginx的Http服务器配置,Gzip配置http {#主模块指令,实现对配置文件所包含的文件的设定,可以减少主配置文件的复杂度,DNS主配置文件中的zonerfc1912,acl基本上都是用include语句。include mime.types;#核心模块指令,智力默认设置为二进制流,也就是当文件类型未定义时使用这种方式default_type application/octet-stream;#下面代码为日志格式的设定,main为日志格式的名称,可自行设置,后面引用#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#引用日志main#access_log logs/access.log main;#设置允许客户端请求的最大的单个文件字节数#client_max_body_size 20M;#指定来自客户端请求头的headebuffer大小#client_header_buffer_size 32k;#指定连接请求试图写入缓存文件的目录路径#client_body_temp_path /dev/shm/client_body_temp;#指定客户端请求中较大的消息头的缓存最大数量和大小,目前设置为4个32KB#large client_header_buffers 4 32k;client_max_body_size 10240m;keepalive_requests 10240;client_header_buffer_size 8k;open_file_cache max=102400 inactive=20s;open_file_cache_valid 30s;open_file_cache_min_uses 1;client_header_timeout 15;client_body_timeout 15;reset_timedout_connection on;send_timeout 15;#开启高效文件传输模式sendfile on;#开启防止网络阻塞#tcp_nopush on;#开启防止网络阻塞#tcp_nodelay on;#设置客户端连接保存活动的超时时间#keepalive_timeout 0;keepalive_timeout 65;#设置客户端请求读取超时时间#client_header_timeout 10;#设置客户端请求主体读取超时时间#client_body_timeout 10;#用于设置相应客户端的超时时间#send_timeout####HttpGZip模块配置#httpGzip modules#开启gzip压缩#gzip on;#设置允许压缩的页面最小字节数#gzip_min_length 1k;#申请4个单位为16K的内存作为压缩结果流缓存#gzip_buffers 4 16k;#设置识别http协议的版本,默认为1.1#gzip_http_version 1.1;#指定gzip压缩比,1-9数字越小,压缩比越小,速度越快#gzip_comp_level 2;#指定压缩的类型#gzip_types text/plain application/x-javascript text/css application/xml;#让前端的缓存服务器进过gzip压缩的页面#gzip_vary on;#########Nginx的server虚拟主机配置server {#监听端口为 80listen 80;#设置主机域名server_name localhost;#设置访问的语言编码#charset koi8-r;#设置虚拟主机访问日志的存放路径及日志的格式为main#access_log logs/host.access.log main;#设置虚拟主机的基本信息location / {#设置虚拟主机的网站根目录root html;#设置虚拟主机默认访问的网页index index.html index.htm;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.htmlerror_page 500 502 503 504 /50x.html;location = /50x.html {root html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}}
