提升 Nginx 安全性:隐藏 Server 信息的最佳实践

在当今互联网安全日益重要的背景下,保护服务器免受攻击成为每位开发者的首要任务。Nginx 作为一个广泛使用的高性能 web 服务器,其默认配置可能会暴露关键信息,给潜在攻击者提供了可乘之机。在这篇博客中,我们将探讨如何通过隐藏和自定义 Nginx 的 Server 头,提升网站的安全性和隐私。

1. 通过编译安装好nginx之后 ,我们下载下载 ngx_http_headers_more_filter_module 模块

$: git clone https://github.com/openresty/headers-more-nginx-module.git

**2.**下载完成之后接着重新编译:

$: ./configure --prefix=/usr/local/nginx \

--sbin-path=/usr/local/nginx/sbin/nginx \

--conf-path=/usr/local/nginx/conf/nginx.conf \

--pid-path=/usr/local/nginx/logs/nginx.pid \

--with-http_ssl_module \

--add-module=./headers-more-nginx-module

编译过程中可能会报错 提示缺少open ssl:

./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-openssl=<path> option.

解决办法安装: openssl-devel

$: sudo dnf install openssl-devel

**3.**开始编译

$: sudo make

$: sudo make install

**4.**编译完成之后 出现下面提示就说明可以了

cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'

test -d '/usr/local/nginx/logs' \

|| mkdir -p '/usr/local/nginx/logs'

test -d '/usr/local/nginx/logs' \

|| mkdir -p '/usr/local/nginx/logs'

test -d '/usr/local/nginx/html' \

|| cp -R html '/usr/local/nginx'

test -d '/usr/local/nginx/logs' \

|| mkdir -p '/usr/local/nginx/logs'

make[1]: 离开目录"/home/test/insert_bao/nginx-1.26.2"

**5.**接着我们在 /usr/local/nginx/conf/nginx.conf 配置文件 server模块下 添加内容:

复制代码
    server_tokens off;  # 隐藏版本信息
    more_set_headers 'Server: CNM KNM SB KKK';  # 自定义或移除 Server 头

**6.**保存退出之后 先清理一下nginx缓存

$: sudo /usr/local/nginx/sbin/nginx -s stop

$: sudo rm -rf /usr/local/nginx/logs/*

$: sudo /usr/local/nginx/sbin/nginx

**7.**检查配置文件是否正确

$: sudo /usr/local/nginx/sbin/nginx -t

$: sudo /usr/local/nginx/sbin/nginx -s reload # 刷新配置文件

8. 最后通过curl -I http://localhost:8080 来测试是否生效

HTTP/1.1 200 OK

Date: Tue, 29 Oct 2024 07:18:57 GMT

Content-Type: text/html

Content-Length: 66

Last-Modified: Tue, 29 Oct 2024 06:53:23 GMT

Connection: keep-alive

ETag: "67208663-42"
Server: CNM KNM SB KKK

Accept-Ranges: bytes

这样做的好处:

自定义 Nginx 的 Server 头信息以及隐藏版本信息有几个好处,主要集中在安全性和隐私方面:

1. 提高安全性

  • 防止信息泄露 :默认的 Server 头会暴露 Nginx 的版本信息,攻击者可以利用这些信息来识别潜在的漏洞。如果隐藏或修改这些信息,可以降低被攻击的风险。

  • 减小攻击面 :通过自定义 Server 头,可以使用通用的名称,增加攻击者识别具体技术栈的难度,从而增加系统安全性。

2. 遵循最佳实践

  • 安全配置:很多安全最佳实践建议不暴露详细的服务器和应用信息,特别是在生产环境中。遵循这些建议有助于提高整体安全性。

3. 增强隐私

  • 减少信息暴露:在某些情况下,隐藏软件版本和类型有助于减少信息泄露的风险,尤其是在对抗潜在的侦察和攻击时。

4. 自定义响应

  • 品牌化 :通过自定义 Server 头,可以将其修改为与您的公司或项目相关的信息,这在某些情况下可以提升品牌形象。

5. 符合合规性要求

  • 法律和政策要求:某些行业或地区可能对数据保护有严格的法律法规要求,隐藏服务器信息有助于满足这些要求。
相关推荐
闲云一鹤21 小时前
nginx 快速入门教程 - 写给前端的你
前端·nginx·前端工程化
chlk1231 天前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑1 天前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件1 天前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
碳基沙盒1 天前
OpenClaw 多 Agent 配置实战指南
运维
深紫色的三北六号1 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash2 天前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI2 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行3 天前
Linux和window共享文件夹
linux
木心月转码ing3 天前
WSL+Cpp开发环境配置
linux