Nginx Quick Reference
This reference collects the commands and directives you reach for most often when operating Nginx day to day: testing and reloading configuration, the core structure of the config file, common location and proxy patterns, and log/debugging basics. It's intended as a fast lookup rather than a first introduction — each item is deliberately terse, assuming you already understand roughly what Nginx does and just need the exact syntax at hand.
Cricket analogy: A quick reference sheet is like a fielding cheat card taped to the boundary rope, listing each player's role at a glance rather than re-explaining the whole game plan.
Essential Commands
nginx -t tests the configuration syntax without applying it and should be run before every reload; nginx -s reload gracefully reloads config by spawning new workers and letting old ones finish in-flight requests; nginx -s stop performs an immediate shutdown while nginx -s quit performs a graceful shutdown that waits for current requests. On systemd-based systems, systemctl reload nginx and systemctl status nginx are the more common day-to-day equivalents, and journalctl -u nginx surfaces startup errors that don't appear in the access or error log, particularly config errors that prevent Nginx from starting at all.
Cricket analogy: nginx -t validating config before applying it is like a third umpire checking replay evidence before confirming a decision, never committing until the check passes.
Core Config Structure and Common Directives
Nginx config nests in blocks: http { server { location { ... } } }, with directives inheriting downward unless overridden. Common top-level directives include worker_processes, events { worker_connections }, and within http, sendfile on, keepalive_timeout, gzip on, and include /etc/nginx/conf.d/*.conf or sites-enabled/*. Within a server block, listen, server_name, root, and index set the basics, while a location block matches request paths using prefix strings, the = modifier for exact match, and ~ / ~* for case-sensitive/insensitive regex matches respectively.
Cricket analogy: Nested config blocks inheriting downward is like a team's overall strategy setting the tone, with each fielding position inheriting the general plan unless the captain gives specific instructions.
Directive precedence within a location block generally follows the most specific match: an exact match (=) beats a longer literal prefix, which beats a shorter literal prefix, which is overridden by the first matching regex (~ or ~*) evaluated in file order, unless the exact match already short-circuited the search.
Logging, Debugging, and Redirects
access_log and error_log directives point to log files (or off to disable); the default error_log level is error, but setting it to debug (requires a debug build) surfaces far more detail when diagnosing a tricky config issue. For redirects, return 301 https://$host$request_uri; inside a port-80 server block is the standard HTTP-to-HTTPS redirect pattern, while rewrite ^/old-path$ /new-path permanent; achieves a similar result for path-level redirects but processes through the rewrite engine rather than short-circuiting immediately like return.
Cricket analogy: Setting error_log to debug for a tricky issue is like reviewing every camera angle available for a contentious decision instead of relying on just the standard broadcast feed.
Leaving error_log set to debug in production generates very large log volumes quickly and can itself become a performance and disk-space problem — only enable it temporarily while actively diagnosing an issue, and revert to warn or error afterward.
# Commands
nginx -t # test config syntax
nginx -s reload # graceful reload
systemctl status nginx # service status
journalctl -u nginx -n 50 # recent startup/service logs
# Common location patterns
# location = /exact exact match, highest precedence
# location /prefix/ longest literal prefix match
# location ~ \.php$ case-sensitive regex
# location ~* \.(jpg|png)$ case-insensitive regex
# HTTP -> HTTPS redirect
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}- nginx -t validates config syntax; nginx -s reload applies changes gracefully without dropping connections.
- Config nests as http { server { location { ... } } }, with most directives inheriting downward unless overridden.
- Location matching precedence: exact (=), longest literal prefix, first matching regex (~/~*) in file order, then fallback prefix.
- return 301 issues an immediate redirect; rewrite modifies the URI and continues processing through the config.
- access_log and error_log control logging destinations and verbosity; debug-level logging should be temporary only.
- systemctl status nginx and journalctl -u nginx surface service-level and startup errors not found in access/error logs.
- Common redirect pattern: return 301 https://$host$request_uri; inside a port-80 server block for HTTPS enforcement.
Practice what you learned
1. What should you run before applying a configuration change with nginx -s reload?
2. Which command performs an immediate (non-graceful) shutdown of Nginx?
3. What is the standard directive pattern for redirecting HTTP to HTTPS?
4. Where would you look to diagnose an Nginx startup failure that doesn't appear in the access or error log?
5. Which location modifier specifies a case-insensitive regular expression match?
Was this page helpful?
You May Also Like
Nginx vs Apache
A practical comparison of Nginx's event-driven architecture against Apache's process-based model, covering configuration philosophy, performance under load, and when to choose or combine each.
Nginx Interview Questions
A curated set of Nginx interview questions covering architecture, configuration, reverse proxying, performance, and troubleshooting, with model answers.
Deploying a Web App Behind Nginx
A practical walkthrough of putting Nginx in front of an application server: reverse proxying, TLS termination, static asset serving, and process management.
Related Reading
Related Study Notes in DevOps
Browse all study notesAnsible Study Notes
DevOps · 30 topics
DevOpsAdvanced Kubernetes Study Notes
Kubernetes · 30 topics
DevOpsAdvanced Bash Scripting Study Notes
Bash · 30 topics
DevOpsApache Kafka Study Notes
Kafka · 30 topics
DevOpsDocker & Kubernetes Study Notes
YAML · 40 topics
DevOpsCI/CD Tools & Pipelines Study Notes
YAML · 37 topics