39
> host example.com
example.com has address 93.184.216.34
example.com has IPv6 address 2606:2800:220:1:248:1893:25c8:1946

I type 93.184.216.34 instead of http://example.com in Chrome. It doesn't load the website. Why?

MechMK1
  • 306
  • 1
  • 9
PerrierCitror
  • 525
  • 1
  • 4
  • 5
  • As a rule of thumb, humans should never use IP addresses, unless configuring an actual server. – David Richerby Dec 03 '18 at 10:37
  • Worth emphasizing that you *did* get a response from the server: a 404 HTTP response. This means that it successfully found the host (the computer on the other side) and some web server (I'd guess something open source running on Linux, like Nginx) running on that host sent back data. – jpmc26 Dec 04 '18 at 23:14

9 Answers9

110

Because the proper HTTP Host header is often required to actually get the intended site.

It's very common to host multiple web sites on the same IP address and distinguish between them based on the HTTP Host header specified by the client (as well as the TLS SNI value nowadays in the case of HTTPS).

That is, when you entered http://example.com into your browser the Host header was example.com, but that is not the case when you entered 93.184.216.34. You reach the same web server in both cases, but you receive different responses (in this particular case 200 vs. 404).

Peter Mortensen
  • 2,319
  • 5
  • 23
  • 24
Håkan Lindqvist
  • 33,741
  • 5
  • 65
  • 90
  • 4
    Could someone expand a little on what `Host` does and what web servers generally use it for? It's kind of hard to search for online because that word is so overloaded. – user1717828 Dec 01 '18 at 20:24
  • 1
    https://tools.ietf.org/html/rfc7230#section-5.4 A http server processes a Host header however it wants to. httpd for example has VirtualHost configuration directives. – John Mahowald Dec 01 '18 at 20:36
  • @user1717828 The main use-case is what I describe in the answer. – Håkan Lindqvist Dec 01 '18 at 20:41
  • @user1717828 Simply open a book about how HTTP works; this will be explained in the first chapters. – Lightness Races in Orbit Dec 01 '18 at 20:47
  • So in theory, would it be possible to request IP address and maybe add HTTP header to the request with something like `wget` or Python's urllib ? I know, sounds a bit overcomplicated. I've just ran into cases where WiFi access point on my university campus would give DHCP lease with non-working DNS server, so I couldn't request a domain name like `example.com` ( and yes, practicall way would be to just edit machine's DNS settings ). I'm just curious what's possible – Sergiy Kolodyazhnyy Dec 01 '18 at 22:02
  • 19
    @SergiyKolodyazhnyy Sure. You could `curl -H "Host: example.com" http://93.184.216.34/` or something like that. – Håkan Lindqvist Dec 01 '18 at 22:16
  • 13
    To paraphrase - "there is not a one-to-one relationship between URLs for websites and IP address." – Pete Dec 02 '18 at 07:20
  • 1
    In fact that particular server (93.184.216.34 2606:2800:220:1:248:1893:25c8:1946) handles _at least_ `example.com example.net example.org example.edu` on the same address(es), which is why it really needs the name in the Host header. – dave_thompson_085 Dec 02 '18 at 14:58
  • It's also important for SEO to only be available at the canonical domain (and protocol). It's therefore generally best to whitelist the host names that are supported. – usr Dec 02 '18 at 18:06
  • 1
    @user1717828 - the term to search for is "virtualhost" - of course, this requires you to know what the host header does in the first place – slebetman Dec 03 '18 at 02:53
  • 1
    Consider mentioning also that when dealing with HTTPS this is complicated even more in that the cert is usually not valid for the IP address (even if it does return a valid site with no `Host`) causing a certificate error. – Vidia Dec 03 '18 at 23:12
14

Because usually web servers use "virtual server" technology and are able to answer on your HTTP request within exactly the domain name you request, but not the IP address of the web servers. Thanks to hiding more than one domain name on one IP address.

For example, the Apache web server is able to respond to your HTTP request with an IP address using the section:

<VirtualHost *:80>
ServerName Default
...
</VirtualHost>

or if No VirtualHost used in configuration at all.

The VirtualHost feature in Apache was introduced in 1996.

Peter Mortensen
  • 2,319
  • 5
  • 23
  • 24
10

In Apache, you can host many websites using just one single IP address. This is called virtual hosting. It's how subdomains can be created, even standalone domains. This is done by setting up an Apache configuration file containing VirtualHost directives for each domain/subdomain.

An example HTTP server that has two virtual hosts, example1.com and example2.com can look like this (IP address definition):

<VirtualHost 93.184.216.34:80>
  ServerName example1.com
  ServerAlias www.example1.com
  DocumentRoot /var/www/example1.com
</VirtualHost>

<VirtualHost 93.184.216.34:80>
  ServerName example2.com
  ServerAlias www.example2.com
  DocumentRoot /var/www/example2.com
</VirtualHost>

It can also look like this (name-based definition):

<VirtualHost *:80>
  ServerName example1.com
  ServerAlias www.example1.com
  DocumentRoot /var/www/example1.com
</VirtualHost>

<VirtualHost *:80>
  ServerName example2.com
  ServerAlias www.example2.com
  DocumentRoot /var/www/example2.com
