Detailed Nginx Configuration Parameters and Multi‑Server Load Balancing Guide
This article provides a comprehensive walkthrough of Nginx configuration directives, explains key parameters such as worker processes, logging, gzip, and demonstrates how to set up multi‑node load balancing with various upstream algorithms using Docker containers and host file adjustments.
The document begins with a complete list of Nginx configuration parameters, describing the purpose of each directive (e.g., user www www;, worker_processes 8;, error_log /var/log/nginx/error.log info;, worker_rlimit_nofile 65535;) and detailing the events and http blocks, including settings for connection limits, file handling, gzip compression, FastCGI timeouts, and log formats.
It then explains how to configure virtual hosts, SSL certificates, and location blocks for PHP processing, static file caching, and proxying, providing example snippets for both HTTP and HTTPS server definitions.
Following the basic configuration, the article introduces Nginx load balancing across multiple backend servers. It lists the IP of the load‑balancing server (192.168.0.4) and two web nodes (192.168.0.5 and 192.168.0.7), and describes the goal of distributing client requests to these nodes.
Four upstream scheduling algorithms are presented with commented code examples:
# Round‑robin (default)
upstream webhost {
server 192.168.0.5:6666;
server 192.168.0.7:6666;
}
# Weighted round‑robin
upstream webhost {
server 192.168.0.5:6666 weight=2;
server 192.168.0.7:6666 weight=3;
}
# ip_hash (session affinity)
upstream webhost {
ip_hash;
server 192.168.0.5:6666;
server 192.168.0.7:6666;
}
# url_hash (requires third‑party module)
upstream webhost {
server 192.168.0.5:6666;
server 192.168.0.7:6666;
hash $request_uri;
}A sample server block using the ip_hash algorithm is shown, with proxy settings such as proxy_pass http://webhost;, header forwarding, client body limits, and timeout values.
Practical Docker commands are provided to create the load‑balancing server and the two web nodes, mount configuration and HTML directories, and expose the required ports. Example commands include:
# Create configuration directory and edit nginx.conf
mkdir -p /opt/confs
vim /opt/confs/nginx.conf
# Start the load‑balancing container
docker run -d -p 8888:80 --name nginx-server -v /opt/confs/nginx.conf:/etc/nginx/nginx.conf --restart always nginx
# Create web page directory for node 1
mkdir -p /opt/html
vim /opt/html/index.html # add simple HTML content
docker run -d -p 6666:80 --name nginx-node1 -v /opt/html:/usr/share/nginx/html --restart always nginx
# Repeat for node 2 (adjust volume path as needed)To test the setup, the article advises adding an entry xxx.com 192.168.0.4 to the Windows hosts file (C:\Windows\System32\drivers\etc) and accessing http://xxx.com in a browser; Nginx will route requests to the backend nodes based on the chosen hash algorithm, automatically handling node failures.
Additional demo configurations with different container IPs (192.168.2.129, 192.168.2.56, 192.168.2.77) and a screenshot of the access results are included to illustrate the expected behavior.
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.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
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.
