I have a webpack configuration of an https webserver. When an http request arrives, it just returns ERR_CONNECTION_REFUSED. How can I configure it to forward all http requests to https?
on package.json, the relevant script is:
"start:staging": "webpack-dev-server --config config/webpack.server.stage.config.js --mode production --open --host 0.0.0.0 --port 443",
on webpack.server.config.js:
module.exports = {
...
devServer: {
historyApiFallback: true,
contentBase: '/',
public: <my domain>,
https: {
key: key,
cert: cert,
ca: ca
}
}
...
I tried playing with proxy
options but couldn't make it to work.
Any assistance would be much appreciated.
I have a webpack configuration of an https webserver. When an http request arrives, it just returns ERR_CONNECTION_REFUSED. How can I configure it to forward all http requests to https?
on package.json, the relevant script is:
"start:staging": "webpack-dev-server --config config/webpack.server.stage.config.js --mode production --open --host 0.0.0.0 --port 443",
on webpack.server.config.js:
module.exports = {
...
devServer: {
historyApiFallback: true,
contentBase: '/',
public: <my domain>,
https: {
key: key,
cert: cert,
ca: ca
}
}
...
I tried playing with proxy
options but couldn't make it to work.
Any assistance would be much appreciated.
- 1 You need a second server that listens on port 80 and redirects the requests. – user5734311 Commented Jul 8, 2019 at 11:08
- 1 i think its not possible via webpack, you may want to try this via apache or ngnix – turtle Commented Jul 8, 2019 at 11:18
- @ChrisG is it something I can achieve with configuration only? or do I need to explicitly create a new webserver for this? – Shirkan Commented Jul 8, 2019 at 11:22
- Forwarding (as in proxying) HTTP to HTTPS pletely negates any advantage of HTTPS and adds pointless plexity and overhead. So I assume you actually want redirection, which forces the client to retry the request against different URL (in this case HTTPS instead of HTTP). The question arises though: why don't you resolve the issue on the client side to begin with? Why can't he connect via HTTPS from the start? – freakish Commented Jul 8, 2019 at 14:08
- @freakish My intention is to handle the situation where a user explicitly tries to connect via HTTP – Shirkan Commented Jul 8, 2019 at 14:39
2 Answers
Reset to default 4It seems that webpack have issue for this feature. Just linking it for anybody who would e here https://github./webpack/webpack-dev-server/issues/126.
You need a second server that listens on port 80 and redirects the requests. – Chris G
Investigated some more and found this to be the answer:
const express = require('express');
// Forward http requests to https
var app = express();
app.listen(80);
app.use(function(req, res, next) {
if(!req.secure) {
return res.redirect(['https://', req.get('Host'), req.url].join(''));
}
next();
});