Boost Nginx Performance: Custom 404 Pages, Status Monitoring, and Concurrency Tuning
This guide walks through customizing Nginx 404 error pages, enabling and reading the stub_status module, increasing worker processes and connections, adjusting kernel limits, expanding header buffers, and configuring browser caching for static assets to dramatically improve server performance and reliability.
Nginx is a high‑performance web and reverse‑proxy server that uses little memory and handles massive concurrency, making it a popular alternative to Apache for large Chinese companies such as Taobao, JD, Baidu, Sina, NetEase, and Tencent.
1. Custom 404 error page
Before optimization, accessing a non‑existent page returns the default "404 Not Found" message.
# firefox http://192.168.4.5/xxxxx // access a missing pageModify nginx.conf to define a custom error page:
# vim /usr/local/nginx/conf/nginx.conf
...
error_page 404 /404.html; // custom error page
...Create /usr/local/nginx/html/404.html with your own content, e.g.: Oops, No NO no page … Reload Nginx and the client now sees the custom 40x page.
# firefox http://192.168.4.5/xxxxx // now shows custom pageCommon HTTP status codes are shown in the following image:
2. View server status information
Enable the --with-http_stub_status_module when compiling Nginx to activate the status page.
# tar -zxvf nginx-1.12.2.tar.gz
# cd nginx-1.12.2
# ./configure \
--with-http_ssl_module // enable SSL
--with-stream // enable TCP/UDP proxy
--with-http_stub_status_module // enable status page
# make && make installStart Nginx and use ss (or netstat) to view listening ports. Useful ss options:
-a : show all ports
-n : numeric output
-t : TCP ports
-u : UDP ports
-l : listening sockets
-p : process name
# netstat -anptu | grep nginx
# ss -anptu | grep nginxAdd a location block in nginx.conf to expose the status page:
# cat /usr/local/nginx/conf/nginx.conf
...
location /status {
stub_status on;
#allow IP;
#deny IP;
}
...
# /usr/local/nginx/sbin/nginx -s reloadQuery the status page:
# curl http://192.168.4.5/status
Active connections: 1
server accepts handled requests
10 10 3
Reading: 0 Writing: 1 Waiting: 0Explanation of the fields:
Active connections – current active connections
Accepts – total accepted connections
Handled – total handled connections (usually equals accepts)
Requests – total client requests
Reading – connections reading request headers
Writing – connections writing responses
Waiting – idle connections waiting for a request
3. Increase Nginx concurrency
Before tuning, an ab test with 2000 concurrent requests fails with "Too many open files".
# ab -n 2000 -c 2000 http://192.168.4.5/
socket: Too many open files (24)Adjust nginx.conf:
# vim /usr/local/nginx/conf/nginx.conf
...
worker_processes 2; // match CPU cores
events {
worker_connections 65535; // max connections per worker
}
...
# /usr/local/nginx/sbin/nginx -s reloadRaise Linux file‑descriptor limits:
# ulimit -a // view limits
# ulimit -Hn 100000 // set hard limit (temporary)
# ulimit -Sn 100000 // set soft limit (temporary)
# edit /etc/security/limits.conf
* soft nofile 100000
* hard nofile 100000Retest with ab – the server now handles the load.
4. Expand request header buffer
A script generating a very long URL triggers a "414 Request-URI Too Large" error.
# cat lnmp_soft/buffer.sh
#!/bin/bash
URL=http://192.168.4.5/index.html?
for i in {1..5000}; do
URL=${URL}v$i=$i
done
curl $URL // results in 414 errorIncrease header buffers in nginx.conf:
# vim /usr/local/nginx/conf/nginx.conf
...
http {
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
}
# /usr/local/nginx/sbin/nginx -s reloadRerun the script – the request succeeds.
5. Browser local cache for static assets
In Firefox, open about:cache to view cache entries, then clear the cache.
Configure Nginx to set a 30‑day expiration for common static file types:
# 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
}
}
# cp /usr/share/backgrounds/day.jpg /usr/local/nginx/html
# /usr/local/nginx/sbin/nginx -s reloadAfter reloading, accessing an image with Firefox shows the file cached for the defined period.
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.
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.
