【问题标题】:Webpack devserver with Nginx and Docker: polling info on the wrong address带有 Nginx 和 Docker 的 Webpack 开发服务器:轮询错误地址的信息
【发布时间】:2019-10-11 08:32:22
【问题描述】:

我在Docker 中有 2 个容器:

Nginx 正确地将请求代理到节点容器,我可以在 http://localhost:8080 访问节点应用程序,但由于某些原因,还有其他类型为 http://localhost:3000/sockjs-node/info?t=1570780621084 的轮询请求失败(net::ERR_CONNECTION_REFUSED),因为在主机上只有端口 8080 上的 Nginx 可见。我认为这些轮询请求应该被定向到 Nginx (http://localhost:8080/sockjs-node/info?t=1570780621084) 但我不知道我必须在 Webpack 开发服务器配置上进行哪些更改才能解决这个问题。

这是 webpack 开发服务器配置:

const path = require("path");
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const {
    BUILD_DIR,        
} = require("./config/config");

module.exports = {
    entry: {
        bundle: [
            "@babel/polyfill", 
            "./src/app.js",
        ]
    },

    output: {
        path: BUILD_DIR,
        filename: "[name].[hash].js"
    },

    devtool: "inline-source-map",

    devServer: {
        host: "localhost",
        port: 3000,        
        contentBase: BUILD_DIR,
        historyApiFallback: false,
        hot: true,
        inline: true,
        watchOptions: {
            poll: true
        },
        disableHostCheck: true,
    },

    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            },
            {
                test: /\.css$/,
                use: ["style-loader", "css-loader"],
            },
            {
                test: /\.scss$/,
                use: ["style-loader", "css-loader", "sass-loader"]
            }
        ],
    },

    plugins: [
        new CleanWebpackPlugin(),

        new HtmlWebpackPlugin({
            template: "src/index.html",
            filename: "index.html",
            inject: true
        }),
    ]
};

这是 Nginx 配置:

upstream reactclient {
    server react-client:3000 fail_timeout=20s max_fails=10;
}

server {
    listen 80;

    location / {
        proxy_pass http://reactclient;
    }   

    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://reactclient;
        proxy_redirect off;

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

到目前为止,我尝试的是在 devserver 配置的条目中添加以下内容,但 id 不起作用。

entry: {
        bundle: [
            `webpack-dev-server/client?http://localhost:8080`,
            "webpack/hot/dev-server",
            "@babel/polyfill", 
            "./src/app.js",
        ]
    },

如何让 Webpack 开发服务器在 Docker 中与 Nginx 一起正常工作?

【问题讨论】:

    标签: javascript docker nginx webpack webpack-dev-server


    【解决方案1】:

    我对类似的请求也有类似的问题

    http://localhost:3000/sockjs-node/info?t=1570780621084

    同样的错误

    (net::ERR_CONNECTION_REFUSED)

    我在前端有一个 react 应用程序,在后端有一个 nodejs 应用程序。 每个都在一个单独的 docker 容器中。 我使用 webpack-dev-server 来运行 react 应用程序,该应用程序使用 sockjs 库生成此 sockjs-node 请求以用于其开发目的。

    为了让它正常工作,我在 devServer 部分编写了下一个选项

    host: '0.0.0.0', // you can change this ip with your ip
    port: 4001, // ssl defult port number
    public: 'https://localhost:9001',
    publicPath: '/',
    

    我的 docker-compose 中有一些端口用于反应应用程序(一个用于 HTTP,第二个用于 HTTPS)

    ports:
      - 9000:4000
      - 9001:4001
    

    因此,“公共”选项设置为使用主机上可访问的端口 9001。 当我想在浏览器 https://localhost:9001 中加载我的应用程序时,我会使用它。 此外,此 URL 用于由应用程序加载 main.js 脚本。

    但我已将“端口”选项设置为 4001。sockjs 库使用此端口来生成我们遇到问题的请求。

    因此,请确保您已在使用 webpack-dev-server 的应用程序的 docker 容器中打开 SSL 端口。 并且您在 webpack 配置文件的 devServer 部分中为“public”和“port”选项设置了正确的值。

    【讨论】:

      【解决方案2】:

      假设 dev-server 在端口 3000 上运行,nginx 在端口 8080 上运行,这对我有用(参见 web-devserver public option):

      Nginx 默认配置文件

      upstream reactclient {
          server react_client:3000 fail_timeout=20s max_fails=100;
      }
      
      server {
          listen 80;
      
          location / {
              proxy_pass http://reactclient;
          }       
      
          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://reactclient;
              proxy_redirect off;
      
              proxy_http_version 1.1;
              proxy_set_header Upgrade $http_upgrade;
              proxy_set_header Connection "upgrade";  
          }
      }
      

      网页包

      // [...]
      devServer: {
              host: '0.0.0.0',
              port: 3000,
              public: `localhost:8080`,
              historyApiFallback: false,
              hot: true,
              inline: true,
              watchOptions: {
                  aggregateTimeout: 300,
                  poll: 1000,
                  ignored: /node_modules/,
              },
              disableHostCheck: true,
          },
      // [...]
      

      【讨论】:

        猜你喜欢
        • 2017-03-23
        • 1970-01-01
        • 2021-08-05
        • 1970-01-01
        • 2017-01-06
        • 1970-01-01
        • 2019-01-15
        • 2022-11-13
        • 1970-01-01
        相关资源
        最近更新 更多