Nginx 解决WebSocket TCP 转发问题

Posted 指针怒草内存栈

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx 解决WebSocket TCP 转发问题相关的知识,希望对你有一定的参考价值。

 

背景:   IM 即时通讯时候  , 前期我用的是IP 没什么问题,当然上线肯定要搞个域名搞搞了!  

   那么问题来了------》nginx  我按照原先那样配置时候不行了,

 

 

连接不了。

 

解决方法:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  \'$remote_addr - $remote_user [$time_local] "$request" \'
    #                  \'$status $body_bytes_sent "$http_referer" \'
    #                  \'"$http_user_agent" "$http_x_forwarded_for"\';

    #access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;


    gzip  on;
     map $http_upgrade $connection_upgrade {
        default upgrade;
        \'\'      close;
    }


    server {
        listen       80;
        server_name  manage.tbnb1.cn;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

    proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        location / {
        proxy_pass http://127.0.0.1:8081;
        proxy_connect_timeout 600;
        proxy_read_timeout 600;
        }
        
    }
server {
        listen       80;
        server_name  sso.tbnb1.cn;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

    proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        location / {
        proxy_pass http://127.0.0.1:8099;
        proxy_connect_timeout 600;
        proxy_read_timeout 600;
        }
        
    }
    server {
        listen       80;
        server_name  q.tbnb1.cn;


        #charset koi8-r;

        #access_log  logs/host.access.log  main;
    
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        location / {
        proxy_pass http://127.0.0.1:8089;
        proxy_connect_timeout 600;
        proxy_read_timeout 600;
        }
        
    }
    server {
        listen       80;
        server_name  message.tbnb1.cn;


        #charset koi8-r;

        #access_log  logs/host.access.log  main;
    
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        location / {
        proxy_pass http://127.0.0.1:8083;
        
        
        proxy_connect_timeout 600;
        proxy_read_timeout 600;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        }
        
    }


    }
    

 

重要位置需要配置的

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

 

proxy_http_version 1.1;

proxy_set_header Upgrade $http_upgrade;

proxy_set_header Connection "upgrade";

 

 

完美解决问题了!  什么东西都要自己去动手 ,你才能发现问题解决问题!  每天用嘴说反向代理  ,负载均衡!  然后都他妈的空谈!

 

以上是关于Nginx 解决WebSocket TCP 转发问题的主要内容,如果未能解决你的问题,请参考以下文章

解决nginx转发websocket报400错误

解决nginx转发websocket报400错误

解决nginx转发websocket报400错误

小程序WebSocket通过F5无法转发问题解决

使用Nginx为TCP/WebSocket协议做反向代理和几个易踩的坑

Docker NGINX 代理不转发 Websocket