B
B
by_EL2021-09-26 11:16:41
Apache HTTP Server
by_EL, 2021-09-26 11:16:41

How will two sites work on one ip and on one port 80?

I don’t understand how two sites can work on one ip and on one port? Please tell me how can this be done?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Sokolov, 2021-09-26
@by_EL

The browser, when requesting a site, sends request headers . Among others, the header Host , in which the domain of the requested site.
The web server, receiving a request, sees this header and decides what content to give - a site with the appropriate domain, or, for example, a default stub.
How, simplified, the browser dialogue with the server looks like:

// юзер вводит адрес http://qna.habr.com/
// браузер ресолвит это имя в IP адрес и подключается к нему по порту 80
// браузер шлёт запрос:
GET / HTTP/1.1
Host: qna.habr.com
Accept-Language: ru

// сервер, видя такое дело, соображает, что речь про сайт qna
// и что надо отдать корневую страницу
// отдаёт контент QnA

A request to the same port on the same IP for, say, a website freelance.habr.comwould look like:
// юзер вводит адрес http://freelance.habr.com/
// браузер ресолвит это имя в (тот же) IP адрес и подключается к нему по порту 80
// браузер шлёт запрос:
GET / HTTP/1.1
Host: freelance.habr.com
Accept-Language: ru

// сервер понимает, что речь про сайт freelance

G
galaxy, 2021-09-26
@galaxy

I will add to the answer Sergey Sokolov that since the time of the ancient Apache (Apache web server), the technology for implementing this has traditionally been called Virtual Hosts . In nginx, for example, there is no such special concept or directive, just the required domain is indicated in the server_name blocks in the server blocks .
The web server really separates requests to different sites from each other by the Host header.
Well, just in case: with https, everything is a little more complicated.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question