Shirkan
Shirkan

Reputation: 987

How to forward http requests to https with webpack?

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.

Upvotes: 4

Views: 3910

Answers (2)

Petr Přikryl
Petr Přikryl

Reputation: 1802

It seems that webpack have issue for this feature. Just linking it for anybody who would come here https://github.com/webpack/webpack-dev-server/issues/126.

Upvotes: 4

Shirkan
Shirkan

Reputation: 987

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();
});

Upvotes: 1

Related Questions