Chapter 8: Configuring Nginx and Apache
Interview Questions
These questions on Nginx and Apache configuration are always asked in DevOps interviews. When you reply, give the directive name and a short example.
Nginx आणि Apache कॉन्फिगरेशनवर हे प्रश्न DevOps इंटरव्ह्यूमध्ये नेहमी विचारतात. उत्तर देताना directive चं नाव आणि एक छोटं example सांगा.
Nginx और Apache कॉन्फ़िगरेशन पर ये सवाल DevOps इंटरव्यू में हमेशा पूछे जाते हैं. जवाब देते समय directive का नाम और एक छोटा example बताओ.
- Q1. What is a server block in Nginx and a virtual host in Apache?
- A configuration section that defines one website: the port it listens on, the host names it answers (
server_name/ServerName), its document root and its rules and logs. Many can exist on one server. - Q2. How does Nginx decide which server block handles a request?
- It matches the port in
listen, then theHostheader againstserver_name(exact, then wildcard, then regex). If nothing matches, it uses thedefault_serverfor that port, or the first block defined. - Q3. You changed the DocumentRoot and now get 403 Forbidden. What do you check?
- File/folder permissions along the path (
namei -l), a matching<Directory>withRequire all granted, the index file, and on SELinux systems the context (ls -Z, fix withsemanage fcontext+restorecon). - Q4. How do you run Apache on port 8081?
- Add
Listen 8081(Ubuntu:ports.conf), create<VirtualHost *:8081>, test and reload, open the port in the security group/firewall, and on CentOS allow it withsemanage port -a -t http_port_t -p tcp 8081. - Q5. What is the difference between
reloadandrestart? - Reload applies new configuration gracefully without dropping connections and keeps the old config if the new one is invalid. Restart stops and starts the service, briefly interrupting traffic.
- Q6. How do you see which virtual hosts Apache has loaded?
sudo apachectl -S(Ubuntu:sudo apache2ctl -S). For Nginx,sudo nginx -Tdumps the full loaded configuration.- Q7. How do you redirect
www.example.comtoexample.com? - A separate block for
www.example.comwithreturn 301 http://example.com$request_uri;(Nginx) orRedirect permanent / http://example.com/(Apache).
More questions across stacks
Q. How do you proxy a Spring Boot app with nginx?location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
Q. How does nginx talk to PHP-FPM?
Over FastCGI, with fastcgi_pass unix:/run/php-fpm/www.sock; (or 127.0.0.1:9000), plus include fastcgi_params;.
Q. How do you serve a React or Angular app?
Build it to static files and serve them with try_files $uri $uri/ /index.html; so client-side routes work on refresh.
Q. How do you rate-limit a login URL in nginx?
Define limit_req_zone in http, then limit_req zone=login burst=5; in the login location.
Q. What is the Apache equivalent of nginx's proxy_pass?ProxyPass / http://127.0.0.1:3000/ with mod_proxy and mod_proxy_http.
Q. Where do you terminate TLS on AWS?
On nginx with Certbot, or on an Application Load Balancer with an ACM certificate. The ALB option means no private key on the server.
Q. How do you redirect HTTP to HTTPS in nginx?
A port 80 server block with return 301 https://$host$request_uri;.
Q. How do you add security headers in nginx?add_header X-Content-Type-Options nosniff always; and similar lines in the server block, then check with curl -I.
Q. How do you hide the PHP version?expose_php = Off in php.ini.
Q. What is the Tomcat equivalent of server_tokens?
Set the server attribute on the Connector, and remove the default ROOT, docs and examples apps.
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.