最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to forward http requests to https with webpack? - Stack Overflow

programmeradmin2浏览0评论

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.

Share Improve this question edited Jul 8, 2019 at 11:56 turtle 1,6191 gold badge14 silver badges30 bronze badges asked Jul 8, 2019 at 11:06 ShirkanShirkan 9971 gold badge9 silver badges14 bronze badges 11
  • 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
 |  Show 6 more ments

2 Answers 2

Reset to default 4

It 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();
});
发布评论

评论列表(0)

  1. 暂无评论