Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not use webpack-dev-server as proxy to https website - ERR_TLS_CERT_ALTNAME_INVALID

I try to use the webpack-dev-server as a proxy for API requests to the remote test server (in order not to raise a heavy Java server on the local machine). The problem is that the remote server uses only HTTPS. For some reason, it does not accept the certificate from the webpack-dev-server, although it opens correctly from the browser.

My package.json part:

"webpack": "^3.12.0",
"webpack-dev-server": "^2.11.3"

My webpack.config.json part:

// -- DEV-SERVER
devServer: {
    contentBase: artifact,
    // publicPath: path.join(artifact, 'build', '/'),
    host: "lk-local.net",
    port: 8443,
    https: {
        key: fs.readFileSync('./src/main/profiles/local/cert/webpack/server.key'),
        cert: fs.readFileSync('./src/main/profiles/local/cert/webpack/server.crt'),
        ca: fs.readFileSync('./src/main/profiles/local/cert/webpack/cacert.crt'),
    },
    historyApiFallback: true,
    hot: true,
    headers: {'Access-Control-Allow-Origin': '*'},
    proxy: {
        '/client-config': 'https://dev-2.MYSERVER.ru/client-config',
        '/client-api/*': 'https://dev-2.MYSERVER.ru/client-api/',
        secure: false,
        changeOrigin: true
    }
}

Webpack-dev-server output:

webpack: Compiled successfully.
[HPM] Error occurred while trying to proxy request /client-api/checkBrowser from lk-local.net:8443 to https://dev-2.MYSERVER.ru/client-api/ (ERR_TLS_CERT_ALTNAME_INVALID) (https://nodejs.org/api/errors.html#errors_common_system_errors)
[HPM] Error occurred while trying to proxy request /client-config from lk-local.net:8443 to https://dev-2.MYSERVER.ru/client-config (ERR_TLS_CERT_ALTNAME_INVALID) (https://nodejs.org/api/errors.html#errors_common_system_errors)

Static opens correctly, requests to the API do not work.

I tried to use a different certificates, and replace https with the true. The result is unchanged. How to solve this problem? Thanks!

like image 237
Evgeniy Karpov Avatar asked Nov 19 '18 06:11

Evgeniy Karpov


1 Answers

I believe your changeOrigin and secure options aren't specified in the right way. The '/client-config': 'https://dev-2.MYSERVER.ru/client-config' form doesn't support them if I'm reading the docs properly. You can do either:

proxy: {
    '/client-config': {
        target: 'https://dev-2.MYSERVER.ru/client-config',
        secure: false,
        changeOrigin: true
    },
    ...
}

or

proxy: [
  {
    context: ['/client-config', '/client-api/*'],
    target: 'https://dev-2.MYSERVER.ru/',
    secure: false,
    changeOrigin: true
  }
]

btw, if your browser doesn't give the security warnings, you shouldn't need the secure: false bit, and much better to leave it out.

like image 75
Scott Lamb Avatar answered Sep 19 '22 07:09

Scott Lamb