禁用nginx中的特定请求登录

Ber*_*als 11 logging nginx

我试图根据请求URI"忽略"或重定向nginx日志中的某些请求.

到目前为止我有这个:

server {
  listen      80;
  #...
  access_log  /var/log/nginx/site.access;
  error_log   /var/log/nginx/site.error info;
  #...

  try_files $uri @app

  location = /lbcheck.html {
    access_log off;
    log_not_found off;
    try_files $uri @app;
  }

  location @app {
    proxy_redirect     off;
    proxy_pass http://unix:/tmp/site.unicorn.sock;
    #...
  }

}
Run Code Online (Sandbox Code Playgroud)

基本上我希望代理的@app响应请求,/lbcheck.html但我不希望它登录/var/log/nginx/site.access;

我知道我可以用if ($uri = /lbcheck.html) { access_log off; }in 来做location @app但如果是邪恶的,所以我不想使用它们.

Tom*_*Tom 16

您可以使用条件记录执行此操作.它看起来像

map $request $loggable {
    ~*lbcheck\.html 0;
    default 1;
}
access_log  /path/logs/name.log if=$loggable;
Run Code Online (Sandbox Code Playgroud)

  • 需要在 .log 之间使用“组合” if= ```access_log /var/log/snaplink26.log 组合 if=$loggable;``` (4认同)
  • 但这似乎只有nginx 1.7.0才有可能. (2认同)

Dan*_*ack 6

我不确定是否有办法在Nginx的配置语法中做到这一点,我只是尝试过,如果没有if语句,它似乎不可能.

但是我确实知道以你想要的方式做这件事是违反Nginx关于如何编写配置文件的哲学.

您应该使用您喜欢的任何配置工具生成nginx.conf文件,而不是编写复杂的配置规则,这样实际的配置文件很简单,并且您的工具为您生成了所有复杂的位.

例如,生成nginx.conf的源文件应该类似于:

%START_APP_CONFIG%
    proxy_redirect     off;
    proxy_pass http://unix:/tmp/site.unicorn.sock;
%END_APP_CONFIG%


location = /lbcheck.html {
    access_log off;
    %INSERT_APP_CONFIG%
}

location @app {
    %INSERT_APP_CONFIG%
}
Run Code Online (Sandbox Code Playgroud)

虽然这可能看起来像是能够调整单个配置变量的很多工作,但它实际上使得在长(和中)期间使用配置文件更加愉快,因为您将始终生成易于阅读的配置文件并理解,而不是在他们中有复杂的条件.