In modern web deployment, configuring multiple sites with Nginx—whether using different domains or subdomains—and enabling HTTPS has become a standard best practice. HTTPS not only enhances the security and trustworthiness of your websites but also contributes positively to SEO rankings.
This guide walks you through a full example of hosting two static sites on yourdomain.com
and its subdomain tgame.yourdomain.com
using Nginx and Certbot on CentOS 7. Whether you’re a beginner webmaster or a DevOps engineer, this tutorial will help you implement a free HTTPS solution with automated certificate management using Let’s Encrypt.
🧾 1. Environment Requirements
Component | Description |
---|---|
OS | CentOS 7 or later |
Web Server | Nginx |
HTTPS Certificate | Let’s Encrypt (via Certbot) |
Domains | yourdomain.com , tgame.yourdomain.com |
Prerequisites | Valid domains with DNS correctly pointing to your public server IP |
🛠️ 2. Install Required Packages
Step 1: Install EPEL and Nginx
sudo yum install epel-release -y
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
Step 2: Install Certbot and Nginx Plugin
sudo yum install certbot python2-certbot-nginx -y
📁 3. Create Website Root Directories (Optional but Recommended)
To better organize content for multiple sites, create separate directories for each domain:
sudo mkdir -p /var/www/yourdomain.com/html
sudo mkdir -p /var/www/tgame.yourdomain.com/html
sudo chown -R nginx:nginx /var/www
You can now place your index.html
or other static content in the respective folders.
⚙️ 4. Configure Nginx Virtual Hosts
Step 1: Create Config for Main Domain
File: /etc/nginx/conf.d/yourdomain.com.conf
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/yourdomain.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Step 2: Create Config for Subdomain
File: /etc/nginx/conf.d/tgame.yourdomain.com.conf
server {
listen 80;
server_name tgame.yourdomain.com;
root /var/www/tgame.yourdomain.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Step 3: Test and Reload Nginx
sudo nginx -t
sudo systemctl reload nginx
🌐 5. Set Up DNS Records
Log in to your DNS provider (e.g., Aliyun, Cloudflare, GoDaddy) and add the following A records:
Type | Name | Value (Server IP) |
---|---|---|
A | @ | 123.123.123.123 |
A | www | 123.123.123.123 |
A | tgame | 123.123.123.123 |
Tips:
- Use
dig tgame.yourdomain.com +short
orping
to verify DNS resolution. - If using Cloudflare, disable proxy (make icon gray) for A records to ensure Let’s Encrypt validation can succeed.
🔐 6. Obtain and Configure HTTPS Certificates
Use Certbot to automatically issue certificates and update your Nginx config.
Issue for Individual Sites
sudo certbot -nginx -d yourdomain.com -d www.yourdomain.com
sudo certbot -nginx -d tgame.yourdomain.com
Recommended: Issue for Multiple Domains at Once
sudo certbot -nginx \
-d yourdomain.com \
-d www.yourdomain.com \
-d tgame.yourdomain.com
Certbot will automatically:
- Validate domain ownership
- Issue a 90-day certificate
- Update your Nginx configs for HTTPS
- Add HTTP → HTTPS permanent redirects (301)
🔁 7. Test Auto-Renewal
Let’s Encrypt certificates are valid for 90 days. Certbot sets up auto-renewal using cron or systemd. Test it manually with:
sudo certbot renew -dry-run
📊 8. Verify Deployment
Open your browser and check:
- https://yourdomain.com
- https://www.yourdomain.com
- https://tgame.yourdomain.com
You should see your site content and a secure HTTPS (green padlock) icon.
❗ 9. Common Issues & Fixes
Problem | Likely Cause | Solution |
---|---|---|
NXDOMAIN error | DNS not resolved | Check DNS records; wait for propagation |
403 Forbidden | Incorrect permissions or missing files | Check ownership and presence of index.html |
Certificate failure | Port 80/443 blocked by firewall/proxy | Open required ports or stop conflicting services |
HTTPS error page | Browser cache or Nginx not reloaded | Clear cache, test config with nginx -t , reload |
✅ Conclusion
With this step-by-step guide, you’ve learned how to:
- Install and configure Nginx on CentOS 7
- Host multiple sites with individual root directories
- Use Let’s Encrypt to issue free HTTPS certificates
- Enable automatic renewal to ensure long-term uptime