nginx配置websocket(前后端分离)

js中

if (window.WebSocket) {
    /*当前环境前端的host,形如:localhost:8089 */
    var host = window.location.host;
    websocket = new WebSocket("ws://"+ host + "/websocket/onlineWebsocket");
}

nginx配置

upstream pgcmng-project {
    server 127.0.0.1:8601;
}


location ^~ /websocket/ {
	proxy_pass  http://pgcmng-project/pgcmng/websocket/;
	proxy_set_header  X-Real-IP  $remote_addr;
	proxy_set_header Host $host:1443;
	proxy_http_version 1.1;
	proxy_set_header Connection keep-alive;
	proxy_set_header Keep-Alive 600;
	keepalive_timeout 600;
	proxy_set_header Upgrade $http_upgrade;
	proxy_set_header Connection "upgrade";

}

最终

ws://"+ host + "/websocket/onlineWebsocket
转换为:
http://127.0.0.1:8601/pgcmng/websocket/onlineWebsocket

你可能感兴趣的:(nginx)