Operations 8 min read

How to Optimize Nginx: Custom 404 Pages, Status Monitoring, and High‑Concurrency Tuning

This guide walks you through customizing Nginx error pages, enabling and reading the built‑in status module, boosting worker and connection limits, adjusting header buffer sizes, and configuring browser caching for static assets, all with concrete commands and configuration examples.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Optimize Nginx: Custom 404 Pages, Status Monitoring, and High‑Concurrency Tuning

1. Custom 404 Error Page

Before the change, a request to a non‑existent URL returns the default 404 message. Edit /usr/local/nginx/conf/nginx.conf and add error_page 404 /404.html;. Create the custom page at /usr/local/nginx/html/404.html (e.g., containing "Oops, No page …"). Reload Nginx with nginx -s reload. After reloading, the browser shows the custom 40x page.

2. Viewing Server Status

Compile Nginx with the --with-http_stub_status_module flag to enable the status page. After installation, add a location block in nginx.conf:

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

Reload Nginx and query the page: curl http://192.168.4.5/status The output shows active connections, accepts, handled, requests, and the numbers of reading, writing, and waiting connections, providing a quick health check.

3. Increasing Concurrency

Initial load testing with ab -n 2000 -c 2000 http://192.168.4.5/ may hit "Too many open files". Increase worker processes and connections in nginx.conf:

worker_processes 2;  # match CPU cores
events {
    worker_connections 65535;
}

Reload Nginx, then retest with ab. To raise the OS file descriptor limit, edit /etc/security/limits.conf:

* soft nofile 100000
* hard nofile 100000

Apply the new limits with ulimit -Hn 100000 and ulimit -Sn 100000.

4. Expanding Header Buffer Cache

When a long URL triggers "414 Request‑URI Too Large", increase the header buffers:

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

Reload Nginx and re‑run the script that builds a 5,000‑parameter URL; the request now succeeds.

5. Browser‑Side Static Asset Caching

In Firefox, open about:cache to view cached entries. To set a 30‑day cache for static files, add a location block:

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

Reload Nginx, then access an image (e.g., http://192.168.4.5/day.jpg) and verify the cache entry and expiration time in Firefox.

Firefox cache view
Firefox cache view
Clearing Firefox cache
Clearing Firefox cache
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.

performance tuningLinuxserver optimization
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.