Ravindra BagaleCourses & study guides मराठी Track your progress

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.

  1. Draw the four questions a web server answers for every request and name the directive for each in Nginx and Apache.
  2. Write the site-file location and enable method for Nginx and Apache on Ubuntu and on Amazon Linux 2023.
  3. Why is Ubuntu's default Apache site named 000-default.conf?
  4. What does Options -Indexes do? What is its Nginx equivalent?
  5. Lab: Move your Nginx site to /var/www/portfolio, then (on CentOS Stream 9) to /srv/portfolio using semanage fcontext.
  6. Lab: Run a second site on port 8081 with Apache. Open it in the security group from My IP only.
  7. Lab: Host site1.example.com and site2.example.com plus a catch-all default on one server. Verify all three with curl -H "Host: ...".
  8. Lab: Hide the server version and check the Server: header with curl -I before and after.
  9. Lab: Deliberately remove a ; from your server block and read the nginx -t error 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 or a2ensite (Ubuntu). Never edit main files for a site.
  • Fallback site: Nginx default_server or first block; Apache first vhost loaded (000-default.conf). Inspect with nginx -T / apachectl -S.
  • Change root: new folder 755/644 → update root / DocumentRoot and <Directory> → CentOS: semanage fcontext + restorecon for 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.

Extra labs across stacks

  1. Lab: Spring Boot behind nginx. Run any Spring Boot jar on 8080. Proxy it, and confirm with curl -I that the response comes through port 80.
  2. Lab: Flask behind Apache. Use ProxyPass to 127.0.0.1:8000.
  3. Lab: React build. Serve dist/ with try_files. Refresh a deep route and confirm you get 200, not 404.
  4. Lab: rate limit. Add limit_req to /login. Run 20 quick curls and expect some 503 responses.
  5. Lab: restrict admin. Add allow <your-ip>; deny all; to an admin location. Test from mobile data and expect 403.
Lab mapnginx:8080 Spring8000 Flaskdist/ React
One nginx in front of three different apps.

Stretch lab: security headers across stacks

  1. Add X-Content-Type-Options and Referrer-Policy in nginx.
  2. Check them with curl -I.
  3. Compare with the app-level way: helmet in Express, SecurityMiddleware in Django, Spring Security's default headers.
  4. 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.