Linux Nginx 配置 Websocket

直接上代码

# 负载均衡
upstream gzgc_balancer {
	#least_conn;
	ip_hash;
	# 这个是tomcat的访问路径
	server 127.0.0.1:8025 weight=1 max_fails=2 fail_timeout=3s;
	server 127.0.0.1:8026 weight=1 max_fails=2 fail_timeout=3s;
}

# 配置
 map $http_upgrade $connection_upgrade {
  default upgrade;
  ''   close;
 }


server {
	listen 端口;
	server_name ip;

	# websocket
	location /websocket{
   		proxy_pass http://gzgc_balancer/websocket;
   		proxy_read_timeout 300s;
   		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_http_version 1.1;
   		proxy_set_header Upgrade $http_upgrade;
   		proxy_set_header Connection $connection_upgrade;
	}

}

前端访问还是用ws://ip:端口

以上配置后端使用若依plus

你可能感兴趣的:(linux,nginx,websocket)