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

后端 未结 10 1970
粉色の甜心
粉色の甜心 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:44

    Add this config to your webpack config file when using webpack-dev-server (you can still specify the host as 0.0.0.0).

    devServer: {
        disableHostCheck: true,
        host: '0.0.0.0',
        port: 3000
    }
    
    0 讨论(0)
  • 2020-12-02 05:48

    The problem occurs because webpack-dev-server 2.4.4 adds a host check. You can disable it by adding this to your webpack config:

     devServer: {
        compress: true,
        disableHostCheck: true,   // That solved it
    
     }      
    

    EDIT: Please note, this fix is insecure.

    Please see the following answer for a secure solution: https://stackoverflow.com/a/43621275/5425585

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

    If you have not ejected from CRA yet, you can't easily modify your webpack config. The config file is hidden in node_modules/react_scripts/config/webpackDevServer.config.js. You are discouraged to change that config.

    Instead, you can just set the environment variable DANGEROUSLY_DISABLE_HOST_CHECK to true to disable the host check:

    DANGEROUSLY_DISABLE_HOST_CHECK=true yarn start  
    # or the equivalent npm command
    
    0 讨论(0)
  • 2020-12-02 05:49

    If you are running webpack-dev-server in a container and are sending requests to it via its container name, you will get this error. To allow requests from other containers on the same network, simply provide the container name (or whatever name is used to resolve the container) using the --public option. This is better than disabling the security check entirely.

    In my case, I was running webpack-dev-server in a container named assets with docker-compose. I changed the start command to this:

    webpack-dev-server --mode development --host 0.0.0.0 --public assets
    

    And the other container was now able to make requests via http://assets:5000.

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