nginx操作cookie使测试IP不写下access.log

nginx操作cookie使测试IP不写入access.log

开发者常常有这样的需求,测试的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

nginx操作cookie使测试IP不写下access.log

 

 

种下cookie后,访问网站就提交了这个nolog cookie,这样这次请求就不会在access.log中留下痕迹。


nginx操作cookie使测试IP不写下access.log