Boost Nginx Performance: Custom 404 Pages, Status Monitoring, and Concurrency Tuning

Learn how to enhance your Nginx server by creating custom 404 error pages, enabling and reading the stub_status module, increasing worker processes and connections, adjusting kernel limits, expanding header buffers, and configuring client-side caching for static assets, with step‑by‑step commands and examples.

Efficient Ops
Efficient Ops
Efficient Ops
Boost Nginx Performance: Custom 404 Pages, Status Monitoring, and Concurrency Tuning

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 because of its low memory footprint and strong concurrency capabilities.

1. Custom 404 Error Page

Before optimization, accessing a non‑existent page returns the default 404 "File not found" message. # firefox http://192.168.4.5/xxxxx Modify /usr/local/nginx/conf/nginx.conf to add a custom error page: error_page 404 /404.html; # custom error page Create the custom page:

# vim /usr/local/nginx/html/404.html
Oops, No page …

Reload Nginx and the browser will display the custom 40x page.

2. Viewing Server Status Information

Enable the --with-http_stub_status_module when compiling Nginx to activate the status page module.

# ./configure \
    --with-http_ssl_module \
    --with-stream \
    --with-http_stub_status_module
# make && make install

Define the status location in nginx.conf:

location /status {
    stub_status on;
    #allow IP;
    #deny IP;
}

Reload Nginx and query 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: 0

The output explains active connections, accepts, handled, requests, reading, writing, and waiting counts.

HTTP status codes table
HTTP status codes table

3. Optimizing Nginx Concurrency

Test concurrency with ab before changes:

# ab -n 2000 -c 2000 http://192.168.4.5/
socket: Too many open files (24)

Increase worker processes and connections in nginx.conf:

worker_processes 2;               # match CPU cores
events {
    worker_connections 65535;   # max connections per worker
}

Adjust Linux limits for open files:

# ulimit -Hn 100000   # hard limit (temporary)
# ulimit -Sn 100000   # soft limit (temporary)
# edit /etc/security/limits.conf
* soft nofile 100000
* hard nofile 100000

Retest with ab to verify improved concurrency.

4. Expanding Header Buffer Size

Running a script that generates a very long URL triggers a 414 error:

# ./buffer.sh
<center><h1>414 Request-URI Too Large</h1></center>

Increase header buffers in the http block:

http {
    client_header_buffer_size 1k;
    large_client_header_buffers 4 4k;
}

Reload Nginx; the long‑URL request now succeeds.

5. Browser Local Cache for Static Assets

In Firefox, open about:cache to view cached entries. After configuring an expires directive, static files are cached for 30 days.

location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
    expires 30d;   # client cache duration
}

Reload Nginx, request an image, and verify via about:cache that the file is cached with the correct expiration.

Firefox cache view
Firefox cache view
Firefox cache cleared
Firefox cache cleared
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendperformanceNginx
Efficient Ops
Written by

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.

0 followers
Reader feedback

How this landed with the community

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.