web 服务的版本号对于恶意的攻击者无疑是有帮助的,因为不同版本号或多或少有其对应的漏洞,一旦被恶意的攻击者发现将对线上服务造成严重危害。而隐藏服务版本号则有助于提升 web 的安全性。
隐藏 Nginx 版本号
上游服务:php
设置 Server 头部值,这将覆盖 nginx 的 Server 默认值。
1 | header('Server: codezm'); |

nginx 层
- 打开
nginx.conf配置文件。 - 找到
http块,增加server_tokens off;指令。 - 重启 nginx :
nginx -s reload。![image-20210106122015268]()
隐藏 PHP 版本号
正常请求时,会响应 X-Powered-By 头部。

PHP 代码可通过设置 header 修改 X-Powered-By 头部响应值。
1 | header('X-Powered-By: codezm-1.0.1,ASP.NET'); |

通过编辑 php.ini 文件,将 expose_php = Off 配置项设置为 Off 可关闭响应 X-Powered-By 头部,默认值是 On。

Nginx 忽略响应头部。
PHP-FPM: fastcgi_hide_header X-Powered-By;
1 | location ~ \.php$ { |
1 | # 重新加载配置文件 |

Proxy: proxy_hide_header X-Powered-By;
配置 nginx 反向代理
1 | location ^~ /phpServer { |
1 | # 重新加载配置文件 |

