13. Multiple Websites on One Server and HTTPS with Certbot
13.2 Multiple Sites with Nginx (Amazon Linux 2023 and Ubuntu)
for s in site1 site2; do
sudo mkdir -p /var/www/$s
echo "<h1>Welcome to $s</h1>" | sudo tee /var/www/$s/index.html
done
Nginx (AL2023/CentOS: files in conf.d/; Ubuntu: files in sites-available/ plus a symlink for each):
# site1.conf
server {
listen 80;
server_name site1.example.com;
root /var/www/site1;
access_log /var/log/nginx/site1_access.log;
}
# site2.conf
server {
listen 80;
server_name site2.example.com;
root /var/www/site2;
access_log /var/log/nginx/site2_access.log;
}
# 00-default.conf: requests by IP or unknown names
server {
listen 80 default_server;
server_name _;
return 444; # close connection (or: return 301 http://site1.example.com;)
}
# Ubuntu only: enable and disable
sudo ln -s /etc/nginx/sites-available/site1.conf /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/site2.conf /etc/nginx/sites-enabled/
sudo rm -f /etc/nginx/sites-enabled/default
Amazon Linux 2023 – complete commands (note \$uri, escaped because the here-doc is unquoted so that $s expands):
for s in site1 site2; do
sudo tee /etc/nginx/conf.d/$s.conf > /dev/null <<CONF
server {
listen 80;
server_name $s.yourdomain.com;
root /var/www/$s;
index index.html;
location / { try_files \$uri \$uri/ =404; }
access_log /var/log/nginx/${s}_access.log;
}
CONF
done
sudo nginx -t && sudo service nginx reload
Ubuntu – same files in sites-available, then enable:
for s in site1 site2; do
sudo tee /etc/nginx/sites-available/$s > /dev/null <<CONF
server {
listen 80;
server_name $s.yourdomain.com;
root /var/www/$s;
index index.html;
location / { try_files \$uri \$uri/ =404; }
}
CONF
sudo ln -sf /etc/nginx/sites-available/$s /etc/nginx/sites-enabled/$s
done
sudo rm -f /etc/nginx/sites-enabled/default
sudo nginx -t && sudo service nginx reload
Ravindra Bagale's Tip
If you write <<'CONF' (with quotes) in a here-doc, $s is not expanded. Here we used it without quotes because the loop needs the variable – but then Nginx variables like $uri must be escaped as \$uri. Many students get caught here; after creating the config, always check it with cat.
Ravindra Bagale's Tip – मराठी
Here-doc मध्ये <<'CONF' (quotes सोबत) लिहिलं तर $s expand होत नाही. Loop मध्ये variable हवा म्हणून इथे quotes शिवाय वापरलं आहे – पण मग Nginx चे $uri सारखे variables \$uri असे escape करावे लागतात. बरेच students इथे फसतात; config बनवल्यावर cat करून नक्की बघा.
Ravindra Bagale's Tip – हिंदी
Here-doc में <<'CONF' (quotes के साथ) लिखा तो $s expand नहीं होता. Loop में variable चाहिए इसलिए यहाँ बिना quotes के इस्तेमाल किया है – पर फिर Nginx के $uri जैसे variables को \$uri जैसे escape करना पड़ता है. बहुत से students यहाँ फँसते हैं; config बनाने के बाद cat करके ज़रूर देखो.
Lab
Create site1 and site2 with Nginx on Amazon Linux or Ubuntu, add the catch-all default, and test with curl -H "Host: ..." http://localhost.
Extended teaching notes
Why this lesson matters
Use this lesson to strengthen systems you own: understand the risk, practise safely in a lab, then write and apply the fix.
Apply the fix across stacks
Same class of bug, same idea in every language.
Java / Spring Boot
Parameterise queries, encode output, CSRF tokens, limited Actuator, dependency scanning.
ASP.NET Core
EF Core / parameterised SQL, Razor encoding, HTTPS, Data Protection.
Node.js / Express
helmet, parameterised SQL, npm audit, avoid eval and shell concat.
Python / Django / Flask / FastAPI
ORM, autoescape, pip-audit, Bandit; never shell=True with user input.
PHP / Laravel
PDO/Eloquent, Blade escaping, composer audit.
Go / Ruby
database/sql placeholders / ActiveRecord; html/template / ERB escaping; govulncheck / Brakeman.
nginx / Apache / Tomcat
Hide versions, TLS, security headers, least-open ports.
MySQL / PostgreSQL / MongoDB / Redis
Private networks, auth on, least-privilege users, no 0.0.0.0/0.
AWS / Azure / GCP
Roles not long-lived keys, private data stores, cloud audit logs on.
Mini case study
Suppose we are on a purple-team exercise for an Indian mid-size company (sample data). Red notes a technique from this lesson against a lab clone. Blue must show the control that stops it, the log that would have fired, and the ticket that hardens production. That is the standard this course trains you for.
Defender's worksheet (do this today)
- Write the lesson title, date and the exact lab target you are allowed to use.
- List three owned systems (VMs, apps, or cloud accounts) where this lesson applies.
- Run one safe check from the lesson on a lab system only. Paste the command and output.
- Write a two-sentence fix you would give a teammate.
- Name one related public advisory (CERT-In, CISA, NVD, or a vendor bulletin) with its date.
Indian workplace (sample data)
Suppose we are a junior analyst at Infosys, TCS, Wipro or a fintech SOC in Bengaluru/Pune. Turn this lesson into one checklist item with an owner, a due date and a place to store evidence (ticket + screenshot).
Global practice
Compare your notes with OWASP, CIS Benchmarks, NIST CSF/IR guidance or a cloud Well-Architected security question. Write five lines on what overlaps.
Everyday analogy
Explain the risk and the fix to a family member without jargon. If they can repeat the warning, you understand it well enough to teach juniors.
Key terms
- Control: a safeguard (patch, config, process, detection).
- Finding: a weakness backed by evidence.
- Remediation: the fix plus how you verified it.
- Scope: systems you have written permission to touch.
- Least privilege: only the access the task needs.
- Defence in depth: several layers so one failure does not end the story.
Common mistakes
- Skipping written permission "just for a quick scan".
- Practising on hostel, office or public targets without authorisation.
- Collecting tool output without writing the fix.
- Copying attack steps onto production to "verify". Defenders verify with versions, configs and patches.
- Publishing screenshots that contain secrets, cookies or internal hostnames.
Recap
- Evidence, fix, owner, date.
- Own lab only.
- Fix > spectacle.