Reputation: 21
I ran a website with Nginx and NodeJS on Lightsail.
When I go to my mywebsite.com, my website is accessible and is secure. However, when I try to enter the public IP for that website, Nginx returns a 404 error page. The website is certified by LetsEncrypt.
I do manage to access the IP address on the browser by adding http:// before the IP address. However, when I normally go to my IP address, Nginx returns a 404 error. Why is this so?
I'm fine with the IP being not accessible. But my CDN is not working because of it. I think it needs to be accessible and not return a 404 page?
Why do I go about diagnosing and solving this problem?
Thanks in advance.
Upvotes: 1
Views: 3433
Reputation: 154995
The Host:
header identifies the hostname/domain-name used by the browser to access a website when the webserver is serving multiple websites on the same TCP binding (IP address + port number + protocol). The webserver then uses that to determine what content to serve.
Web-browsers generate the Host:
header from the URI in the address bar.
When you access http://1.2.3.4/foobar
your browser sends this request:
GET /foobar
Host: 1.2.3.4
When you access http://example.com/foobar
your browser sends this request:
GET /foobar
Host: example.com
When you access http://www.example.com/foobar
your browser sends this request:
GET /foobar
Host: www.example.com
When you access https://example.com/foobar
(or https://www.example.com/foobar
) as opposed to http://example.com
and http://www.example.com
) the HTTP request messages (i.e. the request headers) requests are the same as their http:
equivalents but the underlying HTTP connection is wrapped in a TLS (formerly SSL) connection.
In your case, you need to edit your nginx configuration to add another binding to your website that maps http://<IP-address>
to your website, as well as https://<IP-address>
. You need to check your web-application's code to ensure that it can handle requests using unexpected Host:
headers - you can also configure nginx to modify the Host
header for incoming requests to your usual header.
If you're using HTTPS then you'll need to add your IP address as a SAN (Subject Alternate Name) to your TLS certificate - which is generally a bad idea because IP addresses change and LetsEncrypt doesn't provide certificates for IP addresses anyway so this is moot.
For dev purposes and when you're running nginx locally (i.e. http://localhost
) then you no-longer need a TLS certificate for localhost
because since early 2020 Chromium-based browsers now recognize http://localhost
as being a "secure" website even though it's over http://
and not https://
:
For an immediate workaround, you can use a browser-extension to override the Host:
header when you're accessing a site by IP address.
e.g.:
Upvotes: 2