nginx安全加固

1 禁用目录浏览

禁用nginx.conf中的autoindex模块。

1
2
autoindex off
# 或者删除autoindex配置

2 隐藏版本信息

如果开启的话(默认情况下)所有的错误页面都会显示服务器的版本和信息。nginx.conf配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
http{

include naxsi_core.rules;

include mime.types;

default_type application/octet-stream;

sendfile on;
# 增加下面配置
server_tokens off;

... ...

加固检查:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
root@9340a450ae64:/workspace# curl -I insight.jx-lab.com
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Fri, 28 Jan 2022 03:26:35 GMT
Content-Type: text/html; charset=utf-8,gbk
Content-Length: 14052
Last-Modified: Tue, 14 Apr 2020 06:26:50 GMT
Connection: keep-alive
ETag: "5e9557aa-36e4"
Accept-Ranges: bytes

# 修改后

root@9340a450ae64:/workspace# curl -I insight.jx-lab.com
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 28 Jan 2022 03:30:29 GMT
Content-Type: text/html; charset=utf-8,gbk
Content-Length: 14052
Last-Modified: Tue, 14 Apr 2020 06:26:50 GMT
Connection: keep-alive
ETag: "5e9557aa-36e4"
Accept-Ranges: bytes

3 限制HTTP请求方法

备份nginx.conf配置文件。

编辑配置文件,添加如下内容:

1
2
3
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 444;
}

保存,然后后重启nginx服务。

备注:只允许常用的GET和POST方法,顶多再加一个HEAD方法

4 限制IP访问

备份nginx.conf配置文件。

编辑配置文件,添加如下内容:

1
2
3
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 444;
}

保存,然后后重启nginx服务。

备注:只允许常用的GET和POST方法,顶多再加一个HEAD方法

5 限制并发和速度

编辑配置文件,在server标签内添加如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
limit_zone one $binary_remote_addr 10m;
server
{
listen 80;
server_name down.test.com;
index index.html index.htm index.php;
root /usr/local/www;
#Zone limit;
location / {
limit_conn one 1;
limit_rate 20k;
}
………
}

保存,然后后重启nginx服务。

6 控制超时时间

编辑配置文件,具体设置如下:

1
2
3
4
client_body_timeout 10;  #设置客户端请求主体读取超时时间
client_header_timeout 10; #设置客户端请求头读取超时时间
keepalive_timeout 5 5; #第一个参数指定客户端连接保持活动的超时时间,第二个参数是可选的,它指定了消息头保持活动的有效时间
send_timeout10; #指定响应客户端的超时时间

保存,然后后重启nginx服务。

-------------本文结束感谢您的阅读-------------

本文标题:nginx安全加固

文章作者:OperationMAN

发布时间:2022年01月28日 - 11:01

最后更新:2022年03月31日 - 16:03

原始链接:https://kxinter.gitee.io/2022/01/28/nginx安全加固/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

坚持原创技术分享,您的支持将鼓励我继续创作!