Nginx 解决WebSocket TCP 转发问题

时间:2021-03-22 13:40:26

 

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

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

 

Nginx 解决WebSocket TCP 转发问题

 

连接不了。

 

解决方法:

#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";

 

 

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