您好,欢迎来到99网。
搜索
您的当前位置:首页Nginx防御与加固

Nginx防御与加固

来源:99网

Nginx防御与加固



1、日志配置

默认是开启日志的,我们可以在nginx.conf中配置日志格式log_format,存放路径。

cat /etc/nginx/nginx.conf


2、禁止目录浏览

在/etc/nginx/nginx.conf中加上

autoindex off

保存,重启即可。


3、目录执行权限

在/etc/nginx/nginx.conf中:

# 去掉单个目录的php执行权限
location ~/attachements/.*\.(php|php5)?$ {
	deny all;
}

# 去掉多个目录的php执行权限
location ~/(attachments|upload)/.*\.(php|php5)?$ {
	deny all;
}

1、以上的配置文件代码需要放到location ~.php{…}上面,如果放到下面是无效的。

2、attachments需要写相对路径,不能写绝对路径。


4、错误页面重定向

cat /etc/nginx/conf.d/default.conf

也可能在其他配置文件,可以利用grep -rn “server_name” ./进行搜索。

#将错误页面重定向到404.html页面
error_page 400 401 402 403 404 405 408 410 412 413 414 415 500 501 502 503 504 506 /404.html;
location = /404.html {
	#放错误页面的目录路径
	root /var/www/html
}

5、Nginx其他防御方法

  • 隐藏版本信息,修改nginx.conf

    server_tokens off
    
  • HTTP请求方法,修改nginx.conf

    if($request_method !~^(GET|HEAD|POST)$) {
    	return 444;
    }
    
  • IP访问,修改nginx.conf

    location /admin {
    	deny 192.168.1.1;  #拒绝IP
    	allow 192.168.1.0/24;  #允许IP
        allow 10.1.1.0/16;  #允许IP
        deny all;   #拒绝其他所有IP
    }
    
  • 降权运行,修改nginx.conf

    user www-data
    

6、日志分析

360星图,awk

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 99spj.com 版权所有 湘ICP备2022005869号-5

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务