nginx安全加固

nginx模块 

http_substitutions_filter_module

modsecurity

Tengine支持了动态加载模块

增加modsecurity模块  

modsecurity倾向于过滤和阻止web危险

http://waringid.blog.51cto.com/65148/1629905

关闭服务器标记  http{server_tokens off} 隐藏nginx版本

自定义缓存

server

{

client_body_buffer_size  16K;

client_header_buffer_size  1k;

client_max_body_size  1m;

large_client_header_buffers  4  8k;

}

设置timeout设低来防御DOS攻击

http

{

client_body_timeout   10;

       client_header_timeout  30;

       keepalive_timeout     30  30;

       send_timeout          10;

}

限制访问的方法

server

{

if($request_method !~ ^(GET|HEAD|POST)$) {        

                     return404;

              }

}

实际中应该使用444

日志切割

kill-USR1 $(cat /var/run/nginx/nginx.pid)

模块 ngx_http_access_module 允许限制某些IP地址的客户端访问

location/ {

    deny  192.168.1.1;

    allow 192.168.1.0/24;

    allow 10.1.1.0/16;

    allow 2001:0db8::/32;

    deny  all;

}


在原来的编译参数的首行加入--add-module=/root/install/naxsi-core-0.51-1/naxsi_src

nbs.rules文件


增加IP限制功能

模块ngx_http_limit_conn_module和来ngx_http_limit_req_module


http {

limit_req_zone$binary_remote_addr zone=one:10m rate=10r/s;

limit_conn_zone$binary_remote_addr zone=two:10m;

     }

server{

limit_req zone=one burst=5;

limit_conn two 15;

}


增加WAF模块

增加fail2ban防止非法IP

ngx_http_stub_status_module  模块


Nginx模块GeoIP匹配处理IP所在国家、城市

if ( $geoip_country_code !~  ^(CN|US)$ ) {

        return 403;

}


自定义nginx版本号

http://purplegrape.blog.51cto.com/1330104/1291871


封杀各种user-agent

if ($http_user_agent ~* "java|python|perl|ruby|curl|bash|echo|uname|base64|decode|md5sum|select|concat|httprequest|httpclient|nmap|scan" ) {

    return 403;

}

if ($http_user_agent ~* "" ) {

    return 403;

}


封杀特定的url

location ~* \.(bak|save|sh|sql|mdb|svn|git|old)$ {

rewrite ^/(.*)$  $host  permanent;

}


location /(admin|phpadmin|status)  { deny all; }


封杀特定的http方法和行为

if ($request_method !~ ^(GET|POST|HEAD)$ ) {

    return 405;

}

 

if ($http_range ~ "\d{9,}") {

    return 444;

}


强制网站使用域名访问,可以逃过IP扫描

if ( $host !~* 'abc.com' ) {

    return 403;

}


url 参数过滤敏感字

if ($query_string ~* "union.*select.*\(") { 

    rewrite ^/(.*)$  $host  permanent;

 

if ($query_string ~* "concat.*\(") { 

    rewrite ^/(.*)$  $host  permanent;

}


强制要求referer

if ($http_referer = "" ) {

    return 403;


老外的配置

set $add 1;

           location /index.php {

                   limit_except GET POST {

                        deny all;

               }

               set $ban "";

               if ($http_referer = "" ) {set $ban $ban$add;}

               if ($request_method = POST ) {set $ban $ban$add;}

               if ($query_string = "action=login" ){set $ban $ban$add;}

               if ($ban = 111 ) {

                   access_log /var/log/[133]nginx/ban IP;

                   return 404;

               }

               proxy_pass http://127.0.0.1:8000; #here is a patch

           }


目录只读

mount --bind /data /var/www/html

mount -o remount,ro --bind /data /var/www/html

极大程度上可以防止提权篡改




本文转自 liqius 51CTO博客,原文链接:http://blog.51cto.com/szgb17/1710068,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值