概括
这提供了 NginX 安全配置强化指南。配置指南侧重于 NginX 本身。因此,Linux 操作系统配置加固不在此处介绍。
它包括以下主题: 2.1 缓冲区溢出保护 2.2 删除不必要的备份文件 2.3 删除版本号 2.4 缓解缓慢的 HTTP DoS 攻击 2.5 仅允许访问指定域 2.6 限制 IP 客户端访问 2.7 SSL/TLS 配置 2.8 SSL 模块 2.9 HTTP 安全标头 2.10限制 HTTP 方法
安全配置
缓冲区溢出保护
## Size Limits & Buffer Overflows
## the size may be configured based on the needs.
client_body_buffer_size 1024K;
client_header_buffer_size 1024K;
client_max_body_size 512m;
large_client_header_buffers 4 256k;
有关每个配置值的详细信息,请参阅此处。 http://nginx.org/en/docs/http/ngx_http_core_module.html
基于 CIS Appache 配置指南
- 将 Http 请求头的大小限制为 1024 字节或更小
- 将请求正文中允许的字节数限制为 102400 或更少
删除不必要的备份文件
使用以下命令搜索是否有任何不必要的备份文件要删除。
find /usr/local/nginx -name '.?*' -not -name .ht* -or -name '*~' -or -name '*.bak*' -or -name '*.old*'
find /usr/local/nginx/html/ -name '.?*' -not -name .ht* -or -name '*~' -or -name '*.bak*' -or -name '*.old*'
删除版本号
# 显示nginx版本号错误或http头可能导致黑客搜索已知漏洞。
# 因此,应该为每个 http 响应删除版本号。
server_tokens off;
缓解慢速 HTTP DoS 攻击
## Timeouts definition ##
client_body_timeout 15s;
client_header_timeout 15s;
keepalive_timeout 1800;
send_timeout 15s;
## End ##
- client_body_timeout:定义读取客户端请求正文的超时时间。超时仅在两次连续读取操作之间设置,而不是为整个请求正文的传输设置。如果客户端在此时间内没有传输任何内容,则向客户端返回 408(请求超时)错误。
- client_header_timeout:定义读取客户端请求头的超时时间。如果客户端在这段时间内没有传输整个标头,则向客户端返回 408(请求超时)错误。
- keepalive_timeout:第一个参数设置一个超时时间,在此期间,客户端连接将在服务器端保持打开状态。零值禁用保持活动客户端连接。可选的第二个参数在“Keep-Alive: timeout=time”响应头字段中设置一个值。两个参数可能不同。Mozilla 和 Konqueror 可以识别“Keep-Alive: timeout=time”标头字段。MSIE 在大约 60 秒内自行关闭保持连接。
- send_timeout:设置向客户端发送响应的超时时间。超时仅在两个连续的写操作之间设置,而不是为整个响应的传输而设置。如果客户端在这段时间内没有收到任何内容,则关闭连接。
有关每个配置值的详细信息,请参阅此处。 http://nginx.org/en/docs/http/ngx_http_core_module.html
只允许访问指定域
## i.e. abc.com, images.abc.com and www.abc.com
if ($host !~ ^(abc.com|www.abc.com|images.abc.com)$ ) {
return 444;
}
##
限制 IP 客户端访问
仅将特定文件夹限制为某些源 IP 客户端。
## the docs folder is only allowed specific IP range in 192.168.1.0/24
location /docs/ {
## block one workstation
deny 192.168.1.1;
## allow anyone in 192.168.1.0/24
allow 192.168.1.0/24;
## drop rest of the world
deny all;
}
SSL/TLS 配置
禁用 SSLv3(自 nginx 0.8.19 起默认启用)
server {
# SSL protocols TLS v1~TLSv1.2 are allowed. Disabed SSLv3
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
}
有关 SSL 模块配置的更多详细信息,请参阅http://wiki.nginx.org/NginxHttpSslModule
SSL 模块
server {
# enables server-side protection from BEAST attacks
ssl_prefer_server_ciphers on;
# Disabled insecure ciphers suite. For example, MD5, DES, RC4, PSK
ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4:@STRENGTH";
# -!MEDIUM:exclude encryption cipher suites using 128 bit encryption.
# -!LOW: exclude encryption cipher suites using 64 or 56 bit encryption algorithms
# -!EXPORT: exclude export encryption algorithms including 40 and 56 bits algorithms.
# -!aNULL: exclude the cipher suites offering no authentication. This is currently the anonymous DH algorithms and anonymous ECDH algorithms.
# These cipher suites are vulnerable to a "man in the middle" attack and so their use is normally discouraged.
# -!eNULL:exclude the "NULL" ciphers that is those offering no encryption.
# Because these offer no encryption at all and are a security risk they are disabled unless explicitly included.
# @STRENGTH:sort the current cipher list in order of encryption algorithm key length.
}
HTTP 安全标头
# X-Frame-Options is to prevent from clickJacking attack
add_header X-Frame-Options SAMEORIGIN;
# disable content-type sniffing on some browsers.
add_header X-Content-Type-Options nosniff;
# This header enables the Cross-site scripting (XSS) filter
add_header X-XSS-Protection "1; mode=block";
# This will enforce HTTP browsing into HTTPS and avoid ssl stripping attack
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";
参考https://www.owasp.org/index.php/List_of_useful_HTTP_headers
限制 HTTP 方法
## Only GET, Post, PUT are allowed##
if ($request_method !~ ^(GET|PUT|POST)$ ) {
return 444;
}
## In this case, it does not accept other HTTP method such as HEAD, DELETE, SEARCH, TRACE ##
在大多数情况下,建议禁用 TRACE 方法。
一,为什么要做连接超时设置?
nginx在保持着与客户端的连接时,要消耗cpu/内存/网络等资源,
如果能在超出一定时间后自动断开连接,
则可以及时释放资源,起到优化性能、提高效率的作用
说明:刘宏缔的架构森林是一个专注架构的博客,地址:刘宏缔的架构森林 - 博客园
对应的源码可以访问这里获取: liuhongdi (刘宏缔) · GitHub
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,keepalive的超时时长:keepalive_timeout
1,keepalive的作用:
HTTP 的 KeepAlive 模式:
webserver 在处理完一个请求后保持这个 TCP 连接的状态仍然是打开。
如果再次接收到来自此客户端的其它请求,
服务端会使用这个未关闭的连接,而不是再新建一个连接
2,keepalive的配置:
keepalive_timeout 60 45;
两个参数分别是:
nginx服务的超时时间(默认值是75s,建议设置为60秒)
nginx在给浏览器的响应header信息中的超时时间
注意,第二个参数设置之后才会在浏览器端出现keepalive一项:
如图:
3,nginx给header的超时信息并不是肯定会得到浏览器的执行
4,keepalive_timeout的值应该大于client_body_timeout
三,客户端header的超时时长:client_header_timeout
client_header_timeout 15s;
默认值是60s
客户端向服务端发送一个完整的 request header 的超时时间
如果60s内没有收到完整的http request header,则为超时
如果客户端超时,Nginx 返回 HTTP 408(Request Timed Out)。
四,客户端body的超时时长:client_body_timeout
client_body_timeout 15s;
默认值是60s
客户端向服务端发送 request body 的超时时间
如果连续的60s内没有收到客户端的1个字节,则表示超时
如果客户端超时,Nginx 返回 HTTP 408(Request Timed Out)。
五,向客户端发送数据超时时长:send_timeout
send_timeout 15s;
默认值是60s
send_timeout 指定客户端的响应超时时间。
这个设置指的是在这段时间内,客户端没有读取任何数据,nginx就会关闭连接.
六,如果有大文件上传时需配置哪个指定?
有大文件上传时,需要指定body的最大值
client_max_body_size 50m;
这个指定的默认值是1M,基本上不可能满足使用需求
如果需要上传较大的文件,在这里指定一个最大值
这里指定文件最大是50MB