0
0
Nginxdevops~3 mins

Why virtual hosting serves multiple domains in Nginx - The Real Reasons

Choose your learning style9 modes available
The Big Idea

What if one server could magically become many, each showing a different website?

The Scenario

Imagine you have a single server and want to show different websites to visitors depending on the web address they type, like example.com or myblog.com.

Without virtual hosting, you'd need a separate server or IP address for each website.

The Problem

Manually setting up one server per website is expensive and wastes resources.

It also makes managing many sites slow and confusing because you juggle multiple servers.

The Solution

Virtual hosting lets one server handle many websites by checking the web address visitors use.

This way, the server sends the right website content without needing extra machines.

Before vs After
Before
server {
  listen 192.168.1.1:80;
  server_name example.com;
  root /var/www/example;
}

server {
  listen 192.168.1.2:80;
  server_name myblog.com;
  root /var/www/myblog;
}
After
server {
  listen 80;
  server_name example.com myblog.com;
  root /var/www/sites;

  location / {
    if ($host = 'example.com') {
      root /var/www/example;
    }
    if ($host = 'myblog.com') {
      root /var/www/myblog;
    }
  }
}
What It Enables

Virtual hosting makes it easy and cheap to run many websites on one server, saving money and time.

Real Life Example

A small business can host its main site, blog, and shop all on one server using virtual hosting, instead of paying for three separate servers.

Key Takeaways

Manual hosting needs one server per website, which is costly.

Virtual hosting lets one server serve many domains by checking the requested address.

This saves money, simplifies management, and uses resources efficiently.