Master Nginx Upstream Load Balancing and Caching: Step‑by‑Step Guide
This tutorial walks through configuring Nginx upstream for load balancing and enabling proxy caching of static resources, covering experiment topology, environment setup, key upstream parameters, example configurations, cache directives, and performance observations.
Outline
Introduction
Experiment Topology
Experiment Environment
Nginx Upstream Module Introduction
Nginx Cache Introduction
Conclusion
Introduction
This article continues the previous Nginx series, demonstrating how to use Nginx as a load balancer and cache. The experiment does not cover all scenarios such as data synchronization between web servers.
Experiment Topology
Experiment Environment
Host IP Address Function lb.anyisalin.com 172.16.1.2 Load balancer and cache static resources web1.anyisalin.com 172.16.1.3 Provide web service web2.anyisalin.com 172.16.1.4 Provide web service
Note: All operations are performed with SELinux and iptables disabled
Nginx Upstream Module Introduction
The ngx_http_upstream_module module is used to define groups of servers that can be referenced by the proxy_pass , fastcgi_pass , uwsgi_pass , scgi_pass , and memcached_pass directives. (Excerpt from official documentation)
In short, the upstream module defines a server group that can be combined with other proxy modules to achieve load balancing.
How to Use Upstream
Below is a simple upstream definition that provides basic load‑balancing functionality.
upstream servers {
server 172.16.1.2;
server 172.16.1.3;
}
location / {
proxy_pass http://servers;
}Common upstream parameters
upstream name {
[ip_hash]
server address [weight=number] [max_fails=number] [fail_timeout=number] [down|up|backup];
...
}
# ip_hash: similar to LVS's sh algorithm, directs the same IP to the same host
# weight: weight setting
# max_fails: number of failed connections before marking a server down
# fail_timeout: timeout considered as failure
# down: take server offline
# backup: set server as backup (used only when primary servers are down)Upstream Usage Example
Installation of nginx is omitted; see the previous blog for details.# Add to nginx main configuration
http {
upstream servers {
server 172.16.1.3;
server 172.16.1.4;
}
}
server {
location / {
proxy_pass http://servers;
}
}
# Reload nginxSee the image; we achieved a simple load‑balancing effect using different pages.
Nginx Cache Introduction
Cache is king; the Nginx proxy module supports caching static resources, reducing server load and speeding up responses.
Caching Static Resources
proxy_cache_path /cache levels=1:1 keys_zone=mycache:64m; # http block
location /index.html { # location block
proxy_pass http://172.16.1.3;
proxy_cache mycache; # use mycache
proxy_cache_valid 200 1d; # cache 200 responses for 1 day
proxy_cache_valid 500 501 502 503 1m; # cache error responses for 1 minute
proxy_cache_use_stale error; # use stale cache if origin fails
}
mkdir /cache
chown nginx:nginx /cacheTesting shows that caching significantly improves the response time of static resources.Conclusion
Due to time constraints, this article briefly introduced Nginx upstream and cache usage; many configuration parameters were not detailed. Future posts will cover them in depth, and the Nginx series will continue—stay tuned!
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
