1,避免被針對版本直接使用漏洞
修改nginx.conf文件
在http模塊中添加如下信息
server_tokens off;
2,某些目錄為運維頁面,不要公開訪問
編輯nginx.conf
在server標簽內添加如下內容
location ~ /attachments/.*\.(php|php5)?$ {
deny all;
}
location ~ /(attachments|upload)/.*\.(php|php5)?$ {
deny all;
}
3,敏感目錄使用白名單訪問
修改nginx.conf文件
在server中添加
location /upload {
allow 192.168.1.0/24;
allow 10.1.1.1/32;
deny all;
}
4,防止通過瀏覽器直接查看目錄內容
編輯nginx.conf文件
在http模塊下添加一行內容
autoindex off;
5,制作重定向,防止默認頁面存在安全隱患
編輯nginx.conf
在server模塊下加入
error_page 404 /404.html;
location = /404.html {
root /usr/local/nginx/html;
}
6,修改日記格式,便于審計
編輯nginx.conf文件
在http模塊內啟用標簽main的log_format格式
log_format main ‘$remote_addr - $remote_user [$time_local] “$request” ‘ ‘$status $body_bytes_sent “$http_referer” ‘ ‘“$http_user_agent” “$http_x_forwarded_for”’;
在server標簽內調用
access_log logs/host.access.log main
7,只允許常用的get和post方法,減少漏洞
編輯nginx.conf
在server模塊中加入判斷信息
if ($request_method !~* GET|POST) {??????????? return 403;
}
8,減緩被ddos攻擊時資源消耗速度
編輯nginx.conf文件
在http模塊中聲明
limit_req_zone $binary_remote_addr zone=allips:10m rate=20r/s;
添加在server的location中
location / {
limit_req zone=allips burst=5 nodelay;
limit_rate 20k;
}
定義一個名為allips的limit_req_zone用來存儲session,大小是10M內存,
以$binary_remote_addr 為key,限制平均每秒的請求為20個,
1M能存儲16000個狀態,rete的值必須為整數,
如果限制兩秒鐘一個請求,可以設置成30r/m limit_req_zone $binary_remote_addr zone=allips:10m rate=20r/s;
限制每ip每秒不超過20個請求,漏桶數burst為5
brust的意思就是,如果第1秒、2,3,4秒請求為19個,
第5秒的請求為25個是被允許的。
但是如果你第1秒就25個請求,第2秒超過20的請求返回503錯誤。
nodelay,如果不設置該選項,嚴格使用平均速率限制請求數,
第1秒25個請求時,5個請求放到第2秒執行,
設置nodelay,25個請求將在第1秒執行。
limit_req zone=allips burst=5 nodelay; … } … } … }
例如:如果想設置用戶下載文件的前10m大小時不限速,大于10m后再以128kb/s限速可以增加以下配內容,修改nginx.conf文件
location /download {
limit_rate_after 10m;
limit_rate 128k; }
9,緩解ddos造成的影響
編輯nginx.conf文件
在http模塊中設置
client_body_timeout 10;
client_header_timeout 10;
keepalive_timeout 5 5;
send_timeout10;
10,防止通過其他途徑使用本網站資源
location ~* \.(jpg|jpeg|png|gif|bmp|swf|rar|zip|doc|xls|pdf|gz|bz2|mp3|mp4|flv)$ {
valid_referers none blocked 192.168.0.1 *.baidu.com;
if ($invalid_referer) {
rewrite ^/ https:
}
root /usr/share/nginx/img;
}
11,防止高權限運行nginx進程
編輯nginx.conf
在http模塊下修改
user nobody;
12,防止非法后綴被服務器識別
1) 將php.ini文件中的cgi.fix_pathinfo的值設為0
2) 將/etc/php5/fpm/pool.d/www.conf中security.limit_ectensions后面的值設為.php