您的位置:首页 > 运维架构 > Nginx

nginx安全加固

2015-11-05 16:24 543 查看
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
极大程度上可以防止提权篡改
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nginx 安全