[Solved] Invalid Host Header when ngrok tries to connect to React dev server – Ngrok

Photo of author
Written By M Ibrahim
localhost ngrok reactjs

The Problem:

A developer is attempting to test a React application on a mobile device using ngrok to make the application accessible to other devices. The application seems to work well with other devices, but when they try to connect ngrok to the React dev server, they encounter an ‘Invalid Host Header’ error. They suspect that React may be blocking requests from unknown sources by default.

The Solutions:

Solution 1: Update Ngrok to accept Different Host Headers

When using ngrok to connect to a React dev server, you may encounter an "Invalid Host Header" error. This happens because React, by default, blocks requests from another source. To resolve this, you can configure ngrok to accept a different host header.

Steps:

  1. Open your command prompt or terminal.

  2. Navigate to the directory where you have installed ngrok.

  3. Run the following command to start ngrok with a custom host header:

ngrok http --host-header=rewrite 8080

In this command, replace "8080" with the port number where your React dev server is running.

  1. This solution should allow you to use ngrok to connect to your React dev server without encountering the "Invalid Host Header" error.

Note: If you are experiencing the error in an embedded page that pulls the bundle.js from the React app, this solution may not work. This issue arises because ngrok rewrites the header to localhost, where the app is now operating.

Solution 1: Using ngrok command line arguments

If you do not need to use Authentication, you can modify the ngrok command line arguments to add a host header. This can be done by adding either of the following flags to your ngrok command:

  • --host-header=rewrite
  • --host-header="localhost:9000"

The first flag tells ngrok to rewrite the host header in all requests to match the host header specified in the ngrok URL. The second flag sets the host header to a specific value, in this case “localhost:9000”.

Solution 2: Using webpack configuration

If you are using webpack to bundle your React application, you can add the following configuration to your webpack.config.js file to disable the host check:

devServer: {
    disableHostCheck: true
}

This will allow ngrok to connect to your React dev server without generating the “Invalid Host Header” error.

Note that disabling the host check may make your application more vulnerable to cross-site request forgery (CSRF) attacks. If you are using authentication in your application, you should carefully consider the security implications of disabling the host check before doing so.

Solution 3: Use the –host-header flag with ngrok

The “–host-header” flag can be used to specify the host header that ngrok should use when making requests to your local server. By setting the host header to “localhost:4200”, you can tell ngrok to make requests to your local server using the “localhost:4200” host header. This should resolve the “Invalid Host Header” error.

To use the “–host-header” flag, simply add it to your ngrok command, like this:

ngrok http https://localhost:4200 --host-header="localhost:4200"

Once you’ve done this, ngrok should be able to connect to your React dev server without any problems.

Solution 4: Set disableHostCheck to true

When utilizing “webpack devServer,” the most straightforward approach to resolve this issue is to activate the “disableHostCheck” option. By doing this, you are instructing the server to disable the default behavior of blocking requests from external sources.

To employ this solution, locate the “devServer” section within your Webpack configuration. Adjust the “disableHostCheck” property to “true,” as seen in the example below:

devServer: {
    contentBase: path.join(__dirname, './dist'),
    compress: true,
    host: 'localhost',
    port: 8080,
    disableHostCheck: true //for ngrok
}

Be mindful that you can also set the “host” property to “0.0.0.0” to allow connections from any IP address (including those originating from devices connected via ngrok). However, this may expose your application to potential security vulnerabilities, so proceed with caution.

With these modifications in place, ngrok should be able to establish a connection to your React dev server without encountering the “Invalid Host Header” error.