Operations 9 min read

How to Optimize Nginx: Custom Error Pages, Status Monitoring, and Performance Tuning

This guide walks you through customizing Nginx 404 error pages, enabling the stub_status module to monitor server metrics, increasing worker processes and connections, adjusting header buffers, and configuring client-side caching for static assets to boost web server performance.

Efficient Ops
Efficient Ops
Efficient Ops
How to Optimize Nginx: Custom Error Pages, Status Monitoring, and Performance 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.

1. Custom 404 error page

Before optimization, accessing a non‑existent page returns the default 404 message. # firefox http://192.168.4.5/xxxxx Modify nginx.conf to define a custom error page:

# vim /usr/local/nginx/conf/nginx.conf
... 
error_page 404 /404.html;   # custom error page
... 
# vim /usr/local/nginx/html/404.html
Oops, No page …
# nginx -s reload

After reloading, the browser shows the custom 404.html page.

2. Viewing server status

Enable the --with-http_stub_status_module when compiling Nginx to expose a status page.

# tar -zxvf nginx-1.12.2.tar.gz
# cd nginx-1.12.2
# ./configure \
    --with-http_ssl_module \
    --with-stream \
    --with-http_stub_status_module
# make && make install

Start Nginx and reload the configuration, then define the status location:

# cat /usr/local/nginx/conf/nginx.conf
... 
location /status {
    stub_status on;
    #allow IP;
    #deny IP;
}
... 
# /usr/local/nginx/sbin/nginx -s reload

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 fields represent active connections, total accepts, handled requests, total requests, and the numbers of reading, writing, and waiting connections.

3. Increasing concurrency

Run a high‑concurrency test with ab and observe the “Too many open files” error.

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

Increase worker processes and connections in nginx.conf:

# 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

Adjust Linux limits for open files:

# ulimit -a
# ulimit -Hn 100000   # hard limit
# ulimit -Sn 100000   # soft limit
# vim /etc/security/limits.conf
* soft nofile 100000
* hard nofile 100000

Retest with ab to verify improved concurrency.

4. Expanding header buffer size

Before the change, a script generating a very long URL triggers a “414 Request‑URI Too Large” response.

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

Increase header buffers in the http block:

# 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

After reloading, the long‑URL request succeeds.

5. Browser local caching of static assets

In Firefox, open about:cache to view cached entries, then clear the cache.

Configure Nginx to set an expiration time for static files:

# 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;   # cache for 30 days
    }
}
# cp /usr/share/backgrounds/day.jpg /usr/local/nginx/html
# /usr/local/nginx/sbin/nginx -s reload

After reloading, accessing an image (e.g., day.jpg) stores it in the browser cache for 30 days, which can be verified again via about: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.

LinuxNginxServer Configuration
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.