目录
前言
在当今互联网技术高速发展的背景下,Web服务的安全性与可靠性已成为企业及开发者不可忽视的核心课题。作为全球最受欢迎的高性能Web服务器和反向代理工具,Nginx凭借其轻量级、高并发处理能力和灵活的模块化设计,占据了全球近三分之一的Web服务器市场份额。然而,随着网络攻击手段的不断升级(如DDoS、SQL注入、恶意爬虫等),以及全球范围内对数据隐私保护的法规要求(如GDPR、等保2.0),掌握Nginx的安全防护策略与HTTPS部署能力,已成为运维工程师和开发者的必备技能
一. 核心安全配置
1. 隐藏版本号
在生产环境中,需要隐藏 Nginx 的版本号,以避免泄漏 Nginx 的版本,使攻击者不能针对特定版本进行攻击。在隐藏版本号之前,可以使用 Fiddler 工具抓取数据包,查看Nginx 版本,也可以在 OpenEuler 中使用命令 curl -I http://192.168.10.101/查看
[root@localhost ~]# curl -I 192.168.10.101
HTTP/1.1 200 OK
Server: nginx/1.26.3 //版本号
Date: Mon, 07 Apr 2025 08:32:36 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Mon, 07 Apr 2025 08:17:10 GMT
Connection: keep-alive
ETag: "67f38a06-267"
Accept-Ranges: bytes
修改配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# curl -I 192.168.10.101
HTTP/1.1 200 OK
Server: nginx #版本号被隐藏
Date: Mon, 07 Apr 2025 08:39:31 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Mon, 07 Apr 2025 08:17:10 GMT
Connection: keep-alive
ETag: "67f38a06-267"
Accept-Ranges: bytes
2. 限制危险请求方法
不安全的请求方式,是潜在的安全风险,TRACE(易引发XST攻击)、PUT/DELETE(文件修改风险)、CONNECT(代理滥用),通过正则表达式匹配请求方法,非白名单方法返回444(无响应关闭连接)
修改配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
if ($request_method !~ ^(GET|HEAD|POST)$) {
return 444;
}
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf te

1188

被折叠的 条评论
为什么被折叠?



