Operations 10 min read

Master Nginx Optimization: Hide Version, Tune Workers, Enable Gzip & More

This guide walks through comprehensive Nginx service optimization techniques—including hiding the server version, adjusting user and group settings, configuring cache expiration, automating log rotation, fine‑tuning connection timeouts, scaling worker processes, enabling gzip compression, preventing hotlinking, and an overview of essential modules—providing clear commands and configuration snippets for each step.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Nginx Optimization: Hide Version, Tune Workers, Enable Gzip & More

1. Nginx Service Optimization

(1) Hide Nginx Version Number

Hide the Nginx version to avoid exposing security vulnerabilities. Two methods: edit the configuration file or modify the source code.

server_tokens off; ## disable version number
nginx.h ## modify source

Recompile and install:

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
make -j 2 && make install

Alternatively, use the headers-more-nginx-module-0.34.tar.gz plugin and add more_clear_headers directives to remove specific response headers.

(2) Change Nginx User and Group

Nginx processes need a user and group for file access control. By default it runs as nobody. Change it by specifying the user and group during compilation or by editing the configuration file:

user <span>username</span> <span>groupname</span>;

(3) Set Web Page Cache Duration

Configure expires directives in the http, server, or location blocks to define how long static resources are cached, improving response speed for repeat requests.

(4) Implement Log Rotation

As Nginx runs longer, log files grow. Use Linux cron jobs and signal‑based scripts to rotate logs periodically, preventing oversized log files from hindering monitoring.

shell script + crontab

(5) Configure Connection Timeouts

Set timeout parameters to avoid long‑lived idle connections:

keepalive_timeout 60;
client_header_timeout 60;
client_body_timeout 60;

(6) Adjust Worker Process Count

Increase worker_processes to match the number of CPU cores (or up to twice the core count) for high‑concurrency scenarios, and optionally bind workers to specific CPUs:

cat /proc/cpuinfo | grep -c "physical id"   # view CPU cores
ps aux | grep nginx                         # view Nginx processes
worker_processes auto;                     # set to CPU count
worker_cpu_affinity ...;                    # bind workers to CPUs

(7) Enable Gzip Compression

Activate the ngx_http_gzip_module to compress responses, reducing bandwidth usage. Add gzip on; and related parameters in the configuration. Note that compressing already compressed media (images, videos) or very large files may waste CPU resources.

(8) Set Up Anti‑Hotlinking

Prevent unauthorized use of site resources by configuring referer checks. Example: if ($invalid_referer) { rewrite ... } Define valid_referers such as *.example.com and allow requests without a referer.

(9) Common Nginx Modules

http_stub_status_module   # status statistics
http_gzip_module          # compression
http_rewrite_module       # URL rewriting
http_ssl_module           # HTTPS encryption
http_auth_basic_module    # basic authentication
http_fastcgi_module       # FastCGI support
http_image_filter_module  # image processing
http_mp4/flv_module       # video handling
http_limit_req_module     # request limiting
http_limit_conn_module    # connection limiting
http_proxy_module         # proxy forwarding
http_upstream_*_module    # load balancing
stream                    # TCP/UDP proxy

For the full set of configuration examples and screenshots, refer to the original source.

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.

SecurityNGINXserver optimization
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.