What if one server could magically become many, each showing a different website?
Why virtual hosting serves multiple domains in Nginx - The Real Reasons
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.
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.
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.
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;
}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;
}
}
}Virtual hosting makes it easy and cheap to run many websites on one server, saving money and time.
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.
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.