Deep Dive on NGINX Directives
NGINX directives—from the basic to the advanced—with practical examples, recommended default values, and production tips.
Last updated
NGINX directives—from the basic to the advanced—with practical examples, recommended default values, and production tips.
Last updated
NGINX’s flexibility comes from its rich set of configuration directives. In production, understanding these directives can help fine-tune performance, security, and resource management.
Purpose: Defines the number of worker processes. , the default value is 1.
Example:
Production Tip: Use auto, as this ensures efficient utilization of system resources to match available CPU cores.
Purpose: Specifies the file where errors are logged.
Example:
Default/Recommended: Default is usually error.log
in the installation directory; using a log level like warn
or error
is ideal for production. Use debug for issues.
Purpose: Allows you to include additional configuration files.
Example:
Production Tip: Modularize configurations for maintainability.
Purpose: Sets the maximum number of simultaneous connections per worker.
Example:
Default/Recommended: Default is often 512 or 1024; adjust based on traffic.
Production Tip: A higher value can help handle heavy loads, but ensure the OS limits are adjusted accordingly.
Purpose: Allows a worker to accept all new connections at once.
Example:
Production Tip: Helps reduce latency under high connection rates.
Purpose: Include MIME types and other HTTP-level configs.
Example:
Production Tip: Keep MIME types updated to ensure proper file delivery.
Purpose: Optimize file transfers.
Example:
Production Tip: These settings minimize latency and maximize throughput for static files.
Purpose: Defines the timeout for persistent connections.
Example:
Default/Recommended: The default is usually 75 seconds; 65 seconds is a common production setting.
Purpose: Limits the size of client request bodies.
Example:
Production Tip: Set a sensible limit based on your application’s needs to mitigate DoS risks.
Purpose: Bind virtual hosts to specific IPs/ports and domain names.
Example:
Production Tip: Use precise names and ports to avoid ambiguity in multi-tenant environments.
Purpose: Define where static files are served from.
Example:
Production Tip: Ensure file paths match your deployment structure to prevent 404s.
Purpose: Customize responses for error codes.
Example:
Production Tip: Custom error pages enhance user experience and can hide sensitive server information.
Purpose: Handle requests based on URI patterns.
Example:
Production Tip: Combine exact (location =
), prefix, and regex-based locations carefully to avoid unexpected matches.
Purpose: Change request URIs using regex.
Example:
Production Tip: Use permanent redirects (301
) only when URLs have permanently changed.
Purpose: Check for the existence of files before proxying or handling errors.
Example:
Production Tip: Use try_files
to efficiently serve static content and reduce backend load.
Purpose: Forward requests to a backend server or upstream group.
Example:
Production Tip: Ensure you correctly forward headers using additional directives (e.g., proxy_set_header
) for proper client IP logging and security.
Purpose: Define groups of backend servers for load balancing.
Example:
Production Tip: Configure health checks and use weighted balancing to distribute load evenly.
Purpose: Modify or add headers when proxying.
Example:
Production Tip: Proper header forwarding is crucial for security and logging.
Purpose: Modify the “Location” header in responses from proxied servers.
Example:
Production Tip: Adjust proxy redirects to maintain client-side URL consistency.
Purpose: Forward requests to FastCGI application servers.
Example:
Production Tip: Ensure file paths and socket settings are correct; secure your FastCGI interface to prevent unauthorized access.
Purpose: Configure caching for proxied content.
Example:
Production Tip: Monitor cache performance and configure proper expiration policies.
Purpose: Enable response compression.
Example:
Production Tip: Tweak gzip_min_length
and gzip_types
to balance CPU usage and bandwidth savings.
ssl_certificate and ssl_certificate_key
Purpose: Define the SSL certificate and key for secure connections.
Example:
Production Tip: Use automated certificate renewal and strong ciphers.
Purpose: Restrict access based on IP addresses.
Example:
Production Tip: Implement IP whitelisting for sensitive resources.
Purpose: Enable HTTP basic authentication.
Example:
Production Tip: For stronger security, integrate with external authentication providers.
Purpose: Specify DNS servers for resolving domain names (useful for dynamic or upstream configurations).
Example:
Default/Recommended: If not set, NGINX falls back to the system resolver. In production, explicitly setting resolvers (for example, Google DNS) improves reliability when using domain names in upstream blocks.
Production Tip: Use a short resolver_timeout
to avoid delays, and update the DNS list as needed.
Purpose: Define timeouts for reading client request bodies and headers.
Example:
Production Tip: Shorten timeouts to mitigate slow client attacks while avoiding premature termination for legitimate users.