Nginx · The edge of your stack
How is an Nginx configuration structured?
Short answer
Nginx config is a tree of directives in contexts: main, events, http, server and location. Directives are inherited by child contexts unless the child overrides them.
Most settings have a defined context. Putting a directive in the wrong place is a common reason a config fails to start. The hierarchy also explains inheritance: a setting in http applies to every server and location below it until overridden.
user nginx;
worker_processes auto;
events {
worker_connections 4096;
}
http {
include /etc/nginx/mime.types;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name example.com;
root /var/www/site;
location / {
try_files $uri $uri/ =404;
}
}
}Context rules
- main holds process-wide directives such as user, worker_processes and pid
- events controls connection acceptance and worker connection limits
- http groups all HTTP servers, MIME types, logging and shared zones
- server represents a virtual host; location selects a URI prefix or pattern
- include splices another file into the current context
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What happens if you put a server block outside http?
- 2Which directives are inherited by a location?
- 3Why does add_header in a location hide headers from the server block?
- 4How would you split a large config into maintainable files?
Say it in the interview
An Nginx config is a tree: main, events, http, server and location. Each directive has allowed contexts, and child contexts inherit settings unless they override them. I keep shared HTTP settings in http, virtual hosts in server blocks and URI-specific rules in locations. I also use includes to split large configs, and I remember that some directives such as add_header do not inherit the way people expect.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Nginx mock interview and get scored feedback on depth, clarity and confidence.