我正在尝试将webpack-dev-server
运行在 Docker 容器内,并通过 NGINX 主机访问它。初始的index.html
加载成功,但是与开发服务器的 Web Sockets 连接无法连接。
VM47:35 WebSocket连接到'ws://example.com/sockjs-node/834/izehemiu/websocket'失败:WebSocket握手过程中出错:意外的响应代码:400
我正在使用以下配置。
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream webpack_dev_server {
server node;
}
server {
server_name _;
listen 80;
root /webpack_dev_server;
location / {
proxy_pass http://webpack_dev_server;
}
location /sockjs-node/ {
proxy_pass http://webpack_dev_server/sockjs-node/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host; # pass the host header - http://wiki.nginx.org/HttpProxyModule#proxy_pass
proxy_http_version 1.1; # recommended with keepalive connections - http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_http_version
# WebSocket proxying - from http://nginx.org/en/docs/http/websocket.html
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
proxy_redirect
并不是必需的。更重要的是,它可能是错误的,并导致无效的重定向。 - Paweł Szczur