Nginx configuration contexts: main, http and server
What is a context?
A context is a place in the configuration where a directive is allowed. For example, worker_processes belongs outside every block, while worker_connections belongs inside events. Nginx checks context as well as punctuation. A correctly spelled directive can still be invalid if placed in the wrong block.
main (no enclosing braces) worker_processes events worker_connections http upstream (optional group) map (optional mapping) server location stream (optional subsystem) mail (optional subsystem)Read this diagram from the outside inward. The main level contains global settings. events describes connection handling. http contains HTTP application delivery settings; its server blocks describe virtual servers and location blocks select settings for matching request URIs. A server block does not necessarily correspond to a separate physical machine.
See a complete lab configuration
worker_processes 1;pid logs/nginx.pid;lock_file logs/nginx.lock;error_log logs/error.log notice;events { worker_connections 32; }http { access_log logs/access.log; client_body_temp_path temp/body; proxy_temp_path temp/proxy; fastcgi_temp_path temp/fastcgi; uwsgi_temp_path temp/uwsgi; scgi_temp_path temp/scgi; default_type text/plain; server { listen 127.0.0.1:18083; server_name localhost; location / { root html; } }}The listener accepts connections on IPv4 loopback port 18083. The root directive supplies the directory used when resolving file paths. In a correctly prepared lab containing html/hello.txt, a request for /hello.txt should return that file. The prefix, directories and permission requirements are documented in the fixture README. This is a predicted result, not a recorded HTTP test.
Other HTTP blocks and optional subsystems
upstream demo_backend { server 127.0.0.1:18084;}map $uri $page_kind { default other; /hello.txt greeting;}Both blocks belong directly inside http. An upstream is a named group of backend addresses; defining a group does not automatically send requests to it. A map defines a variable based on another value. Here $page_kind would be greeting for the matching URI and other for the default case, when the result variable is used. These blocks are previews; no backend needs to run for this lesson.
stream is a separate outermost subsystem for TCP or UDP proxying, and mail is a separate subsystem for mail proxy protocols. They require corresponding compiled or loaded modules. Their server blocks have their own meanings and permitted directives. They are not HTTP locations. Consult the exact subsystem module reference instead of assuming all similarly named directives are interchangeable.
Find context errors before deployment
events { worker_connections 32;}http { # HTTP settings belong here.}The problem was placement rather than spelling. When reading a module reference, find the directive’s Context line first; then review the syntax, default and any documented inheritance or version restrictions. Nested HTTP locations have additional restrictions that the later location-matching chapter will explain.
Practice
Place pid, worker_connections, server_name, and an HTTP upstream into their appropriate contexts. For server_name, choose an HTTP server block. Explain why an upstream server address is not another virtual HTTP listener.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Configuration contexts
Check the main concepts taught in this lesson.
Where does worker_connections belong?
What is main?
Where is an HTTP upstream group declared?
Why can stream be unavailable?
What does a virtual HTTP server block define?