開發者常常有這樣的需求,測試的IP或者站長的IP不要寫入nginx的access.log中,這樣會影響日誌閱讀的質量。讀者第一個想法就是在nginx.conf中配置,如果測試IP和站長的IP不寫入access.log,但是常常有這樣的情況,比如博主的公司有一個IP池,博主自己也搞不清有多少IP,這樣就無法設置通過IP來控制日誌。博主想了一個方法,在站長的瀏覽器端種下cookie,如果nginx解析到這個cookie,則不寫入access.log。
nginx.conf如下設置
location =/
{if ($http_cookie ~ 'nolog') {
access_log off;
}
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://localhost:8080$lang;
}
可以在網站中放一個servlet專門種客戶端瀏覽器的cookie
public class NoNginxLogServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException {
Cookie cookie = new Cookie("nolog", "nolog");
cookie.setMaxAge(60 * 60 * 24 * 365);
cookie.setPath("/");
response.addCookie(cookie);
}
}
http://www.findmaven.net 是博主的網站,是一個findjar和findmaven的搜索引擎,可以根據class名或者jar名找到包含它的jar和maven gav
種下cookie後,訪問網站就提交了這個nolog cookie,這樣這次請求就不會在access.log中留下痕跡。