提升 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'

make1: 离开目录"/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. 符合合规性要求

  • 法律和政策要求:某些行业或地区可能对数据保护有严格的法律法规要求,隐藏服务器信息有助于满足这些要求。
相关推荐
orion5715 小时前
Missing Semester Class1:course overview and introduction of shell
linux
SkyWalking中文站19 小时前
认识 Horizon UI · 6/17:Trace 探索器
运维·监控·自动化运维
用户1204872216121 小时前
Linux驱动编译与加载
linux·嵌入式
火车叼位1 天前
写给初级开发者:SSL、SSH、HTTPS 与证书体系全解析
运维
用户805533698031 天前
Input 子系统架构:Core、Handler、Driver 三层是怎么协作的
linux·嵌入式
用户805533698031 天前
RK-Forge外设系列开篇 - 把板子从「能启动」变成「能用」:Ethernet/SPI/MMC 三个纯接线外设
linux·github·嵌入式
小猿姐1 天前
唯品会大规模数据库云原生实践:基于 KubeBlocks 管理数千实例的统一运维之路
运维·elasticsearch·云原生
七歌杜金房2 天前
我终于又有了自己的 Linux 电脑
linux·debian·mac
SkyWalking中文站2 天前
认识 Horizon UI · 5/17:3D 基础设施地图
运维·监控·自动化运维
tntxia3 天前
linux curl命令详解_curl详解
linux