node.js – 带有NGINX proxy_pass的Webpack Dev Server

前端之家收集整理的这篇文章主要介绍了node.js – 带有NGINX proxy_pass的Webpack Dev Server前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

VM47:35 WebSocket connection to ‘ws://example.com/sockjs-node/834/izehemiu/websocket’ Failed: Error during WebSocket handshake: Unexpected response code: 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;
  }
}
最佳答案
代理传递应该是你的webpack-dev-server容器的ip和端口,你需要关闭proxy_redirect;

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

另外,不要忘记将poll添加到webpack-dev中间件

  watchOptions: {
    aggregateTimeout: 300,poll: 1000
  }
原文链接:https://www.f2er.com/nginx/434988.html

猜你在找的Nginx相关文章