7. Apache and Nginx: Install and Understand
7.5 Reading an Nginx Server Block
Also in the AWS course: this lesson is the same as 8.2 Reading an Nginx configuration in the AWS course, which is the main version of this page.
Config file vachta aali ki web server tumchya haatat yeto. Nginx madhe directives (naav value;) aani blocks (naav { ... }) astat.
main context (user, worker_processes, error_log)
├── events { worker_connections ... }
└── http { (mime types, logging, gzip, include conf.d/*.conf)
└── server { ← one per website (a "server block")
listen ...; server_name ...; root ...;
└── location /path { ... } ← rules for part of the URL
}
}
A fully commented server block:
# /etc/nginx/conf.d/mysite.conf (Ubuntu: /etc/nginx/sites-available/mysite)
server {
listen 80; # IPv4 port
listen [::]:80; # IPv6 port
server_name example.com www.example.com; # which Host names this block answers
root /var/www/mysite; # document root: URL "/" maps here
index index.html index.htm; # file served for a folder request
location / {
try_files $uri $uri/ =404; # file? folder? otherwise 404
}
location /images/ {
expires 7d; # let browsers cache images for 7 days
}
error_page 404 /404.html; # custom error page (file inside root)
autoindex off; # never list folder contents
access_log /var/log/nginx/mysite_access.log;
error_log /var/log/nginx/mysite_error.log;
}
| Directive | What it does | Example |
|---|---|---|
listen |
Port (and optional IP) to accept connections on; default_server makes this the fallback |
listen 80 default_server; |
server_name |
Names matched against the Host header; _ is a "match nothing" placeholder |
server_name example.com www.example.com; |
root |
Folder that URL paths are appended to | root /var/www/mysite; |
index |
File(s) to try for a folder URL | index index.html index.php; |
location |
Rules for matching URL paths | location /api/ { ... } |
try_files |
Try files in order, then fall back | try_files $uri $uri/ /index.html; (SPA) |
return |
Send a redirect or status immediately | return 301 https://$host$request_uri; |
error_page |
Custom error page | error_page 404 /404.html; |
client_max_body_size |
Maximum upload size (default 1 MB) | client_max_body_size 20M; |
server_tokens |
Show/hide Nginx version in headers and error pages | server_tokens off; |
The two classic Nginx syntax errors
Every directive ends with a semicolon ;, and every { needs a matching }. sudo nginx -t prints the exact file and line number, e.g. unexpected "}" in /etc/nginx/conf.d/mysite.conf:9. Read it slowly. It is almost always the line before the one reported.
How Nginx chooses the server block
- It looks at blocks whose
listenmatches the port of the request. - Among those, it picks the block whose
server_namematches theHostheader exactly (then wildcard names like*.example.com). - If nothing matches, it uses the block marked
default_serverfor that port, or the first block loaded if none is marked.
That rule explains the most common complaint: "I configured my site but I still see the welcome page." Some other block won, because it is the default or because your server_name doesn't match what the browser sent.
Ravindra Bagale's Tip
Forgetting a semicolon ; is the most common mistake students make in Nginx. sudo nginx -t tells you the file and line number – and the mistake is usually on the line before that line. Read calmly.
Ravindra Bagale's Tip – मराठी
Nginx मध्ये semicolon ; विसरणं ही students ची सगळ्यात common चूक आहे. sudo nginx -t file आणि line number सांगतो – आणि चूक बहुतेक वेळा त्या line च्या आधीच्या line वर असते. शांतपणे वाचा.
Ravindra Bagale's Tip – हिंदी
Nginx में semicolon ; भूलना students की सबसे common गलती है. sudo nginx -t file और line number बताता है – और गलती ज़्यादातर उस line से पहले वाली line पर होती है. शांति से पढ़ो.
Practice task
Open /etc/nginx/nginx.conf on Amazon Linux (or /etc/nginx/sites-available/default on Ubuntu) and identify listen, server_name, root, index and every location block.