Backend Development 9 min read

Comprehensive Guide to Nginx Optimization, Modules, Load‑Balancing Algorithms, Log Management, Custom Error Pages, and Comparison with Apache and LVS

This article presents a detailed overview of Nginx performance optimizations, module types, load‑balancing algorithms, log rotation scripting, custom error page setup, smooth restart procedures, configuration checks, and a comparative analysis with Apache and LVS for backend server administrators.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Comprehensive Guide to Nginx Optimization, Modules, Load‑Balancing Algorithms, Log Management, Custom Error Pages, and Comparison with Apache and LVS

1. Nginx Optimization – Common optimization techniques include hiding version information, modifying source code, changing the default user, lowering privileges, tuning worker processes, CPU affinity, event model tuning, adjusting max client connections, file descriptor limits, enabling efficient file transfer, gzip compression, log rotation scripts, selective logging, log permission settings, restricting access by file extensions, blocking directories, IP‑based access control, illegal domain blocking, anti‑scraping measures, and controlling concurrent connections.

2. Nginx Modules – Modules are classified structurally into core modules (HTTP, EVENT, MAIL), basic modules (HTTPAccess, FastCGI, Proxy, Rewrite), and third‑party modules (Upstream Hash, Notice, Access Key). Functionally they are Handles (process requests), Filters (modify output), and Proxies (interact with backend services).

3. Load‑Balancing Algorithms – Nginx supports several algorithms: round‑robin (default), weight‑based round‑robin, ip_hash (hash by client IP), fair (third‑party, based on response time), and url_hash (third‑party, based on request URL). Each algorithm distributes traffic according to different criteria.

4. Log Rotation Script – The following shell script moves current logs, renames them with a date stamp, moves them to an archive directory, signals the Nginx master process to reopen logs, and optionally deletes logs older than 90 days: #!/bin/sh set -x logs_path="/usr/local/nginx/logs" oldlogs_path="/nginx_oldlogs/" for i in `ls $logs_path/* |grep -v nginx.pid |grep -v oldlogs` do mv "$i" "$i"_${HOS}_$(date -d "yesterday" +"%Y%m%d") mv "$i"_${HOS}_$(date -d "yesterday" +"%Y%m%d") $oldlogs_path done nginx_pid=`ps -ef |grep nginx|grep master|awk '{print $1}'` kill -USR1 $nginx_pid /usr/local/nginx/sbin/nginx -t && /usr/local/nginx/sbin/nginx -s reload sleep 2 #docker exec -it tengine /usr/local/nginx/sbin/nginx -s reopen #sleep 1 #clear the expired logs cd $oldlogs_path #find ./ -mtime +90 -exec rm -rf {} \; echo "The old logfiles is deleted!!"

5. Custom 403 Error Page – Add fastcgi_intercept_errors on; under fastcgi_temp_file_write_size 128k; in nginx.conf , then define error_page 403 = /403.htm; in the server block and place the 403.htm file in the site root. Test with /usr/local/nginx/sbin/nginx -t and restart if no errors.

6. Smooth Restart – Verify configuration syntax before restarting using /user/local/webserver/nginx/sbin/nginx –t –c /user/local/webserver/nginx/conf/nginx.conf . Perform a graceful reload with kill –HUP $(cat /user/local/webserver/nginx/logs/nginx.pid) .

7. Configuration Check Commands – The nginx -t command tests the configuration file for syntax errors, which is essential before reloading.

8. Nginx vs. Apache – Nginx is lightweight, uses asynchronous non‑blocking architecture, consumes less memory, and offers modular design, while Apache provides powerful rewrite capabilities, a vast module ecosystem, and high stability. Apache’s bug count is generally lower, but Nginx excels in high‑concurrency scenarios.

9. Nginx vs. LVS – LVS operates at the IP layer, offering strong load capacity, high reliability, and broad application support but lacks regex handling. Nginx works at layer 7, supports regex‑based routing, simple configuration, and handles HTTP/SMTP with algorithms like RR, weight, ip_hash, and fair. LVS algorithms include rr, wrr, lc, and wlc.

OptimizationLoad BalancingconfigurationNginxlog rotationbackend server
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.