2025年3月15日 星期六 甲辰(龙)年 月十四 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 服务器 > Nginx

Nginx防御与加固

时间:04-02来源:作者:点击数:33

1、日志配置

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

  • cat /etc/nginx/nginx.conf
image-20210520103707639

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” ./进行搜索。

image-20210520105905524
  • #将错误页面重定向到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
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门