Nginx WebSocket 配置

in 编程
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9

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;

 

关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9
扫一扫关注公众号添加购物返利助手,领红包
Comments are closed.

推荐使用阿里云服务器

超多优惠券

服务器最低一折,一年不到100!

朕已阅去看看