Nginx作为一款强大的Web服务器和反向代理服务器,在配置WSS(WebSocket Secure)和SSE(Server-Sent Events)方面有着出色的表现。接下来,咱们就详细聊聊如何在Nginx中进行这两项配置,以及一些优化技巧。

一、WSS配置详解

(一)变量定义处理请求头

在Nginx配置中,首先要定义变量来处理WebSocket的UpgradeConnection请求头,代码如下:

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

这段代码的作用是根据$http_upgrade的值来动态设置$connection_upgrade的值。简单来说,如果$http_upgrade有值,$connection_upgrade就被设置为upgrade;要是$http_upgrade为空,$connection_upgrade就被设置为close 。这一步为后续支持WebSocket协议奠定了基础。

(二)配置WebSocket代理

完成变量定义后,接着要配置WebSocket代理,具体代码如下:

location ^~ /ws/ { proxy_pass http://127.0.0.1:8000; # 转发到后端WebSocket服务器 proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_read_timeout 300s; proxy_send_timeout 300s; proxy_connect_timeout 3600s; } 
  • proxy_pass http://127.0.0.1:8000;:表示将匹配/ws/路径的请求转发到后端地址为127.0.0.1:8000的WebSocket服务器上。
  • proxy_http_version 1.1;:使用HTTP/1.1协议,因为WebSocket协议需要基于HTTP/1.1才能正常工作。
  • proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection $connection_upgrade;:这两行代码确保请求头中包含UpgradeConnection字段,这是支持WebSocket协议的关键设置。
  • 后面几行设置的超时时间都比较长,像proxy_read_timeout 300s;proxy_send_timeout 300s;proxy_connect_timeout 3600s; ,这么做是为了防止因为长时间没有活动而导致连接断开,保证WebSocket连接的稳定性。

(三)启用SSL/TLS支持WSS

如果项目需要支持WSS(即WebSocket通过安全的HTTPS协议进行通信),那就得在Nginx中配置SSL/TLS,示例代码如下:

server { listen 443 ssl; server_name your_domain.com; ssl_certificate /path/to/your/certificate.pem; ssl_certificate_key /path/to/your/private.key; # WebSocket配置 location ^~ /ws/ { # WebSocket代理配置 } } 
  • listen 443 ssl;:表示服务器监听443端口,并启用SSL加密。
  • server_name your_domain.com;:这里要填写你实际的域名。
  • ssl_certificatessl_certificate_key分别指定SSL证书文件和私钥文件的路径。配置好这些后,WebSocket连接就会通过HTTPS进行加密,保障数据传输的安全性。

二、SSE配置与优化

(一)配置SSE代理

下面来看看SSE的配置,代码如下:

location ~ ^/sse/ { proxy_pass http://127.0.0.1:8000; # 转发到后端SSE服务器 proxy_http_version 1.1; proxy_set_header Connection ''; proxy_buffering off; proxy_cache off; proxy_read_timeout 3600s; proxy_send_timeout 3600s; proxy_connect_timeout 1h; proxy_set_header Host $host; proxy_set_header X-Accel-Buffering no; add_header Cache-Control no-cache; chunked_transfer_encoding on; gzip off; # 跨域资源共享(CORS)配置 add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Allow-Credentials' 'true' always; add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS' always; add_header 'Access-Control-Allow-Headers' 'Origin,Authorization,Accept,X-Requested-With' always; if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'Origin,Authorization,Accept,X-Requested-With'; add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain charset=UTF-8'; add_header 'Content-Length' 0; return 204; } } 
  • proxy_pass http://127.0.0.1:8000;:将匹配/sse/路径的请求转发到后端地址为127.0.0.1:8000的SSE服务器。
  • proxy_http_version 1.1;:SSE同样需要HTTP/1.1协议的支持。
  • proxy_buffering off;proxy_cache off;:这两个设置是为了确保Nginx不会缓存响应,而是直接把数据发送给客户端,保证SSE数据的实时性。
  • proxy_read_timeoutproxy_connect_timeout设置得比较长,像proxy_read_timeout 3600s;proxy_connect_timeout 1h; ,目的是支持SSE的长连接特性。
  • add_header Cache-Control no-cache;:明确告诉客户端不要缓存响应,保证每次获取的都是最新数据。
  • 跨域资源共享(CORS)配置:如果客户端和服务器不在同一个域名下,就需要配置CORS。这部分配置通过添加多个add_header指令,设置了允许跨域的源、凭证、方法和请求头。if ($request_method = 'OPTIONS')这部分代码则专门处理预检请求,返回相应的CORS头信息。

(二)优化SSE性能

为了进一步提升SSE的性能,还可以进行以下优化:

  • 启用HTTP/2协议:在listen指令中添加http2参数,代码如下:
listen 443 ssl http2; 

HTTP/2协议相比HTTP/1.1有很多优势,比如多路复用、头部压缩等,能有效提升数据传输效率。

  • 启用Gzip压缩:通过以下配置启用Gzip压缩:
gzip on; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; 

这样可以对指定类型的文件进行压缩,减少数据传输量,提高传输速度。

通过上述详细的配置步骤和优化方法,Nginx就能很好地支持WSS和SSE,在实际项目中,大家可以根据具体需求对这些配置进行调整和完善。