Install
# Amazon Linux 2023
sudo dnf install -y nginx
sudo systemctl enable --now nginx
# Ubuntu 24.04: use these instead
sudo apt update
sudo apt install -y nginx
sudo systemctl enable --now nginxRun only the distribution-appropriate pair. Check sudo nginx -T to see the complete active configuration, including included files. Ubuntu commonly uses sites-available/sites-enabled, while Amazon Linux commonly includes conf.d. Do not assume the package's default document root is identical on both.
Create your own root
sudo mkdir -p /var/www/academy
printf '<h1>Nginx academy</h1>\n' | sudo tee /var/www/academy/index.html
sudo chmod 755 /var/www/academy
sudo chmod 644 /var/www/academy/index.htmlCreate /etc/nginx/conf.d/academy.conf with the block below. For this lab use the hostname academy.example.com and test with an explicit Host header, so package default-server behaviour does not hide the result.
server {
listen 80;
server_name academy.example.com;
root /var/www/academy;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}sudo nginx -t
sudo systemctl reload nginx
curl -H 'Host: academy.example.com' http://127.0.0.1/Understand the directives
listen selects an address/port; server_name selects a hostname; root maps a URL suffix to a filesystem path; index names files to try for directory requests. try_files tests candidate paths before returning 404. A server block is not automatically a DNS record. root appends a URI path; alias substitutes the matching location prefix and needs careful trailing-slash handling. Exact, prefix and regex locations have matching rules: do not copy a regex location into a production config without checking which requests it captures.
Logs and troubleshooting
sudo tail -n 30 /var/log/nginx/error.log
sudo ss -lntp“Address already in use” means another listener owns the port. Duplicate default_server definitions prevent startup. A 403 may be permissions, a missing index with directory listing disabled, or security policy. When SELinux is enforcing, check contexts and audit messages rather than disabling it globally.
Assignment
Add /lessons/network.html and request it. Compare a real file, a missing file and a directory. Stop Nginx or move its port before running Apache simultaneously.
Official references
Ravindra’s Tip
server_name DNS record नहीं बनाता। DNS traffic को server तक लाता है; Nginx hostname देखकर सही site चुनता है।
Interview and revision check
Why might raw-IP access show a different site?
Without the expected Host name, Nginx may select the default server. Test the configured hostname or an explicit Host header.
Ravindra Bagale · Cloud & DevOps Academy · Handbook and project downloads