Configure Domain and Subdomain with Nginx on Ubuntu
Serve different apps with domains and subdomains in Nginx.
Modern web applications often require multiple entry points—perhaps a main site (example.com
) and an admin dashboard (admin.example.com
). Nginx, a high-performance web server, makes it straightforward to host different applications by mapping domains and subdomains to specific apps or directories. This guide will walk you through configuring both a domain and a subdomain with Nginx on Ubuntu.
Prerequisites
- Root or sudo access to your Ubuntu server
- Nginx installed (
sudo apt install nginx
) - Domains/subdomains pointing to your server (update DNS A records as needed)
- Sample web app directories you want to serve
1. Directory Structure
Let’s assume you want:
- The main app at:
example.com
(served from/var/www/example.com/html
) - The admin panel at:
admin.example.com
(served from/var/www/admin.example.com/html
)
Create the directories:
sudo mkdir -p /var/www/example.com/html
sudo mkdir -p /var/www/admin.example.com/html
Assign permissions:
sudo chown -R $USER:$USER /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/admin.example.com/html
Create a simple index file to confirm:
echo "<h1>Welcome to example.com</h1>" > /var/www/example.com/html/index.html
echo "<h1>Welcome to admin.example.com</h1>" > /var/www/admin.example.com/html/index.html
2. Create Nginx Server Blocks
Nginx uses server blocks (similar to Apache “virtual hosts”) to serve different content based on the domain.
Navigate to the sites-available directory:
cd /etc/nginx/sites-available
Main Domain: example.com
Create a configuration file:
sudo nano /etc/nginx/sites-available/example.com
Insert the following:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
try_files $uri $uri/ =404;
}
}
Subdomain: admin.example.com
Create a config for the subdomain:
sudo nano /etc/nginx/sites-available/admin.example.com
Add this content:
server {
listen 80;
server_name admin.example.com;
root /var/www/admin.example.com/html;
index index.html index.htm;
access_log /var/log/nginx/admin.example.com.access.log;
error_log /var/log/nginx/admin.example.com.error.log;
location / {
try_files $uri $uri/ =404;
}
}
3. Enable the Domain and Subdomain
To enable your configurations, create symbolic links from sites-available
to sites-enabled
:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/admin.example.com /etc/nginx/sites-enabled/
Remove the default configuration if you don’t need it:
sudo rm /etc/nginx/sites-enabled/default
4. Test and Reload Nginx
Before applying changes, verify that your config files are correct:
sudo nginx -t
If all’s well, reload Nginx:
sudo systemctl reload nginx
5. Verify Configuration
Try accessing:
Each should display the welcome message set previously.
6. (Optional) Secure with Let's Encrypt SSL
If you're going live, securing your sites with HTTPS is essential. The easiest way is via Certbot:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com -d admin.example.com
Follow the prompts to complete SSL installation.
7. DNS Reminders
Don’t forget to set up your DNS records:
- An A Record for
example.com
to your server IP - An A Record for
admin.example.com
to the same server IP
DNS changes can take some time to propagate.
Conclusion
Nginx makes hosting multiple apps easy by mapping domains and subdomains to different directories or backends. With just a few configuration tweaks, you can serve multiple applications from a single server—perfect for modern web development needs. For advanced scenarios, you can proxy to different backends, set up rate limits, or implement caching, all with Nginx’s powerful configuration options.
Happy hosting!