S
S
Sergey Sokolov2019-11-30 15:18:25
Nginx
Sergey Sokolov, 2019-11-30 15:18:25

How to disable nginx log for one route in Laravel/Lumen application?

nginx, php-fpm, Lumen
nginx config:

upstream php {
  server  php-fpm:9000;
}
server {
    # ...
    root   /var/www/lumen/public;    
    try_files $uri $uri/ /index.php$is_args$args;
    # ...
    location ~ \.php$ {
      include fastcgi.conf;
      fastcgi_pass php;
    }
}

I would like to disable logging of requests for one route. Something like
location  /api/servertime {
  access_log off;
}

But this is how the route stops working. If you add fastcgi parameters to it as for php, the route works, but everything still gets into the logs.
I also tried to make a nested location under php:
location ~ \.php$ {
  location  ~ ^/api/servertime$ { access_log off; }

But everything still gets into the logs.
How right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dodo512, 2019-11-30
@sergiks

location = /api/servertime {
    access_log off;
    rewrite ^ /index.php break;
    include fastcgi.conf;
    fastcgi_pass php;
}

Another option is to add a condition tolocation ~ \.php$
location ~ \.php$ {
    if ($request_uri ~ "^/api/servertime") {
        access_log off;
    }
    include fastcgi.conf;
    fastcgi_pass php;
}

nginx.org/ru/docs/http/ngx_http_log_module.html#ac...
From version 1.7.0 added the ability to write to the log by condition The access_log путь [формат [if=условие]];
request will not be written to the log if the result of the condition is “0” or an empty string.
map $request_uri $loggable {
    default            1;
    ~^/api/servertime  0;
}
server {
    # ...
    root   /var/www/lumen/public;    
    try_files $uri $uri/ /index.php$is_args$args;
    # ...
    location ~ \.php$ {
      access_log /var/log/nginx/access.log combined if=$loggable;
      include fastcgi.conf;
      fastcgi_pass php;
    }
}

Z
Zolg, 2019-11-30
@Zolg

location /api/servertime {
access_log off;
include fastcgi.conf;
fastcgi_pass php;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question