Nginx做负载均衡之后,WebSocket无法直接连接到服务器,需要进行一些额外的配置

nginx.conf配置:

http节点下:

  1. #websocket配置
  2. map $http_upgrade $connection_upgrade {
  3. default upgrade;
  4. '' close;
  5. }
  1. server {
  2. listen 80;
  3. server_name www.easyboot.xyz;
  4. location / {
  5. proxy_pass http://localhost:8080;
  6. #配置Nginx支持webSocket开始
  7. proxy_set_header Host $http_host;
  8. proxy_http_version 1.1;
  9. proxy_set_header Upgrade $http_upgrade;
  10. proxy_set_header Connection "upgrade";
  11. }
  12. }

注意上述的「map」与「server」节点平级