Master Nginx Virtual Hosts: Domain, IP, and Port Configurations Explained
This step‑by‑step guide shows how to configure Nginx virtual hosts using domain names, IP addresses, and ports, covering directory preparation, nginx.conf edits, syntax testing, service reload, and client verification for each method.
Domain‑based virtual host
Step 1 – Create site directories
# mkdir -p /usr/local/nginx/html/site1
# mkdir -p /usr/local/nginx/html/site2
# echo "Welcome to Site 1" > /usr/local/nginx/html/site1/index.html
# echo "Welcome to Site 2" > /usr/local/nginx/html/site2/index.htmlStep 2 – Edit nginx.conf
Insert two server blocks inside the http section. The first block listens on port 80, uses server_name www.site1.com, and sets root html/site1. The second block is identical except for server_name www.site2.com and root html/site2. Both blocks define index index.html, a location / that tries files, error pages, and a rule to deny access to hidden .ht files.
Step 3 – Test configuration and reload Nginx
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reloadStep 4 – Verify in a browser
http://www.site1.com should display "Welcome to Site 1".
http://www.site2.com should display "Welcome to Site 2".
For local testing, add entries to /etc/hosts mapping the domain names to the server IP.
IP‑based virtual host
Step 1 – Create site directories
# mkdir -p /usr/local/nginx/html/ip1
# mkdir -p /usr/local/nginx/html/ip2
# echo "Welcome to IP 192.168.14.111" > /usr/local/nginx/html/ip1/index.html
# echo "Welcome to IP 192.168.14.112" > /usr/local/nginx/html/ip2/index.htmlStep 2 – Edit nginx.conf
Add two server blocks that bind to the specific IP addresses.
server {
listen 192.168.14.111:80;
server_name 192.168.14.111;
root html/ip1;
index index.html;
location / { try_files $uri $uri/ =404; }
error_page 500 502 503 504 /50x.html;
location = /50x.html { root html; }
}
server {
listen 192.168.14.112:80;
server_name 192.168.14.112;
root html/ip2;
index index.html;
location / { try_files $uri $uri/ =404; }
error_page 500 502 503 504 /50x.html;
location = /50x.html { root html; }
}Step 3 – Test configuration and reload Nginx
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reloadStep 4 – Verify in a browser
http://192.168.14.111 should display "Welcome to IP 192.168.14.111".
http://192.168.14.112 should display "Welcome to IP 192.168.14.112".
Because the test runs in a VM with a single NIC, an additional virtual NIC is added for the second IP:
ip addr add 192.168.14.110/24 dev ens33Port‑based virtual host
Step 1 – Create site directories
# mkdir -p /usr/local/nginx/html/port1
# mkdir -p /usr/local/nginx/html/port2
# echo "Welcome to Port 8080" > /usr/local/nginx/html/port1/index.html
# echo "Welcome to Port 9090" > /usr/local/nginx/html/port2/index.htmlStep 2 – Edit nginx.conf
Add two server blocks that listen on different ports.
server {
listen 8080;
server_name localhost;
root html/port1;
index index.html;
location / { try_files $uri $uri/ =404; }
error_page 500 502 503 504 /50x.html;
location = /50x.html { root html; }
}
server {
listen 9090;
server_name localhost;
root html/port2;
index index.html;
location / { try_files $uri $uri/ =404; }
error_page 500 502 503 504 /50x.html;
location = /50x.html { root html; }
}Step 3 – Test configuration and reload Nginx
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reloadStep 4 – Verify in a browser
http://192.168.14.111:8080 should display "Welcome to Port 8080".
http://192.168.14.111:9090 should display "Welcome to Port 9090".
Summary
The three configurations demonstrate how Nginx can serve multiple sites distinguished by domain name, IP address, or port. The steps include creating separate document roots, adding appropriate server blocks to nginx.conf, testing the configuration with nginx -t, reloading the service, and verifying the expected responses in a browser.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
