Chapter 8: Configuring Nginx and Apache
8.2 Reading an Nginx configuration
In short: Nginx configuration is made of directives (name value;) and blocks (name { ... }), also called contexts.
Friends, an Nginx config is made of directives and blocks. Once you understand these four or five directives – listen, server_name, root, index, location – you can read any server block.
मित्रांनो, Nginx कॉन्फिग म्हणजे directives आणि ब्लॉक्स. एकदा हे चार-पाच directives समजले – listen, server_name, root, index, location – की कोणताही server ब्लॉक वाचता येतो.
दोस्तों, Nginx कॉन्फ़िग मतलब directives और ब्लॉक्स. एक बार ये चार-पाँच directives समझ आ गए – listen, server_name, root, index, location – तो कोई भी server ब्लॉक पढ़ सकते हो.
Nginx configuration is made of directives (name value;) and blocks (name { ... }), also called contexts:
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
Many students write server_name http://example.com; with http://, and the block never matches. Put only the host name in server_name: server_name example.com www.example.com;. And if the welcome page shows up, first check which block is the default_server.
Ravindra Bagale's Tip – मराठी
खूप students server_name http://example.com; असा http:// सोबत लिहितात, आणि block कधीच match होत नाही. server_name मध्ये फक्त host नाव: server_name example.com www.example.com;. आणि welcome page दिसत असेल तर default_server कोणता block आहे ते आधी बघा.
Ravindra Bagale's Tip – हिंदी
बहुत students server_name http://example.com; ऐसे http:// के साथ लिखते हैं, और block कभी match नहीं होता. server_name में सिर्फ host नाम: server_name example.com www.example.com;. और welcome page दिख रहा हो तो पहले देखो कि default_server कौन सा block है.
Lab
Chala, tumcha server block ughda aani pratyek line samor comment madhe tumchya shabdat arth liha. Mag curl -I -H "Host: example.com" http://localhost aani curl -I http://localhost chalva aani kontya block ne uttar dila te access log madhun shodha.