Comprehensive Nginx Tutorial: Installation, Configuration, Reverse Proxy, Load Balancing, and High Availability
This guide provides a step‑by‑step tutorial on Nginx, covering its overview, single‑instance installation, reverse proxy setup, load‑balancing configuration, static‑dynamic separation, high‑availability clustering with keepalived, detailed configuration directives, and performance considerations, complete with command‑line examples and code snippets.
This article presents a complete walkthrough of Nginx, starting with an overview of its high performance HTTP and reverse‑proxy capabilities and listing major users such as Baidu, Tencent, and Alibaba.
Installation : It demonstrates preparing a CentOS 6.10 VM, installing required packages, downloading Nginx 1.18.0, extracting, configuring, compiling, and installing to /usr/local/nginx. Example commands include:
# yum install -y gcc gcc-c++ make libtool wget pcre pcre-devel zlib zlib-devel openssl openssl-devel # wget http://nginx.org/download/nginx-1.18.0.tar.gz # tar -zxvf nginx-1.18.0.tar.gz # cd nginx-1.18.0 && ./configure && make && make installAfter installation, the service can be started with /usr/local/nginx/sbin/nginx and stopped with /usr/local/nginx/sbin/nginx -s quit. Firewall rules are opened using iptables to allow port 80.
Reverse Proxy : The guide shows how to configure location / blocks to proxy requests to Tomcat instances, including necessary proxy_set_header directives to preserve client IP and host information.
Load Balancing : An upstream block named myserver is defined with two backend servers (ports 8080 and 8081). The server block proxies traffic to this upstream, demonstrating round‑robin distribution and explaining alternative strategies such as weight, ip_hash, and fair.
Static/Dynamic Separation : By adding a location /www/ block with root /data/, static files are served directly by Nginx while dynamic content continues to be handled by Tomcat.
High Availability : The article introduces keepalived for VRRP‑based failover. It provides a sample /etc/keepalived/keepalived.conf configuration, a health‑check script /etc/keepalived/nginx_check.sh, and steps to start the keepalived service, ensuring a virtual IP remains reachable if the primary Nginx node fails.
Configuration Details : It explains the global, events, http, server, and location blocks, covering directives such as worker_processes, worker_connections, sendfile, gzip, and SSL settings. The syntax and wildcard behavior of the location directive (=, ~, ~*, ^~) are also described.
Nginx Architecture : The master‑worker model is illustrated, noting that the master process manages workers and configuration reloads, while workers handle connections using the epoll event model. Calculations for maximum concurrent connections are provided for both static serving and reverse‑proxy scenarios.
Overall, the tutorial equips readers with practical commands, configuration examples, and architectural insights needed to deploy, tune, and maintain a robust Nginx infrastructure.
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.
Architect
Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.
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.
