nginx代理后端接口和websocket
默认接口和websocket路径都是/ 所以需要在nginx里面判断
#对代理服务器的请求中的“ Connection”标头字段的值取决于客户端请求标头中是否存在“ Upgrade”字段
map $http_upgrade $connection_upgrade { default upgrade; '' close; } upstream roobot_server { server 172.17.0.1:9201; keepalive 2000; } server { listen 80; server_name ip地址; location / { proxy_pass http://roobot_server/; proxy_set_header Host $host:$server_port; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#以下两句是对http还是ws进行判断 都有则为ws proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_connect_timeout 300s; proxy_send_timeout 500s; proxy_read_timeout 500s; } location /roobot/ { root /usr/share/nginx/html; index index.html index.htm; } location ~.*(js|css|png|gif|jpg|mp3|ogg|ttf|woff)$ { root /usr/share/nginx/html/roobot; } }