Chapter 8: Configuring Nginx and Apache
Interview Questions
- 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).