Mastering Nginx: Reverse Proxy, Load Balancing, and High Availability Explained
This comprehensive guide introduces Nginx’s high‑performance architecture, explains forward and reverse proxy concepts, demonstrates load‑balancing and static‑dynamic content separation, provides practical configuration commands, and walks through real‑world setups for reverse proxy, load‑balancing, static‑dynamic separation, and high‑availability using Keepalived.
Nginx is a high‑performance HTTP and reverse‑proxy server known for low memory usage and strong concurrency capabilities.
Designed for performance optimization, Nginx can handle up to 50,000 concurrent connections.
Nginx Knowledge Map
Reverse Proxy
Forward Proxy : In a LAN, users cannot directly access the Internet and must go through a forward proxy server.
Reverse proxy: Clients are unaware of the proxy; they send requests to the reverse‑proxy server, which forwards them to target servers and returns the responses, hiding the real server IPs.
Load Balancing
Clients send multiple requests to servers; servers may interact with databases and return results.
Typical request/response flow:
As traffic grows, a single server becomes insufficient. Scaling vertically is limited, so we add more servers and distribute requests—load balancing.
Example: 15 requests are evenly distributed to three servers, each handling five requests.
Static‑Dynamic Separation
Separating static and dynamic content across different servers speeds up page rendering and reduces load on any single server.
Before separation:
After separation:
Installation
Reference link: https://blog.csdn.net/yujing1314/article/details/97267369
Nginx Common Commands
<code>./nginx -v</code> <code>./nginx</code> <code>./nginx -s stop</code> <code>./nginx -s quit</code> <code>./nginx -s reload</code>Nginx Configuration File
The configuration file consists of three main blocks:
1. Global Block
Settings that affect the entire Nginx server, including worker processes and overall concurrency.
2. Events Block
Controls network connections, such as connection serialization and maximum connections.
3. HTTP Block
Contains directives for reverse proxy, load balancing, etc.
<code>location [ = | ~ | ~* | ^~ ] url { ... }</code>=: exact match, stop searching.
~: case‑sensitive regex.
~*: case‑insensitive regex.
^~: highest priority prefix match.
Reverse Proxy Practice
1) Configure reverse proxy to map
www.123.comto a local Tomcat on port 8080.
2) Example mapping
http://192.168.25.132:9001/edu/→
192.168.25.132:8080and
/vod/→
8081using regex location rules.
Load Balancing Practice
Modify
nginx.confto define upstream servers and load‑balancing method, then reload Nginx.
<code>./nginx -s reload</code>Supported methods:
Round‑robin (default)
Weight
Fair (based on response time)
IP‑hash (session persistence)
Static‑Dynamic Separation Practice
Configure Nginx to serve static files directly while proxying dynamic requests to Tomcat.
Nginx High Availability
Use two Nginx instances with Keepalived and a virtual IP to ensure failover.
Keepalived Installation
<code># yum install keepalived -y</code> <code># rpm -q -a keepalived</code>Configuration Example
<code>global_defs {</code><pre><code> notification_email {</code><pre><code> [email protected]</code><pre><code> [email protected]</code><pre><code> [email protected]</code><pre><code> }</code><pre><code> notification_email_from [email protected]</code><pre><code> smtp_server 192.168.25.147</code><pre><code> smtp_connect_timeout 30</code><pre><code> router_id LVS_DEVEL</code><pre><code>}</code><pre><code>vrrp_script chk_nginx {</code><pre><code> script "/usr/local/src/nginx_check.sh"</code><pre><code> interval 2</code><pre><code> weight 2</code><pre><code>}</code><pre><code>vrrp_instance VI_1 {</code><pre><code> state BACKUP</code><pre><code> interface ens33</code><pre><code> virtual_router_id 51</code><pre><code> priority 90</code><pre><code> advert_int 1</code><pre><code> authentication {</code><pre><code> auth_type PASS</code><pre><code> auth_pass 1111</code><pre><code> }</code><pre><code> virtual_ipaddress {</code><pre><code> 192.168.25.50</code><pre><code> }</code><pre><code>}</code>Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
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.