隐藏 Nginx 头信息

junlan
4
2025-02-21

1. 隐藏 nginx 版本号

| Syntax: | **server_tokens** on | off | build | *string*; |
| :------- | ------------------------------------------------ |
| Default: | server_tokens on; |
| Context: | http, server, location |

Enables or disables emitting nginx version on error pages and in the “Server” response header field.

The build parameter (1.11.10) enables emitting a build name along with nginx version.

Additionally, as part of our commercial subscription, starting from version 1.9.13 the signature on error pages and the “Server” response header field value can be set explicitly using the *string* with variables. An empty string disables the emission of the “Server” field.

  • 修改配置
[root@blog blog]# vi nginx/default.conf
server {
    listen 443 ssl;
    server_name land.snimay.net;

    server_tokens off;    # 放在 server 块
}

# 重新加载配置
root@e14a24e48290:/# nginx -s reload

[root@blog blog]# curl -I https://land.snimay.net/
HTTP/1.1 404 Not Found
Server: nginx/1.27.4     # 修改配置前显示版本号
Date: Fri, 21 Feb 2025 08:47:56 GMT
Content-Type: application/problem+json
Content-Length: 198
Connection: keep-alive
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Content-Type-Options: nosniff
Strict-Transport-Security: max-age=31536000
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 0
Referrer-Policy: strict-origin-when-cross-origin
Strict-Transport-Security: max-age=31536000; includeSubDomains

[root@blog blog]# curl -I https://land.snimay.net/
HTTP/1.1 404 Not Found
Server: nginx    # 修改配置后隐藏版本号
Date: Fri, 21 Feb 2025 08:48:22 GMT

2. 隐藏 nginx server 头信息

https://zhuanlan.zhihu.com/p/650113278