Chapter 8: Configuring Nginx and Apache
Practice Questions and Lab Exercises
Come on, practice time! Do every lab, and make one mistake on purpose to read the error – once you learn to read errors, configs won't scare you.
चला, आता प्रॅक्टिस! प्रत्येक लॅब करा आणि मुद्दाम एक चूक करून error वाचा – error वाचायला शिकलात की कॉन्फिग घाबरवणार नाही.
चलो, अब प्रैक्टिस! हर लैब करो और जान-बूझकर एक गलती करके error पढ़ो – error पढ़ना सीख गए तो कॉन्फ़िग डराएगा नहीं.
- Draw the four questions a web server answers for every request and name the directive for each in Nginx and Apache.
- Write the site-file location and enable method for Nginx and Apache on Ubuntu and on Amazon Linux 2023.
- Why is Ubuntu's default Apache site named
000-default.conf? - What does
Options -Indexesdo? What is its Nginx equivalent? - Lab: Move your Nginx site to
/var/www/portfolio, then (on CentOS Stream 9) to/srv/portfoliousingsemanage fcontext. - Lab: Run a second site on port 8081 with Apache. Open it in the security group from My IP only.
- Lab: Host
site1.example.comandsite2.example.complus a catch-all default on one server. Verify all three withcurl -H "Host: ...". - Lab: Hide the server version and check the
Server:header withcurl -Ibefore and after. - Lab: Deliberately remove a
;from your server block and read thenginx -terror message.
Quick Revision
- Four questions per request: port (
listen/Listen) → site (server_name/ServerName, Host header) → folder (root/DocumentRoot) → file (index/DirectoryIndex). - Site files:
conf.d/*.conf(AL2023/CentOS),sites-available+ symlink ora2ensite(Ubuntu). Never edit main files for a site. - Fallback site: Nginx
default_serveror first block; Apache first vhost loaded (000-default.conf). Inspect withnginx -T/apachectl -S. - Change root: new folder 755/644 → update
root/DocumentRootand<Directory>→ CentOS:semanage fcontext+restoreconfor paths outside/var/www. - New port: config + security group + firewalld +
semanage port(CentOS). - Workflow: backup (
.bak) → edit →nginx -t/apachectl configtest→sudo service ... reload→curl -I→ logs.
Got it? Port, site, folder, file – these four questions, plus the backup-test-reload workflow. Now let's move on and give the Amul-style dairy's site a real domain.
समजलं का? पोर्ट, साइट, फोल्डर, फाइल – हे चार प्रश्न आणि backup-test-reload हा workflow. आता पुढे जाऊया Amul-स्टाइल डेअरीच्या साइटला खरं डोमेन द्यायला.
समझ आया? पोर्ट, साइट, फ़ोल्डर, फ़ाइल – ये चार सवाल और backup-test-reload वाला workflow. अब आगे चलते हैं Amul-स्टाइल डेयरी की साइट को असली डोमेन देने.
Extra labs across stacks
- Lab: Spring Boot behind nginx. Run any Spring Boot jar on 8080. Proxy it, and confirm with
curl -Ithat the response comes through port 80. - Lab: Flask behind Apache. Use
ProxyPassto 127.0.0.1:8000. - Lab: React build. Serve
dist/withtry_files. Refresh a deep route and confirm you get 200, not 404. - Lab: rate limit. Add
limit_reqto/login. Run 20 quick curls and expect some503responses. - Lab: restrict admin. Add
allow <your-ip>; deny all;to an admin location. Test from mobile data and expect403.
Stretch lab: security headers across stacks
- Add
X-Content-Type-OptionsandReferrer-Policyin nginx. - Check them with
curl -I. - Compare with the app-level way: helmet in Express,
SecurityMiddlewarein Django, Spring Security's default headers. - Decide where to set them once (the web server or the app), so they are not duplicated.
Real example: security scanners used by Indian banks during audits commonly flag missing headers. Fixing them at nginx covers every app behind it.
Key terms
- server_name: The host names a server block answers to.
- listen: The port (and IP) a block binds to.
- default_server: The fallback block for a port.
- Options -Indexes: The Apache setting that turns off directory listing.
- semanage fcontext: Sets SELinux labels for a path.
- limit_req: nginx request rate limiting.
Mini case study
Suppose we are a junior cloud engineer at a Bengaluru e-commerce startup (sample data). The team runs a PHP admin, a Node.js storefront API and a Python report job on two EC2 instances. Using this chapter, you write a one-page runbook: which service listens where, how to test and reload each one, which logs to read, and which ports are public (only 80 and 443, plus 22 from the office IP). New team members can then fix most problems on their own.