日誌
屏蔽user-agent並屏蔽日誌
| 1 2 3 4 5 6 7 8 | if ($http_user_agent ~* (^badbot/useragent$)) {     rewrite (.*) /badbot break; } location = /badbot {     access_log off;     return 403; } | 
不屏蔽user-agent(允許其訪問),但屏蔽日誌
| 1 2 3 4 5 6 7 | server {     set $is_spider 1;     if ($http_user_agent ~* "bot|spider|Bot|Disqus|WebIndex|YunGuanCe") {         set $is_spider 0;     }     access_log /var/log/nginx/access.log combined if=$is_spider; } | 
按uri屏蔽日誌(可以和上面的按user-agent用同一個變量來同時過濾uri和user-agent)
| 1 2 3 4 5 6 7 8 9 10 11 | map $request $loggable {     ~/favicon.ico 0;     ~/images/* 0;     ~/js/* 0;     ~/css/* 0;     default 1; } server {     access_log /var/log/nginx/access.log combined if=$loggable; } | 
Header
按mime type設置緩存時間
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | map $sent_http_content_type $cacheable_types {     "text/css"    "max-age=14400";     "text/plain"  "max-age=86400";     "image/jpeg"  "max-age=86400";     "image/png"   "max-age=86400";     "image/gif"   "max-age=86400";     "image/x-icon" "max-age=86400";     "application/x-7z-compressed" "max-age=864000";     "application/x-javascript" "max-age=86400";     "application/json" "max-age=86400";     "application/x-bittorrent" "max-age=864000";     default       ""; } server {     location / {         root   "/var/www/html/";                    add_header "Cache-Control" $cacheable_types;     } } | 
防攻擊
簡單的無狀態cookie challenge(需要lua-nginx-module)
crawlers塊中可以手動填寫要屏蔽的IP
將其中的s改成隨機字符串+時間戳可以變成有狀態版本(需使用redis/memcached/shared memory存儲生成的隨機字符串)
將set-cookie改成通過js生成cookie可以變成javascript challenge,注意要在js里加上瀏覽器上下文判斷,如var cookie=location.protocol?cookie:””; 或者DOM操作
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | geo $crawlers{     1.2.3.0/24 1;     4.5.6.7 1;     default 0; } server {     location ~ \.php {         set $_c 0;         if ($http_user_agent ~* "bad.guy/123") {             set $_c 1;         }                     if ($crawlers){             set $_c 1;         }         if ($_c){             access_log     off;             access_by_lua_block {                 local expires = 90                 local s = ngx.time()                 local cob = tonumber(ngx.var.cookie_cob)                 if cob == nil then cob=0 end                 local coa = ngx.md5("saltsalt" .. ngx.var.remote_addr .. "saltsalt" .. cob)                 if s - cob > expires or ngx.var.cookie_coa ~= coa then                     coa = ngx.md5("saltsalt" .. ngx.var.remote_addr .. "saltsalt" .. s)                     ngx.header["Set-Cookie"] = {"coa=" .. coa .."; path=/; domain=.example.com; HttpOnly",                         "cob=" .. s.."; path=/; domain=.example.com; HttpOnly"}                     ngx.header["Content-Type"] = "text/html"                     ngx.say("<meta http-equiv ='refresh' content='0'>");                     ngx.exit(200)                 end             }         }         # fastcgi_pass     } } | 
其他
植入cookie
需要注意的是使用ngx.time()產生秒級的時間,用來做隨機數種子可能會衝突,因此建議加上另外的隨機變量(如下面的例子用的是客戶端的ip) 可以使用ngx.now()產生毫秒精度時間
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | local modname = "util" local _M = { _VERSION = '0.01' } local mt = { __index = _M } function _M.random_str(l, seed, r)     local s = r or 'abcdefghijklmnhopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789=+-'     local ret ='' 	-- os.time() is precise to second 	math.randomseed(os.time() * 1000 + ngx.crc32_short(seed or ""))     for i=1 ,l do         local pos = math.random(1, string.len(s))         ret = ret .. string.sub(s, pos, pos)     end     return ret end function _M.plant_cookie(k, h) 	local ck = require "resty.cookie" 	local cookie, err = ck:new() 	if not cookie then 		ngx.log(ngx.ERR, "ERROR PLANTING COOKIE", err) 		return 	end 	local _, err = cookie:get(k) 	if _ then 		return 	end 	local ok, err = cookie:set({ 		key = k, value = _M.random_str(32, ngx.var.remote_addr, '0123456789abcdef'), path = "/", 		domain = h or ngx.var.http_host, 		httponly = true, 		expires= 'Thu, 31-Dec-37 23:55:55 GMT', 		max_age = 2147483647 	}) end return _M | 
| 1 2 3 4 5 6 | location / {     access_by_lua '         local _ = require("util")         _.plant_cookie("cookie_name",".example.come")     '; } | 
 
	
    
爸爸 我來前排跪舔
好厲害的樣子呀!!
[害羞]
nginx: [emerg] unknown “crawlers” variable $crawlers ???
$crawlers ???
哈哈這個變量用來手動屏蔽IP,比如geo $crawlers{ 1.2.3.0/24 1; 4.5.6.7 1; default 0;}
多謝提醒
反正都在nginx層,沒什麼壓力,你可以加個limit_req_rate
最近碰到個好奇葩的問題啊,大佬有沒有碰到過 Nginx 經常不處理請求詳細http://bbs.csdn.net/topics/392008066
Nginx 經常不處理請求詳細http://bbs.csdn.net/topics/392008066
貼下日誌嘛
沒啥日誌,nginx一些緩存文件未找到的日誌2016/08/23 22:08:57 [crit] 29891#0: unlink() “/tmp/wpcache/8/84/61ea03f64ead57389493cea88df29848” failed (2: No such file or directory)PHP無錯誤日誌
簡化了下配置文件,去除memcahed緩存,去除pagespeed優化。問題依舊 更新後配置文件 http://bbs.csdn.net/topics/392008066
 更新後配置文件 http://bbs.csdn.net/topics/392008066
哦我想起來我的博客之前也有這貓餅,感覺是php的opcache配置有問題
應該不是吧,抽風期間我連靜態文件都沒法訪問。
有可能,看了下,並沒有。附上幾張圖抽風時服務器監控圖。http://ww4.sinaimg.cn/large/ec76730dgw1f74aave54ej20wg0don1j.jpg一個是CPU資源,抽風時沒處理請求 負載0,,抽風完畢後等待的請求一股腦衝進來 負載飆升。然後是最重要的連接數,抽風時連接數太不正常了。關於磁盤與網絡http://ww1.sinaimg.cn/large/ec76730dgw1f74acj1vudj20wk06lac0.jpg
看圖沒看懂,20:44開始抽風?難道有人在cc你
不,只是ngixn炸了。抽風時不處理請求=負載0,,抽風完畢後抽風時期等待的請求一股腦衝進來=負載飆升。這就是負載坐過山車一樣
我怎麼感覺和nginx沒關係
啊…..我一直認為是nginx的問題,其他東西還能阻止訪問靜態文件么?
在抽風時試着重啟PHP,竟然。。。管用…竟然是後端的問題。
感覺扔的東西越來越多了….memcached也一用PHP就出問題
不知道,我一個一個丟的丟到opcache那就沒問題了 誒不對我是php7,你用的是7不
誒不對我是php7,你用的是7不
對呀對呀