Chapter 6: Web Servers — Nginx and Apache
6.1 What is a web server?
Friends, so far we have launched a server. But a server without a web server is like a shop with the shutter down — customers come but nobody serves them. Mauli Dairy's customers open the order page, but if nobody is there to answer, they see nothing. Let's understand what a web server really does.
मित्रांनो, आतापर्यंत आपण सर्व्हर लॉन्च केला. पण वेब सर्व्हर नसलेला सर्व्हर म्हणजे शटर बंद असलेलं दुकान – कस्टमर्स येतात पण त्यांना कोणी सर्व्ह करत नाही. Mauli Dairy चे कस्टमर्स ऑर्डर पेज उघडतात, पण तिथे उत्तर देणारा कोणी नसेल तर काहीच दिसत नाही. चला, वेब सर्व्हर खरंच काय करतो ते समजून घेऊ.
दोस्तों, अब तक हमने सर्वर लॉन्च किया. लेकिन वेब सर्वर के बिना सर्वर मतलब शटर बंद दुकान – कस्टमर्स आते हैं पर उन्हें कोई सर्व नहीं करता. Mauli Dairy के कस्टमर्स ऑर्डर पेज खोलते हैं, लेकिन वहाँ जवाब देने वाला कोई न हो तो कुछ नहीं दिखता. चलो, समझते हैं कि वेब सर्वर असल में क्या करता है.
A web server is software that listens on a port (80 for HTTP, 443 for HTTPS), accepts HTTP requests from browsers and returns responses — HTML pages, images, CSS, JavaScript files, or data produced by an application.
Browser EC2 instance
+--------+ GET /index.html +---------------------------------------+
| | -------------------> | Web server (Nginx / Apache) :80 |
| | | ├─ static file? → read from disk |
| | <------------------- | └─ dynamic? → pass to PHP / |
+--------+ 200 OK + HTML | Node / Python app|
+---------------------------------------+
A web server does three main jobs:
- Serve static content — files that are the same for everyone (HTML, CSS, JS, images).
- Pass dynamic requests to an application (PHP-FPM, Node.js, Gunicorn) — called reverse proxying — and return its output.
- Handle cross-cutting concerns — TLS/HTTPS, compression, caching, logging, virtual hosts (many websites on one server), load balancing, rate limiting.
Common HTTP status codes
| Code | Meaning | Typical cause |
|---|---|---|
| 200 | OK | Success |
| 301 / 302 | Moved permanently / temporarily | Redirect (e.g. HTTP → HTTPS) |
| 304 | Not modified | Browser cache is fresh |
| 400 | Bad request | Malformed request |
| 401 / 403 | Unauthorised / Forbidden | Missing login / file permissions, no index file, SELinux |
| 404 | Not Found | Wrong path or wrong document root |
| 500 | Internal server error | Bug in app / PHP error |
| 502 | Bad Gateway | Reverse-proxy target (app) is down or wrong port/socket |
| 503 | Service unavailable | Overloaded or maintenance |
| 504 | Gateway timeout | App took too long to respond |
Ravindra Bagale's Tip
Many students mix up 502 and 504. Remember: 502 Bad Gateway means the app behind the proxy is down or the port/socket is wrong; 504 means the app took too long to reply. And if you get 403, check permissions and the index file first – the server is not down.
Ravindra Bagale's Tip – मराठी
खूप students 502 आणि 504 मध्ये गोंधळतात. लक्षात ठेवा: 502 Bad Gateway म्हणजे proxy मागचा app बंद आहे किंवा port/socket चुकीचा आहे; 504 म्हणजे app ने उत्तर द्यायला खूप वेळ लावला. आणि 403 आला तर आधी permissions आणि index file बघा – server down नाही.
Ravindra Bagale's Tip – हिंदी
बहुत students 502 और 504 में उलझ जाते हैं. याद रखो: 502 Bad Gateway मतलब proxy के पीछे का app बंद है या port/socket गलत है; 504 मतलब app ने जवाब देने में बहुत देर लगाई. और 403 आए तो पहले permissions और index file देखो – server down नहीं है.
Lab
Chala, tumchya laptop var curl -I https://www.example.com chalva aani pahilya line madhla status code liha. Mag curl -I https://www.example.com/nahi-asa-page chalva aani 404 disato ka te bagha. Doni responses madhla Server: header pan liha.