提高Nginx服务器安全性、稳定性和性能的12个操作
也许你已经听过以上关于Nginx的神奇事情,也许你已经非常喜欢Nginx并且想知道如何提高Nginx服务器的安全性、稳定性,或者如果你正在考虑用 Nginx 替换 Apache,那么这篇文章非常适合进一步阅读。
本文将介绍用于提高 Nginx 服务器安全性、稳定性和性能的 12 个操作。
1:保持Nginx及时升级
Nginx当前稳定版本为1.14.0。最好升级到最新版本。查看官方发行说明,您会发现他们修复了很多错误。任何制造的产品环境都不希望暴露于此类错误的风险中。另外,虽然安装包比从源代码编译更容易,但第二个选项有两个优点:
- 1)允许您向 Nginx 添加更多模块(如 more_header、mod_security),
- 2)始终提供更新的版本比安装包。您可以在 Nginx 网站上查看发行说明。
2:删除未使用的 Nginx 模块
编译安装时,在执行 ./configure 方法时添加以下配置指令,以显式删除未使用的模块:
./configure --without-module1 --without-module2 --without-module3例如:./configure --without-http_dav_module --withouthttp_spdy_module#注意事项:配置指令是由模块提供的。确保你禁用的模块不包含你需要使用的指令!在决定禁用模块之前,应该检查Nginx文档中每个模块可用的指令列表。3:禁用 Nginx 配置中的 server_tokens 当该项 时3 server_tokens 启用,404 页面会显示当前 Nginx 版本号。这显然是危险的,因为黑客可以利用这些信息来测试相应 Nginx 版本中的漏洞。只需关闭nginx.conf中http模块中的server_tokens即可,例如: server { listen 192.168.0.25:80; Server_tokens off; server_name tecmintlovesnginx.com www.tecmintlovesnginx.com; access_log /var/www/logs/tecmintlovesnginx.access.log; error_log /var/www/logs/tecmintlovesnginx.error.log error; root /var/www/tecmintlovesnginx.com/public_html; index index.html index.htm;}#重启Nginx后生效:
server { listen 192.168.0.25:80; Server_tokens off; server_name tecmintlovesnginx.com www.tecmintlovesnginx.com; access_log /var/www/logs/tecmintlovesnginx.access.log; error_log /var/www/logs/tecmintlovesnginx.error.log error; root /var/www/tecmintlovesnginx.com/public_html; index index.html index.htm;}#重启Nginx后生效:4:禁止非法HTTP User Agents
User Agent是HTTP协议中的浏览器标识,禁止非法User Agents,可以屏蔽一些爬虫的请求并且扫描器对这些请求并没有消耗大量的Nginx服务器资源。为了更好地维护,最好创建一个包含不需要的用户代理列表的文件。例如 /etc/nginx/blockuseragents.rules 包含以下内容:
map $http_user_agent $blockedagent { default 0; ~*malicious 1; ~*bot 1; ~*backdoor 1; ~*crawler 1; ~*bandit 1;}然后将如下语句放入配置文件的server模块内:include /etc/nginx/blockuseragents.rules;并加入if语句设置阻止后进入的页面:5:禁用不必要的 HTTP 方法
例如,某些网站和应用程序可能只支持 GET、POST 和 HEAD 方法。可以通过在服务器模块的配置文件中添加以下方法来防止一些欺骗攻击
if ($request_method !~ ^(GET|HEAD|POST)$) {return 444;}6:设置缓冲区容量上限
这样的设置可以防止缓冲区溢出攻击(服务器模块也是如此)
client_body_buffer_size 1k;client_header_buffer_size 1k;client_max_body_size 1k;large_client_header_buffers 2 1k;#设置后,不管多少HTTP请求都不会使服务器系统的缓冲区溢出了。7:限制最大连接数
- 在http模块和外部服务器模块中设置limit_conn_zone,可以设置连接的IP
- 在http模块、服务器或位置中设置limit_conn,可以设置最大IP连接数。例如:
limit_conn_zone $binary_remote_addr zone=addr:5m;limit_conn addr 1;8:设置日志监控
上面的截图已经存在。如何设置nginx日志
你可能需要获取由于第7点的设置而无法访问的日志
grep addr /var/www/logs/tecmintlovesnginx.error.log --color=auto同时你还可以过滤日志中的以下内容:
- 客户端IP
- 浏览器类型
- 方法HTTP请求
- 内容请求
- 服务器响应
9:避免链接来自服务器的图像,这当然会增加服务器的压力。 。假设你有一个img目录来存储图像,你自己的IP是192.168.0.25。为了防止外部链接,请添加以下配置 location /img/ { valid_referers none blocked 192.168.0.25; if ($invalid_referer) { return 403; }}
10:禁用 SSL,仅启用 TLS
location /img/ { valid_referers none blocked 192.168.0.25; if ($invalid_referer) { return 403; }}尽可能避免使用 SSL。如果您想使用 TLS,可以在服务器模块中进行以下设置:
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;11:执行证书加密 (HTTPS)
首先生成密钥和整数,使用以下任一选项:
# openssl genrsa -aes256 -out tecmintlovesnginx.key 1024# openssl req -new -key tecmintlovesnginx.key -out tecmintlovesnginx.csr# cp tecmintlovesnginx.key tecmintlovesnginx.key.org# openssl rsa -in tecmintlovesnginx.key.org -out tecmintlovesnginx.key# openssl x509 -req -days 365 -in tecmintlovesnginx.csr -signkey tecmintlovesnginx.key -out tecmintlovesnginx.crt#然后配置Server模块server { listen 192.168.0.25:443 ssl; server_tokens off; server_name tecmintlovesnginx.com www.tecmintlovesnginx.com; root /var/www/tecmintlovesnginx.com/public_html; ssl_certificate /etc/nginx/sites-enabled/certs/tecmintlovesnginx.crt; ssl_certificate_key /etc/nginx/sites-enabled/certs/tecmintlovesnginx.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2;}12:HTTPS 的 HTTP 重定向请求
在第11点基础上增加return 301 https://$server_name$request_uri;版权声明
本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。
code前端网