80

As from what I understand the DNS link the domain name with the IP address of the server the website is stored on, does that mean each server can only hold one website? If they don't, how does calling the server's IP address know which website I want if there are many on the same server?

  • 13
    Wikipedia has a good introduction to Shared Web Hosting. If you enter http://<IP_ADDR>/ in your browser, the HTTP request won't have a domain within the Host: header. In case of shared hosting, the web server can be configured by the provider to handle this in different ways (e.g. have a default, redirect to provider etc.). – Jedi Jun 18 '16 at 3:30
  • I have clicked links that break with messages like "this server has never/currently does not host the website you are looking for". – Jesvin Jose Jun 18 '16 at 17:52
  • 1
    In case you're looking far a way to run multiple applications on a single server-- say you have two apps MyApp and YourApp on ports 8001 and 8002 respectively. You can have two load balancers or application proxies at: myapp.com and yourapp.com. Have them receive requests at the default ports (80/443) and forward it to the actual server(s) at ports 8001 and 8002 respectively. – rohithpr Jun 18 '16 at 19:30
  • 6
    Great question. Every website used to need its own IP address (a server can have more than one IP address). The Host header in HTTP/1.1 was introduced to get around the exact problem you describe. See "Internet address conservation" in www8.org/w8-papers/5c-protocols/key/key.html – A E Jun 18 '16 at 22:15
  • 6
    If http 1.1 didn't have the host header, ipv6 would be implemented by now ;-) :-( – Lenne Jun 20 '16 at 8:43
149

Basically: the browser includes the domain name in the HTTP request, so the webserver knows which domain was requested and can respond accordingly.


HTTP requests

Here's how your typical HTTP request happens:

  1. The user provides a URL, in the form http://host:port/path.

  2. The browser extracts the host (domain) part of the URL and translates it into an IP address if necessary, in a process known as name resolution. This translation can occur via DNS, but it does not have to (for example, the local hosts file on common OSes bypasses DNS).

  3. The browser opens a TCP connection to the specified port, or defaults to port 80, on that IP address.

  4. The browser sends an HTTP request. For HTTP/1.1, it looks like this:

    GET /path HTTP/1.1
    Host: example.com
    

    (The Host header is standard and required in HTTP/1.1. It was not specified in the HTTP/1.0 spec, but some servers support it anyway.)

From here, the webserver has several pieces of information it can use to decide what the response should be. Note that it is possible for a single webserver to be bound to multiple IP addresses.

  • The requested IP address, from the TCP socket
    • The IP address of the client is also available, but this is rarely used - sometimes for blocking/filtering
  • The requested port, from the TCP socket
  • The requested hostname, as specified in the Host header by the browser in the HTTP request.
  • The requested path
  • Any other headers (cookies, etc.)

As you seem to have noticed, the most common shared hosting setup these days puts multiple websites on a single IP address:port combination, leaving just Host to differentiate between websites.

This is known as a Name-based Virtual Host in Apache-land, while Nginx calls them Server Names in Server Blocks and IIS prefers Virtual Server.


What about HTTPS?

HTTPS is a bit different. Everything is identical up to the establishment of the TCP connection, but after that an encrypted TLS tunnel must be established. The goal is to not leak any information about the request.

In order to verify that the server actually owns this domain, the server must send a certificate signed by a trusted third party. The browser will then compare this certificate with the domain it requested.

This presents a problem. How does the server know which host (website)'s certificate to send, if it needs to do this before the HTTP request is received?

Traditionally, this was solved by having a dedicated IP address (or port) for every website requiring HTTPS. Obviously, this becomes problematic as we start running out of IPv4 addresses.

Enter SNI (Server Name Indication). The browser now passes the hostname during the TLS negotiations, so the server has this info early enough to send the correct certificate. On the server side, configuration is very similar to how HTTP virtual hosts are configured.

The downside is the hostname is now passed as plain text before encryption, and is essentially leaked information. This is usually considered an acceptable tradeoff, considering the hostname is normally exposed in a DNS query anyway.


What if you request a site by IP address only?

What the server does when it does not know which specific host you requested depends on the server implementation and configuration. Typically, there is a "default", "catchall" or "fallback" site specified that will provide responses to all requests that do not explicitly specify a host.

This default site can be its own independent site (often showing an error message), or it could be any of the other sites on the server, depending on the preference of the server admin.

  • 1
    Also, one site may be split across many servers, as is the case with load balancers, like Heroku and Amazon uses. – phyrfox Jun 21 '16 at 2:25
  • 1
    @phyrfox Yea, I thought about adding that, but it's only tangentially related to the question and I didn't want to make the answer too long. Might still end up adding a section for it later. – Bob Jun 21 '16 at 2:41
  • Legend has it that subdomains point so specific computers inside a network. In theory – Loupax Jun 21 '16 at 8:40
  • "Traditionally, this was solved by having a dedicated IP address (or port) for every website requiring HTTPS. Obviously, this becomes problematic as we start running out of IPv4 addresses.", So without that invension, we all would have ipv6 by now. – Lenne Jun 23 '16 at 10:18
92

I have this explanation for non-tech people.

Jack, Jill and Joe live at a dormitory, and they don't have cellphones.

In the phonebook, they are all listed with the same number. (A-record)

You dial the number, and somebody picks up the phone; you say "I'd like to speak to Jill", and you get her on the line.

Instead of an A-record (A phonenumber/IP-adress) in the phonebook, it may just say "Dormitory X", then you must look further for the number for Dormitory X. This is a CNAME record.

