{"dataset":{"id":"00000000-0000-0000-0000-000000000026","slug":"nginx-best-practices","name":"nginx best practices","description":"Source-linked guidance on nginx fundamentals: configuration structure and server blocks, reverse proxying with proxy_pass, response caching, gzip compression, and serving static files. Scoped to nginx; not a general web server corpus.","visibility":"public","version":1,"documentCount":5,"publisher":"Koskamo editorial","license":"Original summaries; linked sources retain their own terms.","reviewedAt":"2026-10-04T00:00:00.000Z","readOnly":true,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},"total":5,"documents":[{"title":"Serving static files with root and location","sourceUrl":"https://nginx.org/en/docs/http/ngx_http_core_module.html","text":"The root directive, valid in the http, server, location, and if in location contexts with a default of html, sets the root directory for requests: the file path is built by merely adding the request URI to the root value, so location /i/ { root /data/w3; } sends /data/w3/i/top.gif in response to a /i/top.gif request. The value can contain variables except $document_root and $realpath_root; when the URI has to be modified rather than appended, the alias directive should be used instead. location blocks match a normalized URI — after decoding text encoded in the %XX form, resolving references to relative path components . and .., and compressing adjacent slashes — using a prefix string, a regular expression introduced by ~ for case-sensitive or ~* for case-insensitive matching, or, since 1.31.5, a predicate variable. nginx first selects the longest matching prefix location, then checks regular expressions in the order they appear and stops at the first match, checking predicate locations only when no regular expression matched. Keeping specific prefix locations ahead of broad ones so routing stays predictable is Koskamo editorial guidance. Scope: nginx. Reviewed October 4, 2026.","id":"nginx-best-practices:Serving static files with root and location","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"gzip compression of responses","sourceUrl":"https://nginx.org/en/docs/http/ngx_http_gzip_module.html","text":"Response compression is controlled in the http, server, location, or if in location context. gzip on enables gzipping of responses, off by default; gzip_comp_level sets a compression level from 1 to 9 with a default of 1; gzip_min_length, default 20, sets the minimum response length to compress, determined only from the Content-Length response header. gzip_types names additional MIME types to compress beyond text/html — responses of that type are always compressed — and the special value * matches any MIME type since 0.8.29; restricting gzip_types to textual types, since compressing already-compressed formats gains little, is Koskamo editorial guidance. gzip_proxied, off by default, decides whether responses to proxied requests are compressed depending on the request and response. gzip_disable, available since 0.6.23, skips gzipping for requests whose User-Agent matches given regular expressions, with the special mask msie6 since 0.7.12. gzip_vary on inserts the Vary: Accept-Encoding response header when compression is active, and the $gzip_ratio variable reports the achieved compression ratio. Scope: nginx. Reviewed October 4, 2026.","id":"nginx-best-practices:gzip compression of responses","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Response caching with proxy_cache","sourceUrl":"https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache","text":"Response caching for proxied content is enabled in the http, server, or location context. proxy_cache_path, valid in http, sets the on-disk cache path and parameters: cached data is stored in files whose names are the result of applying the MD5 function to the cache key, the levels parameter defines a hierarchy of one to three levels where each level accepts the values 1 or 2, and keys_zone allocates a shared memory zone that can be used in several places, as in proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m. A cached response is first written to a temporary file and then renamed; if temporary files and the cache sit on different file systems, the file is copied across them instead of the cheap renaming operation. proxy_cache zone activates caching and the off parameter disables caching inherited from the previous configuration level. proxy_cache_bypass defines conditions under which the response is not taken from the cache when any parameter value is non-empty and not 0. Since 1.11.10, proxy_cache_background_update refreshes an expired item in a background subrequest while the stale response is returned to the client, which requires stale usage to be allowed explicitly. Scope: nginx. Reviewed October 4, 2026.","id":"nginx-best-practices:Response caching with proxy_cache","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Reverse proxying with proxy_pass","sourceUrl":"https://nginx.org/en/docs/http/ngx_http_proxy_module.html","text":"The proxy_pass directive, valid in a location, if in location, or limit_except context, sets the protocol and address of a proxied server and an optional URI to which the location is mapped. The protocol is http or https; the address is a domain name or IP address with an optional port, a server group, or a UNIX-domain socket path written after the word unix and enclosed in colons. If a domain name resolves to several addresses, all of them are used in a round-robin fashion. The value can contain variables; in that case a domain name is first searched among the described server groups and, if not found, determined using a resolver. URI handling depends on whether proxy_pass carries a URI: with one, the part of the normalized request URI matching the location is replaced by the directive's URI; without one, the request URI is passed to the server in the same form as sent by the client, or the full normalized request URI when a changed URI is processed. Before version 1.1.12, the original request URI might have been passed instead of the changed URI in some cases. Scope: nginx. Reviewed October 4, 2026.","id":"nginx-best-practices:Reverse proxying with proxy_pass","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Configuration structure and server blocks","sourceUrl":"https://nginx.org/en/docs/beginners_guide.html","text":"nginx runs one master process, which reads and evaluates configuration and maintains worker processes, and several worker processes that actually process requests using an event-based model. Configuration lives in a file named nginx.conf by default, placed in /usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx. nginx consists of modules controlled by directives: a simple directive is a name and parameters ending with a semicolon, while a block directive ends with braces and, when it can hold other directives inside, is called a context — events, http, server, and location are examples. Directives outside any context belong to the main context; events and http reside in main, server in http, and location in server. The rest of a line after the # sign is a comment. A configuration can include several server blocks distinguished by the ports they listen on and by server names; once nginx decides which server processes a request, it tests the request URI against that server's location directives and selects the one with the longest matching prefix. Scope: nginx. Reviewed October 4, 2026.","id":"nginx-best-practices:Configuration structure and server blocks","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"}]}