Nginx use cases that every engineer must know
Last updated
Last updated
nginx use cases
Nginx is one of the most influential and versatile web servers, widely used for handling high-performance applications. Initially designed as a web server, it has evolved into a multi-functional tool used for various engineering tasks. Whether you’re a DevOps engineer, a site reliability engineer, or a backend developer, understanding Nginx’s capabilities can significantly enhance your infrastructure.
In this article, we’ll explore the unique and essential use cases of Nginx that every engineer must know.
This configuration serves static files from /var/www/html
and forwards PHP requests to a backend.
Listens on port 80 for requests to example.com
.
Serves files from the /var/www/html
directory.
If a user visits the site, Nginx will look for index.html
, index.htm
, or index.php
as the default page.
Use this setup if you are hosting a PHP-based website like WordPress.
This configuration forwards requests to a backend application running on a port 5000
.
This setup distributes traffic among three backend servers.
Nginx uses Below load-balancing method
Round Robin (Default)
Requests are distributed sequentially across all available servers.
Forwards requests to one of the backend servers in a round-robin manner.
Each backend (192.168.1.101, 192.168.1.102, 192.168.1.103) must have Nginx installed and serving content.
upstream
block defines a group of backend servers that can be used for load balancing and proxying requests.
👉 How Round Robin Works?
1st request → 192.168.1.101
2nd request → 192.168.1.102
3rd request → 192.168.1.103
4th request → 192.168.1.101
(cycle repeats)
A sticky session (also called session persistence) ensures that a user’s requests always go to the same backend server during their session. This is useful when backend servers store session-specific data (e.g., user authentication, shopping carts).
Without sticky sessions, requests from a single user might be routed to different backend servers, leading to inconsistent session behavior.
✅ required when:
Your backend servers store session-specific data (e.g., user authentication).
You cannot use a shared session storage (like Redis or a database).
🚫 Not needed when:
Your app stores sessions in a centralized database (e.g., PostgreSQL, MySQL).
You use distributed session storage (e.g., Redis, Memcached).
For scalability, it’s better to store sessions in Redis instead of enabling sticky sessions.
1. Path-Based Routing
Route different paths to different backends.
2. Host-Based Routing
Route based on domain name.
3. Header-Based Routing
Route based on HTTP headers (e.g., User-Agent).
4. Load Balancing Routing
Distribute traffic across multiple servers.
5. Geolocation Routing
Route users based on country.
6. Cookie-Based Routing
A/B testing, feature rollouts.
7. Rate-Based Routing
Limit API calls to prevent abuse.
8. Query Parameter Routing
Route based on URL parameters.
9. File Extension-Based Routing
Serve static files separately.
Nginx is more than just a web server — it’s a powerful tool for engineers handling complex infrastructure needs. Whether you’re optimizing performance, securing applications, or managing API traffic, mastering these use cases will help you build scalable and resilient systems.
Do you use Nginx in any unique way? Let me know in the comments! 🚀