1. 程式人生 > 實用技巧 >每個Web伺服器僅容納一個網站嗎?

每個Web伺服器僅容納一個網站嗎?

do-web-servers-only-hold-one-website-each-00

When you first start learning how domain names, IP addresses, web servers, and websites all fit and work together, it can be a little confusing or overwhelming at times. How is it all set up to work so smoothly? Today’s SuperUser Q&A post has the answers to a curious reader’s questions.

當您第一次開始學習域名,IP地址,Web伺服器和網站如何相互配合並協同工作時,有時可能會有些混亂或不知所措。 如何順利地運作? 今天的“超級使用者問答”帖子回答了好奇的讀者的問題。

Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.

今天的“問答”環節由SuperUser提供,它是Stack Exchange的一個分支,該社群是由社群驅動的Q&A網站分組。

Photo courtesy of Rosmarie Voegtli (Flickr).

圖片由Rosmarie Voegtli(Flickr)提供

問題 (
The Question)

SuperUser reader user3407319 wants to know if web servers only hold one website each:

超級使用者閱讀器user3407319想知道每個Web伺服器是否僅擁有一個網站:

Based on what I understand about DNS and linking a domain name with the IP address of the web server a website is stored on, does that mean each web server can only hold one website? If web servers do hold more than one website, then how does it all get resolved so that I can access the website I want without any problems or mix ups?

根據我對DNS的瞭解,並將域名與儲存網站的Web伺服器的IP地址連結起來,是否意味著每個Web伺服器只能容納一個網站? 如果網路伺服器確實擁有多個網站,那麼如何解決所有問題,以便我可以訪問我想要的網站而不會出現任何問題或混亂?

Do web servers only hold one website each, or do they hold more?

Web伺服器是每個伺服器僅擁有一個網站,還是擁有更多網站?

答案 (The Answer)

SuperUser contributor Bob has the answer for us:

超級使用者貢獻者Bob為我們提供了答案:

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

基本上,瀏覽器在HTTP請求中包含域名,因此Web伺服器知道請求了哪個域並可以做出相應的響應。

HTTP Requests

HTTP請求

Here is how your typical HTTP request happens:

這是典型的HTTP請求的發生方式:

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

1.使用者提供URL,格式為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 operating systems bypasses DNS).

2.瀏覽器在稱為名稱解析的過程中提取URL的主機(域)部分,並將其轉換為IP地址(如有必要)。 這種轉換可以通過DNS進行,但不是必須的(例如,常見作業系統上的本地主機檔案會繞過DNS)。

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

3.瀏覽器開啟到指定埠的TCP連線,或預設為該IP地址上的埠80。

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

4.瀏覽器傳送一個HTTP請求。 對於HTTP / 1.1,它看起來像這樣:

do-web-servers-only-hold-one-website-each-01

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.

主機標頭是標準的,在HTTP / 1.1中是必需的。 HTTP / 1.0規範中未指定該功能,但是某些伺服器仍然支援該功能。

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

從這裡開始,Web伺服器具有幾條資訊,可用於確定響應的內容。 請注意,單個Web伺服器可能繫結到多個IP地址。

  • The requested IP address, from the TCP socket (the IP address of the client is also available, but this is rarely used, and sometimes for blocking/filtering)

    從TCP套接字請求的IP地址(客戶端的IP地址也可用,但是很少使用,有時用於阻止/過濾)
  • The requested port, from the TCP socket

    從TCP套接字請求的埠
  • The requested host name, as specified in the host header by the browser in the HTTP request

    請求的主機名,由瀏覽器在HTTP請求中的主機頭中指定
  • The requested path

    請求的路徑
  • Any other headers (cookies, etc.)

    任何其他標題(Cookie等)

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 the host to differentiate between websites.

正如您似乎已經注意到的那樣,如今最常見的共享主機設定將多個網站置於一個IP地址:埠組合中,僅由主機來區分網站。

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.

這在Apache領域被稱為基於名稱的虛擬主機,而Nginx在伺服器塊中將其稱為伺服器名稱,而IIS則更喜歡Virtual Server

What About HTTPS?

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.

HTTPS有所不同。 直到建立TCP連線為止,所有內容都是相同的,但此後必須建立加密的TLS隧道。 目的是不洩漏有關請求的任何資訊。

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

為了驗證Web伺服器實際上擁有該域,Web伺服器必須傳送由受信任的第三方簽名的證書。 然後,瀏覽器將將此證書與其請求的域進行比較。

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

這帶來了問題。 如果需要在接收HTTP請求之前執行此操作,Web伺服器如何知道要傳送哪個主機/網站的證書?

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

傳統上,這是通過為每個需要HTTPS的網站設定專用IP地址(或埠)來解決的。 顯然,隨著我們用盡IPv4地址,這已經成為問題。

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

輸入SNI (伺服器名稱指示)。 現在,瀏覽器在TLS協商期間傳遞了主機名,因此Web伺服器足夠早地擁有此資訊以傳送正確的證書。 在Web伺服器端,配置與HTTP虛擬主機的配置非常相似。

The downside is the host name is now passed as plain text before encryption, and is essentially leaked information. This is usually considered an acceptable trade-off though considering the host name is normally exposed in a DNS query anyway.

缺點是主機名現在在加密之前以純文字形式傳遞,並且實質上是洩漏的資訊。 儘管考慮到主機名通常還是會在DNS查詢中公開,但這通常被認為是可以接受的折衷方案。

What If You Request a Website by IP Address Only?

如果僅通過IP地址請求網站怎麼辦?

What the web server does when it does not know which specific host you requested depends on the web server’s implementation and configuration. Typically, there is a “default”, “catch-all”, or “fall back” website specified that will provide responses to all requests that do not explicitly specify a host.

Web伺服器在不知道您請求的特定主機時會做什麼,取決於Web伺服器的實現和配置。 通常,會指定一個“預設”,“全部接收”或“後備”網站,該網站將提供對未明確指定主機的所有請求的響應。

This default website can be its own independent website (often showing an error message), or it could be any of the other websites on the web server depending on the preferences of the web server admin.

該預設網站可以是其自己的獨立網站(通常顯示錯誤訊息),也可以是Web伺服器上的任何其他網站,具體取決於Web伺服器管理員的偏好。



Have something to add to the explanation? Sound off in the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here.

有什麼補充說明嗎? 在評論中聽起來不錯。 是否想從其他精通Stack Exchange的使用者那裡獲得更多答案? 在此處檢視完整的討論執行緒

翻譯自: https://www.howtogeek.com/260675/do-web-servers-only-hold-one-website-each/