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.
<code># firefox http://192.168.4.5/xxxxx // access a missing page</code>Modify
nginx.confto define a custom error page:
<code># vim /usr/local/nginx/conf/nginx.conf
...
error_page 404 /404.html; // custom error page
...</code>Create
/usr/local/nginx/html/404.htmlwith your own content, e.g.:
<code>Oops, No NO no page …</code>Reload Nginx and the client now sees the custom 40x page.
<code># firefox http://192.168.4.5/xxxxx // now shows custom page</code>Common HTTP status codes are shown in the following image:
2. View server status information
Enable the
--with-http_stub_status_modulewhen compiling Nginx to activate the status page.
<code># 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 install</code>Start Nginx and use
ss(or
netstat) to view listening ports. Useful
ssoptions:
-a : show all ports
-n : numeric output
-t : TCP ports
-u : UDP ports
-l : listening sockets
-p : process name
<code># netstat -anptu | grep nginx
# ss -anptu | grep nginx</code>Add a location block in
nginx.confto expose the status page:
<code># cat /usr/local/nginx/conf/nginx.conf
...
location /status {
stub_status on;
#allow IP;
#deny IP;
}
...
# /usr/local/nginx/sbin/nginx -s reload</code>Query the status page:
<code># curl http://192.168.4.5/status
Active connections: 1
server accepts handled requests
10 10 3
Reading: 0 Writing: 1 Waiting: 0</code>Explanation 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
abtest with 2000 concurrent requests fails with "Too many open files".
<code># ab -n 2000 -c 2000 http://192.168.4.5/
socket: Too many open files (24)</code>Adjust
nginx.conf:
<code># 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 reload</code>Raise Linux file‑descriptor limits:
<code># 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 100000</code>Retest 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.
<code># 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 error</code>Increase header buffers in
nginx.conf:
<code># 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 reload</code>Rerun the script – the request succeeds.
5. Browser local cache for static assets
In Firefox, open
about:cacheto view cache entries, then clear the cache.
Configure Nginx to set a 30‑day expiration for common static file types:
<code># 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 reload</code>After reloading, accessing an image with Firefox shows the file cached for the defined period.
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.