Report this

What is the reason for this report?

How to fix "Invalid Host Header" for a React component

Posted on July 10, 2023

I had my first static site deployed at sea-lion-app-dpidq.ondigitalocean.app . Then I added a React webapp component (after following this tutorial: https://react.dev/learn/tutorial-tic-tac-toe) in the same project. DigitalOcan gave me a new HTTP route to sea-lion-app-dpidq.ondigitalocean.app/forest-visualizer but when I try to access it I get the error “Invalid Host Header”.

When deploying the React component, I did not specify any HTTP requests in anyway as I was crueless how to manually configure it. Many StackExchange websites (ex. https://stackoverflow.com/questions/43619644/i-am-getting-an-invalid-host-header-message-when-connecting-to-webpack-dev-ser) say to fix disableHostCheck, but I do not have any file that specifies the script.

Does anyone know how to fix the issue? Please also let me know if this is not enough information to debug.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hey Yutaro 👋,

If you’re using webpack-dev-server locally for development, you need to configure it to allow requests from your DigitalOcean subdomain by setting allowedHosts in the Webpack configuration file.

Create or update your webpack.config.js (if it doesn’t already exist) and add the following under the devServer configuration:

module.exports = {
  // ...other webpack config
  devServer: {
    allowedHosts: ['sea-lion-app-dpidq.ondigitalocean.app'],
  },
};

This tells the dev server to accept requests from your DigitalOcean domain.

If you’re using Create React App (CRA), you may not have direct access to the webpack.config.js file. In that case, you can set the HOST environment variable to match your domain:

  • Add a .env file in the root of your project (if it doesn’t exist) and include the following:
    HOST=sea-lion-app-dpidq.ondigitalocean.app
    

Then try again.

Since you’re deploying the app on DigitalOcean, the error could also appear if you’re serving the app with Webpack’s dev server instead of a production server like Nginx. Make sure you’re using npm run build to create a production build, and then deploy the static files to DigitalOcean’s App Platform or Spaces.

  • If using DigitalOcean App Platform, check that your app is built for production using:
    npm run build
    

Then deploy the generated build folder instead of running it with Webpack’s development server.

That is the recommended way to serve a React app in production rather than using the development server.

As a last resort, you can disable the host check. However, this is not recommended for production environments as it weakens security. To do this with Create React App:

  • Add the following to your .env file:
    DANGEROUSLY_DISABLE_HOST_CHECK=true
    

\— Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.