问题
I have 2 containers in Docker:
- Node.js + Webpack devserver which listen on port 3000
- Nginx which listen on port 80 (mapped on host port 8080 through docker-compose file)
Nginx correctly proxy requests to the node container and I can access the node app at http://localhost:8080, but for some reasons there are other polling requests of the type http://localhost:3000/sockjs-node/info?t=1570780621084 that fail (net::ERR_CONNECTION_REFUSED) because on the host only Nginx on port 8080 is visible. I think that these polling requests should be directed to Nginx (http://localhost:8080/sockjs-node/info?t=1570780621084) but I don't know what I have to change on the Webpack devserver configuration to fix that.
This is the web pack devserver configuration:
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
}),
]
};
This is the Nginx configuration:
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";
}
}
What I've tried so far was to add the following in the entry of the devserver config, but id didn't work.
entry: {
bundle: [
`webpack-dev-server/client?http://localhost:8080`,
"webpack/hot/dev-server",
"@babel/polyfill",
"./src/app.js",
]
},
How can I make Webpack devserver working correctly with Nginx in Docker?
回答1:
Assuming dev-server
is running on port 3000 and nginx on port 8080, this is what has worked for me (see web-devserver public option):
Nginx default.conf
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";
}
}
Webpack
// [...]
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,
},
// [...]
回答2:
I had a similar problem with the requests similar to
http://localhost:3000/sockjs-node/info?t=1570780621084
and the same error
(net::ERR_CONNECTION_REFUSED)
I have a react application on frontend and a nodejs application on backend. Each in a separate docker container. I use webpack-dev-server to run react application, that uses sockjs library that generates this sockjs-node request for its development purposes.
So to get it to work correctly I wrote the next options in the devServer section
host: '0.0.0.0', // you can change this ip with your ip
port: 4001, // ssl defult port number
public: 'https://localhost:9001',
publicPath: '/',
There are the ports in my docker-compose for the react application (one for HTTP and second for HTTPS)
ports:
- 9000:4000
- 9001:4001
So the 'public' option set to using port 9001 that accessible on the host machine. And I use it when want to load my application in a browser https://localhost:9001. Also, this URL used to load main.js script by the application.
But the 'port' option I have set to 4001. This port used by sockjs library to generate the request that we have problems with.
So be sure that you have opened an SSL port in a docker container with application that uses webpack-dev-server. And that you set the right values for 'public' and 'port' options in the devServer section of your webpack config file.
来源:https://stackoverflow.com/questions/58337248/webpack-devserver-with-nginx-and-docker-polling-info-on-the-wrong-address