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

This guide walks through practical Nginx optimizations—including custom 404 error pages, enabling and reading the stub_status page, increasing worker processes and connections, expanding header buffers, and configuring client-side caching for static assets—complete with command‑line examples and configuration snippets.

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

1. Custom 404 Error Page

Before optimization, a request for a non‑existent URL returns the default "404 Not Found" page. By editing /usr/local/nginx/conf/nginx.conf and adding error_page 404 /404.html;, you can serve a custom HTML file (e.g., /usr/local/nginx/html/404.html) and then reload Nginx with nginx -s reload. After the change, browsers display the custom 404 page.

2. Viewing Server Status Information

Compile Nginx with the --with-http_stub_status_module flag to enable the status module. In the configuration file, add a location block:

location /status {
    stub_status on;
    #allow 127.0.0.1;
    #deny all;
}

Reload Nginx and query the page (e.g., curl http://192.168.4.5/status) to see metrics such as active connections, accepts, handled, requests, reading, writing, and waiting. You can also view listening ports with netstat -anptu | grep nginx or ss -anptu | grep nginx. The ss command works on RHEL7 as a drop‑in replacement for netstat.

3. Increasing Nginx Concurrency

Initial high‑concurrency testing with ab -n 2000 -c 2000 http://192.168.4.5/ may fail with "Too many open files". Increase concurrency by editing nginx.conf:

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

Then reload Nginx. Adjust Linux limits to allow more open files:

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

Retest with ab after the changes.

4. Expanding Header Buffer Size

A script that builds a very long URL (5000 query parameters) triggers a "414 Request-URI Too Large" error. Fix this by increasing header buffers in the http block of nginx.conf:

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

Reload Nginx and rerun the script; the request succeeds.

5. Browser Local Cache for Static Assets

In Firefox, entering about:cache shows cached resources. To enable long‑term client caching, add an expires directive for static file types:

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

After reloading Nginx, access a static image (e.g., http://192.168.4.5/day.jpg) and verify via about:cache that the file is cached with the correct expiration.

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.

BackendoptimizationNGINXWeb server
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.