Nginx WebSocket 配置

原创
2018/02/07 15:15
阅读数 6.7K

server {
    listen       80;
    server_name  bi.com;
    access_log  /var/log/nginx/bi.access.log  main;
    error_log  /var/log/nginx/bi.error.log  ;
    # web socket  location must be  / ,otherwise failed.
    location / {
        access_log /var/log/nginx/ws.access.log main;
        error_log /var/log/nginx/ws.err.log;
        proxy_pass http://ws;
        proxy_http_version 1.1;
        proxy_set_header    Upgrade $http_upgrade;
        proxy_set_header    Connection "upgrade";

    }

    location /bi/ {

        // app context path
        proxy_pass http://bi/;
    }
}

upstream bi {
    ip_hash;
    server 172.16.8.2:1904;

    server 172.16.8.3:1904;
}
upstream ws {
    ip_hash;
    server 172.16.8.16:1903;

    server 172.16.8.17:1093;
}

Note: 如果使用了htts 或者是SSL, 请使用wss协议代替ws协议.

如下是一个完整的配置:

      proxy_pass http://backend/;
      proxy_redirect      off;
      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_set_header    X-Forwarded-Proto $scheme;

      # WebSocket specific
      proxy_http_version 1.1;
      proxy_set_header    Upgrade           $http_upgrade;
      proxy_set_header    Connection        "upgrade";

      #
      # Specific for comet or long running HTTP requests, don't buffer up the
      # response from origin servers but send them directly to the client.
      #
      proxy_buffering     off;

      #
      # Bump the timeout's so someting sensible so our connections don't
      # disconnect automatically. We've set it to 12 hours.
      #
      proxy_connect_timeout 43200000;
      proxy_read_timeout    43200000;
      proxy_send_timeout    43200000;

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
3 收藏
0
分享
返回顶部
顶部