【发布时间】:2019-10-11 08:32:22
【问题描述】:
我在Docker 中有 2 个容器:
- Node.js + Webpack devserver 监听 3000 端口
- Nginx 监听端口 80(映射到 主机端口 8080 到 docker-compose 文件)
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