</VirtualHost>

In both cases, two virtual host records are created internally in memory and used by Apache to compare against when a URI request arrives.

When a user types in the IP address via a user agent, the first virtual host listed in the configuration file is used as the primary domain (i.e. in this case example1.com).

When a user types in a domain name, the request is sent to a public Internet DNS network (ICANN) which provides the IP address associated with it. You registered both via an ICANN registrar (like GoDaddy). You must have both of these correct and give some time before propagation takes hold to all DNS servers on the ICANN network. These days it can take up to 24 hours.

When the request is routed to your Apache HTTP server, the IP address and domain name are matched against the list of internal VirtualHost records. When one is found, the document root is used to form the full filesystem path to the object resource to return back to the user agent. If not, a HTTP 404 is sent along with any error document associated with it.

Peter Mortensen
  • 2,319
  • 5
  • 23
  • 24
Kerry Kobashi
  • 219
  • 1
  • 4
8

I like to use the "house" terminology.

You can quite easily send a letter to a house without a name on it and it'll arrive at the house.

If you put the person's name on it then you're sending to the intended recipient.

The destination is the same but how it is handled when it reaches the house is different.

When you specify the site, i.e. www.example.com then the server knows how to handle the request and which host it is intended for and which site to serve back.

Chris Lomax
  • 173
  • 1
  • 2
  • 8
  • This is a helpful metaphor! – Radvylf Programs Dec 04 '18 at 14:35
  • +1 I like the idea of the recipient name. I was thinking of a similar analogy but with IP Address identifying the physical building (computer) but the apartment number (domain name) identifying the actual recipient in multi-tenanted buildings. Of course, this is an over simplification and multi-tenanted computers could have the IP only address server a default site, but I think this is a good analogy. – jmbertucci Dec 05 '18 at 15:49
0

The key term to search for is "name based virtual hosting".

People want to allocate multiple hostnames to the same web server and serve different content for each hostname. This is known as virtual hosting (not to be confused with the more recent concept of virtual machines).

Initially virtual hosting was done by allocating multiple IP addresses to the server, the sever could then send different content based on the IP address used, but this was seen as wasteful.

Therefore the "host" header was introduced, initially as an extension but then later made a mandatory part of the http 1.1 specification in 1997. This header specifies the hostname the client asked for. The server can then serve up different content based on the value of the header.

Peter Green
  • 4,056
  • 10
  • 29
  • "but this was seen as wasteful. " Only for legacy v4 connections. Its now quite possible to set up every virtual host on a different v6 address. – Qwertie Dec 04 '18 at 04:01
  • 1
    We are still a long way off from the point where making your public-facing services IPv6 only is a reasonable thing to do. – Peter Green Dec 04 '18 at 06:06
0

To keep costs down for web servers, a lot of web servers host multiple websites. They do this by using virtual hosts, or Vhosts, in apache2/nginx/etc. So if you go directly to the website's IP Address, you will most likely get an "Apache is working" screen, or possibly even get redirected to the web server's main website.

A Vhost looks at the incoming website address and compares it to the ServerName or ServerAlias names in the enabled Vhosts files. If one of them matches, that specific website is loaded.

Unless the website has a massive load (high numbers of unique visitors/page views) or drives high load applications (think youtube.com, facebook, etc.) it is probably more cost effective to operate on a shared server. It would be a waste of money to get yourself a dedicated server (starting at $60/mo) just to run a Wordpress blog website. You're better off getting a shared platform on a server with probably 200+ websites on one server. Costs will be more in the area of $5/mo.

Another reason for doing this is the lack of IP addresses. There simply isn't enough IPv4 addresses remaining. It is only through the use of NAT for home/business networks and the use of Vhosts that we have any remaining at all. Even when IPv6 becomes main stream, servers will probably stick to Vhosts (server costs).

0

A dedicated IP address is expensive, while creating a new website on a server is basically free.

What happens is that the hosting company rents a single IP address that points to a physical server, then hosts thousands of websites on that IP address using the "virtual host" feature

Think like a P.O. Box, if you just write down the post office address but without the box number, the mail won't be delivered.

Magnetic_dud
  • 1,034
  • 2
  • 15
  • 28
0

There's a lot of answers here with technical detail, but I think the simplest high-level explanation is that even if a web server is properly listening for http traffic on it's IP address, the server must usually also be configured to answer for a particular domain name, and that name must be in the request sent by the client (i.e the web browser)

I say "usually" because it's almost always done this way, but there are in fact methods where you can setup the http server to answer if only the IP address is used.

-1

We need to understand the differences between Virtual IPs and Dedicated IPs.

If a website has a dedicated (not shared) IP, then (for example) http://123.456.789.012 will bring up the website.

Try this, which is the Dedicated IP address of a site that I own, www.negativeiongenerators.com: http://75.126.128.174 But as others have said, it's usually not a good idea.

Mike Waters
  • 137
  • 1
  • 5
  • 1
    This is not universally true. It depends on the web server configuration. You can have a dedicated IP address and still not respond to the IP without the host and you can also have a shared ip address and have the IP address point to one of the websites on the server. There is also nothing wrong with allowing access to a website via its ip address alone although not particularly useful either. – Qwertie Dec 04 '18 at 04:02