2016-11-09 54 views
3

我想讓webpack-dev-server在Docker容器中運行,然後通過NGINX主機訪問它。加載初始的index.html,但與開發服務器的Web套接字連接無法連接。使用NGINX proxy_pass的Webpack開發服務器

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; 
    } 
} 

回答

5

代理通應的IP和端口您的WebPack-DEV-服務器容器,你需要proxy_redirect off;

location /sockjs-node { 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Forwarded-For $remote_addr; 
    proxy_set_header Host $host; 

    proxy_pass http://node:8080; 

    proxy_redirect off; 

    proxy_http_version 1.1; 
    proxy_set_header Upgrade $http_upgrade; 
    proxy_set_header Connection "upgrade"; 
} 

也不要忘記投票添加到您的WebPack-dev的中間件

watchOptions: { 
    aggregateTimeout: 300, 
    poll: 1000 
    } 
+1

我在HTTPS服務器的情況下工作了一些,並且遇到了一些問題,並解決了這些問題並在此處發佈了答案:http://stackoverflow.com/questions/43081342/webpack-dev-server-with- nginx的代理通換HTTPS -domain-causes-neterr-conne這將有助於其他人通過這篇文章。 – JoelParke