Secure Nginx Config: Hide Server Version and Powered-By Headers
To hide the Nginx version, set server_tokens off; in nginx.conf, test with nginx -t, and reload. Remove X-Powered-By in PHP (expose_php = Off) or with an Nginx header directive. Fingerprinting still happens, but you stop giving free version numbers.
Quick answer
sudo sed -n "1,80p" /etc/nginx/nginx.conf | head
# inside http { } block:
# server_tokens off;
sudo nginx -t && sudo systemctl reload nginx
curl -I https://YOUR_SITEWhy hide version headers?
Tools like WhatWeb read Server and X-Powered-By. A clear nginx/1.18.0 or PHP/7.4 speeds up exploit searches. You cannot hide everything, but you can stop handing out the version for free.
What are you changing?
server_tokens controls whether Nginx prints its version. PHP expose_php controls X-Powered-By. Both are small config edits on your server.
How do I do it step by step?
- On your EC2 or VPS, open the main config:
sudo nano /etc/nginx/nginx.confWhat it means: Amazon Linux and Ubuntu both use this file as the main entry.
- Inside the
http { }block add:http { server_tokens off; # ... existing lines } - Test and reload:
sudo nginx -t && sudo systemctl reload nginxnginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successfulWhat it means: Syntax is good and Nginx reloaded without dropping the whole server.
- For PHP-FPM, set in the active
php.ini:expose_php = Off # then: sudo systemctl reload php-fpm # name may be php8.2-fpmWhat it means: PHP stops sending X-Powered-By on responses.
- Verify from your laptop:
curl -I https://YOUR_SITEHTTP/2 200 server: nginx # no nginx/1.x and no x-powered-byWhat it means: Server no longer advertises a version string. Good.
Next, add security headers (see the curl guide) and work through the website security checklist.
Ravindra Bagale's Tip
Hiding banners is layer one, not a magic shield. Still patch. Still use TLS. Kabir in Jaipur hid Server: nginx/1.25 and slept better — then he also turned on automatic security updates.
Ravindra Bagale's Tip – मराठी
Banner लपवणे पहिली थर आहे, जादूची ढाल नाही. तरीही patch करा. तरीही TLS वापरा. जयपूरमधील कबीरने Server लपवला — नंतर automatic security updatesही चालू केले.
Ravindra Bagale's Tip – हिंदी
Banner छिपाना पहली परत है, जादुई ढाल नहीं। फिर भी patch करो। फिर भी TLS रखो। जयपुर के कबीर ने Server छिपाया — फिर automatic security updates भी चालू किए।
Quick quiz: check what you learned
Which nginx setting hides the version number in the Server header?
- server_tokens off;
- autoindex on; in a server
- sendfile off; in http block
- gzip on; for compression
Show answer
A. server_tokens off; removes the version from Server and error pages.
After editing nginx.conf, what must you do?
- Test config then reload nginx
- Delete the access log file
- Disable SELinux forever
- Open port 21 for FTP
Show answer
A. nginx -t then systemctl reload nginx.
Learn it properly
This guide is the short path. These pages go deeper, with labs:
Frequently asked questions
What does server_tokens off do?
Nginx stops adding its version number to the Server header and error pages.
Will this stop WhatWeb completely?
No. It only removes easy version leaks. Other clues remain.
How do I remove X-Powered-By?
For PHP set expose_php=Off. Or add more_clear_headers / proxy_hide_header where supported.
Is changing headers enough hardening?
No. Pair with TLS, least open ports, patches and a checklist.
Where is the deeper course lesson?
Cyber 32.5 Web server hardening and 36.3 WhatWeb fingerprinting.