Closed
Description
At work I do all my development shelled into an AWS instance. This means my code and dev server are both remote, but my browser is local. When I try to access my react app, I get "Invalid Host Header". I think this is due to the fix for webpack/webpack-dev-server#887. From googling around, it looks like I can fix this by ejecting, but that's supposed to be for power users and the reason I'm trying create-react-app
in the first place is I don't understand anything about the ecoystem yet.
Is there anything I can do about this aside from ejecting? Is this worth fixing somehow?
Someone asked a question about this on Stack Overflow but didn't get a response.
Activity
gaearon commentedon May 19, 2017
Can you put your HOST into
.env
file?Then restart the development server.
formido commentedon May 19, 2017
That gives:
gaearon commentedon May 19, 2017
Hmm this means our port detection code couldn't connect to this host. Does the same exact host work with 0.9.x?
Timer commentedon May 19, 2017
This is the exact security flaw this change was meant to close. An outbound (non internal) IP should never be able to connect to a development server.
Changing theHOST
isn't what would make this work, we'd need to add an explicitallowedHosts
option topackage.json
which listed his public IP.Timer commentedon May 19, 2017
@formido as a temporary work around, you should be able to get your machine's public IP address and set it as theHOST
on the amazon server; but you'll also need to set a firewall rule (on the ec2 instance) which lets you bind to that address:edit: don't mind me, idk where my head was -- I forgot you'd be visiting with the machine's url
formido commentedon May 20, 2017
Ok, adding
HOST
to the.env
file worked. The previous error happened because I had forgotten I was running the app in docker.In principle, there's no reason why a development server should not be accessible from an external IP. It depends on the details, e.g., limit access with an AWS security group. For me, it's extremely convenient.
gaearon commentedon May 20, 2017
We'll err on the side of safety but it would be nice if we could serve a more useful page explaining the reason for this limitation and workarounds.
0xjjpa commentedon May 20, 2017
Having the same issue, particularly when trying to work with c9.io
edit: Solved it by adding
0.0.0.0 <domain>
in my/etc/hosts
in addition to adding the domain to the.env
file. Also, as a reference, we might want to inject thepublic
option somewhere in case for whatever reason we want to serve the dev env to a public domain (thinking for example live coding).gaearon commentedon May 20, 2017
I have a proposed solution here: #2288.
What do you think?
gaearon commentedon May 20, 2017
Please update to
react-scripts@1.0.2
.It fixes this issue for users who don't use the
proxy
feature.If you do use the
proxy
feature, please follow these instructions.formido commentedon May 20, 2017
Awesome, great! 👍
rttomlinson commentedon May 22, 2017
@jjperezaguinaga Do you have a small write-up on how to configure c9 and create-react-app to get the dev server to work? My apologies if this isn't the right place to post. Thanks
7 remaining items
lcoder commentedon Nov 23, 2017
I have created my project a long time ago.I configure
package.josn
:"start": "HOST=rc.baidu.com node scripts/start.js",
. And it doesn't work.99sono commentedon Dec 3, 2017
NOTE:
This issue is related to discussion:
angular/angular-cli#6070
On the above link the discussion is clearer and more profound.
As a last resort fix, you can analyse and modify the code line:
QUOTE
Until this gets resolved you can just change the following line in node_modules/webpack-dev-server/lib/Server.js (line 402):
if(this.disableHostCheck) return true;
An additional point is that the message returned by the node js server:
"Invalid Host header"
Is not very useful unless you are running wiresehark.
At minimum, one wants to return to the user something like:
hostname being rejeted is: docker.for.mac.localhost
And this validation is indeed a pain in the ... Considering micro-service-like applications, where to avoid cross origin problems, one connects an apache httpd server that routes to backend services, hostname re-writing is only to be expected. IN the case of docker development, having something like the above hostname that allows to connect to the host os is quite needed.
rayzor65 commentedon Jan 8, 2018
@formido you mentioned you ran into an error due to running the app in docker, I believe this is currently happening to me, may I ask what was the issue/solution?
willian commentedon Jan 8, 2018
@rayzor65 I'm running an app bootstrapped with CRA on docker using docker-compose.
What is the problem you're having?
Here is my
Dockerfile
:And my
docker-compose.yml
:Those files are in the root of my app file structure.
formido commentedon Jan 11, 2018
@rayzor65 IIRC, I was putting the AWS host in the
.env
but it needed to be the docker host.Sorry for the late response, I guess listening on all the notifications on this project can make @s get lost in the noise.
brian-lamb-software-engineer commentedon Feb 27, 2018
Not sure how this is relates, but I got this same error when specifying
host: '0.0.0.0',
(not on create-react-app but on another react app) changing to the actual hostname fixed it for me. This goes in your webpack config for e.g. hostname ofyourhostname.pvt
devssh commentedon May 3, 2018
Fixing the host header in the .env file led to a no-referrer-when-downgrade and slightly different CSS.
Finally after ejecting I had to rewrite my own webpack config to get rid of these security policies for development and modify my server to get rid of cors.
flooyd commentedon Jun 5, 2018
I am on c9, using react-scripts 1.1.4. I have tried everything suggested here, yet I still get Invalid Host Header. Anyone else having this issue still?
bugzpodder commentedon Jun 5, 2018
@flooyd please open a new issue and tell us what didn't work and what error you saw (fill in the issue template). Thank you!
npm start
failed afternpm run eject
and (Invalid Host header) timarney/react-app-rewired#261xhalo32 commentedon Jul 5, 2018
I had the same problem, but my local
/etc/hosts
was to blame 🤦♂️.adamewhite commentedon Jul 17, 2018
Was this ever solved conclusively? I'm still having the same problem after including an .env file with the HOST variable. I'm not sure what else to do.
This project was bootstrapped with [Create React App](https://github.…