Nginx生产环境常规安全加固
持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第5天
今天前同事问我,生产Nginx要做什么加固吗?其实我也不怎么知道,所以稍微整理一下。
1、使用非root用户启动
为了安全期间,最好不要使用root用户启动nginx,有如下两种方式
- 1、默认配置文件里面的#user nobody;
- 2、创建非登录用户nginx 添加配置 user nginx;
2、开启访问日志记录
为了详尽的记录操作信息,默认访问日志是关闭的,增加如下配置,log_format定义的是日志格式,main_body是日志名称,这里定义的是json结构的,nginx.conf的在http模块下面增加如下配置即可。注意日志目录的权限问题:
- 1、打开conf/nginx.conf配置文件;
- 2、在http模块下,配置log_format项 和access_log配置项如下;
log_format main_body
'{"server_port":$server_port,'
'"remote_addr":"$remote_addr",'
'"http_x_forwarded_for":"$http_x_forwarded_for",'
'"@timestamp":"$time_iso8601",'
'"request":"$request",'
'"status":$status,'
'"request_time":$request_time,'
'"request_length":$request_length,'
'"body_bytes_sent":$body_bytes_sent,'
'"http_referer":"$http_referer",'
'"http_user_agent":"$http_user_agent",'
'"upstream_addr":"$upstream_addr",'
'"upstream_status":"$upstream_status",'
'"upstream_response_time":"$upstream_response_time",'
'"upstream_response_length":"$upstream_response_length",'
'"upstream_cache_status":"$upstream_cache_status",'
'"host":"$host",'
'"ssl_cipher":"$ssl_cipher",'
'"bytes_sent":"$bytes_sent",'
'"connection":"$connection",'
'"connection_requests":"$connection_requests",'
'"content_length":"$content_length",'
'"content_type":"$content_type",'
'"msec":"$msec",'
'"remote_port":"$remote_port",'
'"remote_user":"$remote_user",'
'"request_method":"$request_method",'
'"request_uri":"$request_uri",'
'"scheme":"$scheme",'
'"server_addr":"$server_addr",'
'"server_name":"$server_name",'
'"server_protocol":"$server_protocol",'
'"time_iso8601":"$time_iso8601",'
'"time_local":"$time_local",'
'"hostname":"$hostname",'
'"request_body": "->$request_body"'
'}';
access_log logs/mainlog/$host-$server_port-$scheme-access.log main_body;
3、隐藏版本信息
nginx漏洞一般都是在某些特定版本中出现,所以尽量不要将nginx的版本信息暴露出去。然后我们通过如下配置进行隐藏。
- 1、打开conf/nginx.conf配置文件;
- 2、在server栏目下,配置server_tokens项 server_tokens off;
server_tokens off; #隐藏版本信息
修改前请求响应头中包含nginx版本信息,如下图
修改后版本信息被去掉如下图
4、Nginx后端服务指定的Header隐藏状态
当nginx作为反向代理时,会配置很多站点,为了不想泄露后端webserver主机信息,可以在http全局下配置隐藏Nginx后端服务X-Powered-By(php特有的)头。隐藏Nginx后端服务指定Header的状态,可以使用proxy_hide_header(或Lua模块)来隐藏/删除上游服务器返回到你的nginx反向代理(并最终返回到客户端)的头文件:
- 1、打开conf/nginx.conf配置文件;
- 2、在http下配置proxy_hide_header项; 增加或修改为 proxy_hide_header X-Powered-By; proxy_hide_header Server;
proxy_hide_header X-Powered-By; #隐藏php的标头
proxy_hide_header Server;
5、SSL策略进行加固
配置此项请确认nginx支持OpenSSL,运行nginx -V 如果返回中包含built with OpenSSL则表示支持OpenSSL,Nginx SSL协议采用TLSv1.2,只需在server模块下配置对应的协议版本即可。否则无需配置
执行如下命令进行查看
./sbin/nginx -V
在server模块下配置
- 1、打开conf/nginx.conf配置文件;
- 2、在server栏目下,配置ssl_protocols项 ssl_protocols TLSv1.2;
ssl_protocols TLSv1.2;
6、设置连接超时时间
保护服务器资源,硬件CPU mem,连接数。建立连接也是要消耗资源的,我们一般断掉那些连上的链接,但是不做事的php网站建议短连接,PHP程序建立连接消耗的资源和时间要少。JAVA网站建议长连接,JAVA程序建立连接消耗的资源和时间要多。
在server模块下配置
- 1、打开conf/nginx.conf配置文件;
- 2、在http模块下,配置如下配置,具体时间根据业务需要进行调整;
keepalive_timeout 60;
####设置客户端连接保持会话的超时时间,超过这个时间,服务器会关闭该连接。
tcp_nodelay on;
####打开tcp_nodelay,在包含了keepalive参数才有效
client_header_timeout 15;
####设置客户端请求读取超时时间,如果超过这个时间,客户端还没有发送任何数据,Nginx将返回"Request time out (408)" 错误
client_body_timeout 15
####设置客户端请求主题读取超时时间,如果超过这个时间,客户端还发送任何数据,Nginx将返回"Request time out (408)" 错误
send_timeout 15;
####指定响应客户端的超时时间,这个超过仅限于两个连接活动之间的时间,客户端没有任何活动,Nginx将会关闭连接
7、上传文件大小配置
防止不良用户上传大文件导致服务器内存瘫痪,nginx一般是用户触达应用服务器的第一道门槛(一般还有LSB,WAF这些,一般大公司才会用),所以要做好防护。
在http模块下配置
- 1、打开conf/nginx.conf配置文件;
- 2、在http模块下,配置如下配置,具体时间根据业务需要进行调整;
client_max_body_size 10m;
8、注意事项
以上每个配置项配置完成保存后最好进行检查一下,然后重启nginx服务,命令如下:
- 1、配置文件格式检查
出现下图的successful说明格式没问题,然后就可以执行重启操作了。
./sbin/nginx -t
- 2、重启服务
./sbin/nginx -s reload