带有NGINX proxy_pass的Webpack Dev Server

时间:2016-11-09 21:26:00

标签: node.js nginx websocket webpack webpack-dev-server

我试图让webpack-dev-server在Docker容器内运行,然后通过NGINX主机访问它。最初index.html加载但与开发服务器的Web套接字连接无法连接。

  

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

1 个答案:

答案 0 :(得分:14)

代理传递应该是你的webpack-dev-server容器的ip和端口,你需要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
  }