How to Optimize Nginx for High Concurrency, Custom Errors, and Better Caching
This guide walks through customizing Nginx's 404 page, enabling the stub_status module to monitor server metrics, tuning worker processes and connection limits for higher concurrency, expanding client header buffers, and configuring static file caching to improve overall web performance.
Custom 404 Error Page
Before modification, browsers display a generic "404 Not Found" page when accessing a missing resource. 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 reload Nginx with nginx -s reload.
# vim /usr/local/nginx/conf/nginx.conf
...
error_page 404 /404.html;
...
# vim /usr/local/nginx/html/404.html
Oops, No page …
# nginx -s reloadViewing Server Status
Enable the http_stub_status_module during compilation (e.g., --with-http_stub_status_module) and add a location block in the configuration:
location /status {
stub_status on;
#allow 127.0.0.1;
#deny all;
}After reloading, request http://<em>server_ip</em>/status to see metrics such as active connections, accepts, handled requests, reading, writing, and waiting.
Increasing Nginx Concurrency
Adjust the number of worker processes to match CPU cores and raise worker_connections to a high value (e.g., 65535). Then reload Nginx.
worker_processes 2; # match CPU cores
events {
worker_connections 65535;
}Test concurrency with ab -n 2000 -c 2000 http://<em>server_ip</em>/. If you encounter "Too many open files", increase the file descriptor limits:
# ulimit -Hn 100000 # hard limit
# ulimit -Sn 100000 # soft limit
# edit /etc/security/limits.conf
* soft nofile 100000
* hard nofile 100000Optimizing Client Header Buffers
Modify the http block to enlarge header buffers, preventing "414 Request-URI Too Large" errors for long URLs.
http {
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
}Browser Local Caching of Static Assets
Configure Nginx to set an expiration time for static file types, allowing browsers (e.g., Firefox) to cache them for 30 days.
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;
}
}After reloading, verify the cache via about:cache in Firefox.
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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