If Jill is not available, you might get

  • 404 Jill is not here
  • 410 Jill is dead.
  • 301 Jill is moved in with Peter
  • 302 Jill is visiting Peter, call him instead

  • 400 I can't understand you.

  • 401 Who are you? What is the password? or We don't allow male callers after 10pm
  • 402 Payment Required (Are you sure Jill is her real name ;-) )
  • 403 No, that is not the right password.
  • 418 Jill is a teapot :-)
  • 429 Jill can't take any more calls.
  • 451 You are violating your restraining order.

  • 500 Our phone system has broken down.

6

As from what I understand the DNS link the domain name with the IP address of the server the website is stored on, does that mean each server can only hold one website?

First, you need to understand that there are a number of distinct concepts here.

  • Web site, a group of web pages that form a coherent whole.
  • IP address, a numerical address (32-bit for IPv4, 128 bit for IPv6) used by the internet protocol as the source or destination for traffic.
  • Server, a machine whose job is to serve requests from clients.
  • Hostname, a name used to identify a machine in DNS (e.g. "www.example.com" or "en.wikipedia.org")

There is not a one-to-one relationship between any of these things. One server can have multiple IP addresses; multiple hostnames can point at one IP address; one host name can point at multiple IP addresses. Multiple websites can be under the same hostname. One website can be spread across multiple hostnames.

If they don't, how does calling the server's IP address know which website I want if there are many on the same server?

In the old days (HTTP 1.0 and before) each hostname that the server wanted to handle differently had to have its own IP address. This was rather wasteful.

HTTP 1.1 added the Host"header as a mandatory field in the HTTP request (IIRC some vendors had previously supported this as an extension). This told the server which hostname had been requested and hence allowed it to serve different content for different hostnames on the same IP address. Support for HTTP 1.1 in clients is now ubiquitous.

Unfortunately, SSL (later TLS) added a wrinkle. Establishing a SSL/TLS session requires the server to present a certificate to the client that covers the requested hostname, but the HTTP request doesn't arrive until after the SSL/TLS session is established.

It is possible to have one certificate cover multiple hostnames through the use of the SubjectAltName field or the use of wildcards in the CommonName field. However, this poses administrative challenges, especially if the hostnames involved are under domains with different ownership.

So TLS introduced the "server name indication" (SNI) extension. With this extension, the client sends the requested hostname to the server during the TLS handshake procedure. The server can then present the appropriate certificate. Unfortunately, while current versions of all major SSL/TLS implementations support SNI, it has taken a long time for older versions to fall out of use.

  • You forgot to mention that TCP can listen on many ports, running different servers on each... – Toby Speight Jun 22 '16 at 8:09
  • Yes, but you don't have portnumbers in dns, and you can't expect joe.p.user to go to our.fabulous.site:81 Besides, some firewalls block outgoing access to non-standard portnumbers. – Lenne Jun 23 '16 at 10:15
3

The answer is a bit more complicated than some of the answers have made it out to be. When you perform a DNS lookup you MUST get an IP address (A record for IPv4, AAAA for IPv6). You have to be able to open a socket over TCP/IP to communicate or the whole thing fails. That address may represent a server or it might represent a load balancer. It could even represent a proxy. If the host is behind CloudFlare, for instance, the address you get is of a CloudFlare server. The real server is somewhere else. This lets the host avoid problems like Denial-of-Service attacks.

Virtual hosting is what you're asking about (some of the other questions touched on this, but not in any detail). Virtual hosting takes the web request and looks at the host name (i.e. domain.com) to determine which website to serve up. So in the Apache HTTP web server you would have a configuration like this

<VirtualHost *:80>
    ServerName www.domain.com
    ServerAlias domain.com

    DocumentRoot /var/www/domain.com
</virtualHost>

This is simplified for example. So we're telling Apache to listen on port 80 of any IP (in modern virtual machine hosting your machine's IP may be different from its live IP). We then tell it that this is the domain.com website and what directory that website lives under. We can then repeat this block over and over to tell Apache to handle different websites. Every web server supports this type of system.

Another way to handle this would be to tell the web server to direct all web traffic to one single programming script (i.e. PHP, ASP.NET, etc) and then that single script will determine what website and page to display.

1

Using DNS you can assign as many names to an individual IP address as you want (in your hosts file you may simply separate each name with spaces, for example). Using a DNS server you may also assign multiple IP addresses to a single name. It is not limited to a one-to-one relationship.

A web server knows which site to serve by examining the requested URL. It looks at which domain was requested, the port which was requested and what what protocol was used. This has nothing to do with DNS and is handled by the HTTP protocol.

0

Web server has the concept of the host container (here is the documentation for Tomcat, for instance). Multiple host containers can be configured for the same box/ip address, servicing multiple domains. Containers have independent working directories, authentication realms, log directories and things the like.

The server finds the relevant container for the new request buy the name of the domain is part of this HTTP request.

Completely different web server instances can share the same IP address if they run on different ports. This is mostly used in various development and testing environments where domain names may not be available, as production server cannot run on arbitrary port.

Finally, even if the strictly unique IP address is required for a website, a server box often has multiple network adapters so is configured to use multiple IP addresses.

0

Your server IP address can hold many different domain names at the same time.

When you access the website your browser send the HTTP request with domain name in it, and the server can found which website data it should send you back then.

It's called the virtual hosts, so simple as that :)

Take a look here for more information about DNS and virtual hosts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.