Nginx Explained: Essential Configuration Directives and Best Practices
Introduction
Nginx is one of the most popular web servers and reverse proxies used to serve websites, APIs, and web applications. It is known for its high performance, low memory usage, and ability to handle thousands of concurrent connections efficiently.
Whether you are hosting a PHP application, a static website, or acting as a reverse proxy for backend services, understanding the core Nginx configuration directives is essential for building a fast, reliable, and secure server.
This guide introduces the most important Nginx configuration directives, explains how they work together, and provides recommended practices for development and production environments.
Test Environment
What Is Nginx?
Nginx is an open-source web server that can also function as:
Reverse proxy
Load balancer
HTTP cache
API gateway
Static file server
Unlike traditional process-based web servers, Nginx uses an event-driven architecture that allows a small number of worker processes to efficiently handle many simultaneous client connections.
How Nginx Processes Requests
For PHP applications, Nginx forwards PHP requests to PHP-FPM using the FastCGI protocol, while serving static files such as images, CSS, and JavaScript directly.
Where Is the Nginx Configuration File?
Common configuration files include:
/etc/nginx/nginx.conf
Virtual host configuration:
/etc/nginx/conf.d/
or
/etc/nginx/sites-enabled/
depending on your Linux distribution.
Test the Configuration
Before restarting Nginx, always validate the configuration.
sudo nginx -t
Example output:
nginx: the configuration file syntax is ok
nginx: configuration file test is successful
Reload the Configuration
After modifying the configuration:
sudo systemctl reload nginx
If Nginx is not running:
sudo systemctl restart nginx
Verify the service:
sudo systemctl status nginx
Expected output:
Active: active (running)
Essential Nginx Directives
The following directives are among the most commonly configured for production servers.
Recommended Configuration
This provides a solid starting point for many production environments. Depending on your workload, you may need to adjust connection limits, upload sizes, compression settings, and timeout values.
How Nginx and PHP-FPM Work Together
This architecture allows Nginx to efficiently serve static content while delegating PHP execution to PHP-FPM.
Common Issues
413 Request Entity Too Large
Usually caused by:
client_max_body_size
being set too low.
504 Gateway Timeout
Often related to:
fastcgi_read_timeout
PHP-FPM
Long-running PHP scripts
Too Many Open Files
Possible causes include:
Low worker_connections
Operating system file descriptor limits
High concurrent traffic
High CPU Usage
Common reasons include:
Compression of large responses.
Poor application performance.
Excessive logging.
Slow upstream services.
Best Practices
Validate the configuration with nginx -t before reloading.
Use worker_processes auto unless you have a specific tuning requirement.
Enable sendfile for efficient static file delivery.
Configure gzip for text-based content.
Set client_max_body_size according to your application's upload requirements.
Monitor access and error logs to identify bottlenecks.
Tune Nginx together with PHP-FPM and OPcache for the best overall performance.
Related Articles
This article introduces the most important Nginx directives. For detailed explanations of each setting, see:
Nginx worker_processes Explained
Nginx worker_connections Explained
Nginx keepalive_timeout Explained
Nginx client_max_body_size Explained
Nginx fastcgi_read_timeout Explained
Nginx proxy_read_timeout Explained
Conclusion
Nginx is a high-performance web server that forms the foundation of many modern web applications. Its event-driven architecture enables efficient handling of concurrent connections while working seamlessly with PHP-FPM, reverse proxies, and load balancers.
Understanding key directives such as worker_processes, worker_connections, client_max_body_size, sendfile, and gzip allows administrators to build fast, reliable, and scalable web servers. Rather than tuning individual settings in isolation, the best results come from optimizing Nginx together with PHP-FPM and OPcache as part of a complete server performance strategy.
Explore More
› MariaDB Explained: Essential Configuration Variables and Best Practices
› Nginx worker_processes Explained: Configuration, Performance, and Best Practices
› Nginx worker_connections Explained: Configuration, Limits, and Best Practices
› Nginx keepalive_timeout Explained: Configuration, Performance, and Best Practices
› Best Areas to Live in Bangkok in 2026: A Complete Guide for Expats and Digital Nomads
› Best Coworking Spaces in Bangkok 2026: A Guide for Digital Nomads and Remote Workers
› Why Do People in Southeast Asia Love Iced Drinks? The Climate, Culture, and Science Explained
› Best eSIM for Southeast Asia in 2026: Compare the Top Travel eSIMs