Boost Nginx Performance: Custom 404 Pages, Status Monitoring, and Concurrency Tuning
This guide explains how to optimize Nginx by customizing 404 error pages, enabling and reading the stub_status module, increasing worker processes and connections, adjusting Linux file limits, expanding header buffers, and configuring browser caching for static assets, all with concrete command examples.
Nginx is a high‑performance web and reverse‑proxy server widely used by major Chinese companies such as Taobao, JD, Baidu, Sina, NetEase, and Tencent.
1. Custom 404 error page
Before optimization, accessing a non‑existent page shows the default 404 message. # firefox http://192.168.4.5/xxxxx After editing /usr/local/nginx/conf/nginx.conf to add error_page 404 /404.html; and creating a custom 404.html, reload Nginx.
# vim /usr/local/nginx/conf/nginx.conf
... error_page 404 /404.html;
# vim /usr/local/nginx/html/404.html
... (custom content)
# nginx -s reloadNow the client sees the defined 404 page.
2. View server status information
Enable the stub_status module during compilation with --with-http_stub_status_module and add a location block:
# tar -zxvf nginx-1.12.2.tar.gz
# cd nginx-1.12.2
# ./configure ... --with-http_stub_status_module
# make && make install
# /usr/local/nginx/sbin/nginx
# netstat -anptu | grep nginx
# ss -anptu | grep nginxDefine the status page in nginx.conf:
location /status {
stub_status on;
#allow IP;
#deny IP;
}Access http://192.168.4.5/status to see metrics such as Active connections, accepts, handled, requests, Reading, Writing, and Waiting.
3. Increase Nginx concurrency
Before tuning, an ab test shows “Too many open files”. # ab -n 2000 -c 2000 http://192.168.4.5/ Modify nginx.conf to match CPU cores and raise connection limits, then reload.
# vim /usr/local/nginx/conf/nginx.conf
worker_processes 2; # same as CPU cores
events {
worker_connections 65535; # max connections per worker
}
# /usr/local/nginx/sbin/nginx -s reloadAdjust Linux file descriptor limits:
# ulimit -a # view current limits
# ulimit -Hn 100000 # set hard limit (temporary)
# ulimit -Sn 100000 # set soft limit (temporary)
# vim /etc/security/limits.conf
* soft nofile 100000
* hard nofile 1000004. Increase request header buffer size
Before adjustment, a script generating a very long URL triggers “414 Request‑URI Too Large”.
# ./buffer.sh
<center><h1>414 Request-URI Too Large</h1></center>Update the http block:
http {
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
}
# /usr/local/nginx/sbin/nginx -s reload5. Browser local cache for static assets
In Firefox, open about:cache to view and clear the cache. Define a 30‑day expiration for static file types in Nginx.
# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
expires 30d; # client cache for 30 days
}
}
# nginx -s reloadAfter reloading, verify caching by accessing an image and checking about:cache again.
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
