I am getting an “Invalid Host header” message when connecting to webpack-dev-server remotely

后端 未结 10 1969
粉色の甜心
粉色の甜心 2020-12-02 04:57

I am using as an environment, a Cloud9.io ubuntu VM Online IDE and I have reduced by troubleshooting this error to just running the app with Webpack dev server.

I l

相关标签:
10条回答
  • 2020-12-02 05:25

    Rather than editing the webpack config file, the easier way to disable the host check is by adding a .env file to your root folder and putting this:

    DANGEROUSLY_DISABLE_HOST_CHECK=true
    

    As the variable name implies, disabling it is insecure and is only advisable to use only in dev environment.

    0 讨论(0)
  • 2020-12-02 05:28

    If you are using create-react-app on C9 just run this command to start

    npm run start --public $C9_HOSTNAME
    

    And access the app from whatever your hostname is (eg type $C_HOSTNAME in the terminal to get the hostname)

    0 讨论(0)
  • 2020-12-02 05:33

    Hello React Developers,

    Instead of doing this disableHostCheck: true, in webpackDevServer.config.js. You can easily solve 'invalid host headers' error by adding a .env file to you project, add the variables HOST=0.0.0.0 and DANGEROUSLY_DISABLE_HOST_CHECK=true in .env file. If you want to make changes in webpackDevServer.config.js, you need to extract the react-scripts by using 'npm run eject' which is not recommended to do it. So the better solution is adding above mentioned variables in .env file of your project.

    Happy Coding :)

    0 讨论(0)
  • 2020-12-02 05:35

    The more secure option would be to add allowedHosts to your Webpack config like this:

    module.exports = {
    devServer: {
     allowedHosts: [
      'host.com',
      'subdomain.host.com',
      'subdomain2.host.com',
      'host2.com'
       ]
      }
    };
    

    The array contains all allowed host, you can also specify subdomians. check out more here

    0 讨论(0)
  • 2020-12-02 05:37

    This is what worked for me:

    Add allowedHosts under devServer in your webpack.config.js:

    devServer: {
      compress: true,
      inline: true,
      port: '8080',
      allowedHosts: [
          '.amazonaws.com'
      ]
    },
    

    I did not need to use the --host or --public params.

    0 讨论(0)
  • 2020-12-02 05:39

    I found out, that I need to set the public property of devServer, to my request's host value. Being that it will be displayed at that external address.

    So I needed this in my webpack.config.js

    devServer: {
      compress: true,
      public: 'store-client-nestroia1.c9users.io' // That solved it
    }
    

    Another solution is using it on the CLI:

    webpack-dev-server --public $C9_HOSTNAME <-- var for Cloud9 external IP

    0 讨论(0)
提交回复
热议问题