解決127.0.0.1報400而localhost可用的問題

原文鏈接:https://stackoverflow.com/questions/25652409/localhost-is-working-but-not-127-0-0-1-in-windows-7

So typically an HTTP 400 Invalid Hostname error usually means you do not have the website set accept all hostnames and/or IP addresses. I presume because this is a C# application you are hosting this on IIS. To fix this open up IIS Manager (Win+R and enter inetmgr), expand the server then Sites, then right-click on the the website where your application is hosted and select bindings. In this list there should be an http binding for port 54408, double-click on this. Under IP address make sure All Unassigned is selected, and under Host name make sure the field is blank. Hit OK, then Close. The setting should take effect immediately without needing to reset IIS.

If you are only testing this through Visual Studio's built in web deployment, there are similar settings to the above somewhere in VS (I'm a little rusty on it so I can't remember exactly where or how). Alternatively you can set up a web site in IIS exactly how you want it and then have VS deploy to that website instead of using its own internal server. I think there might be some debugging drawbacks to doing it this way though (once again I'm a little fuzzy on details, I'll edit them in when I remember them or figure it out).

A little background as to why these settings exist: Sometimes a server needs to host multiple sites that are all to be accessed via port 80. So lets say we have foo.com and bar.com and they are too small to warrant getting a separate server for both of them. So instead they are both hosted on a sever with an IP address of 1.2.3.4. Now when you enter the URL foo.com into the browser and hit go, it first resolves the host name to 1.2.3.4, and then it creates a request and part of that request is called the host header. The host header is filled with the hostname of the URL entered, in this case foo.com. When the request is received by the server, it looks at the host header and serves back the content for foo.com.

Now if you were to try and enter 1.2.3.4 into the browser, it would create a request with a blank host header, because none was specified. When the request is received by the server, it doesn't know what to do because there are two websites being hosted by the server and there is no host header to specify which one the browser is looking for, so instead it returns an error.

This is probably what is happening in your situation. Your website is being hosted under the localhost hostname and any other request isn't being responded to. The settings I specified to change are basically telling the server that no matter what IP address (network interface) it comes in on, and no matter what the hostname it is looking for, as long as it is coming in on port 54408, serve this website.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章