Nginx 代理WebSocket

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

## √
upstream websocket {
  server 127.0.0.1:9999  weight=10 max_fails=2 fail_timeout=30s;
}

server {
  	listen 8020;
	gzip on;
	gzip_min_length 1k;
	gzip_comp_level 9;
	gzip_types text/plain application/javascript application/x-javascript text/xml text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
	gzip_vary on;
	gzip_disable "MSIE [1-6]\.(?!.*SV1)";
	error_page 411 = @my_error;
	location @my_error {
	}

	# 前端静态文件
	root  data;

	# 代理 websocket
  	location /admin/ws {
    	proxy_pass http://websocket/admin/ws;
    	proxy_http_version 1.1;
    	proxy_set_header Upgrade $http_upgrade;
    	proxy_set_header Connection $connection_upgrade;
    	proxy_set_header Host $host;
  	}

	# 代理前端
  	location /api/ {
    	proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
    	proxy_set_header        Host  $host;
	    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

    	proxy_set_header Upgrade $http_upgrade;
	    proxy_set_header Connection $connection_upgrade;
	    proxy_pass              http://websocket;
    	expires                 0;
	    proxy_http_version 1.1;
  }
}

Nginx 代理WebSocket_第1张图片

参考

Nginx配置WebSocket反向代理
websocket-nginx

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