Webpack Dev Server with NGINX proxy_pass

I am trying to run a webpack-dev-serverDocker inside the container and then access it through the NGINX host. Bootstrap index.html, but connecting web sockets to the dev server cannot be connected.

VM47: 35 WebSocket connection failed with 'ws: //example.com/sockjs- node / 834 / izehemiu / websocket' failed: WebSocket handshake error: Unexpected response code: 400

I am using the following configuration.

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;
  }
}
+4
source share
1 answer

The proxy pass must be the ip and port of your webpack-dev-server container, and you need 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
  }
+7

Source: https://habr.com/ru/post/1660345/


All Articles