不记录对某些 php 脚本的访问

不记录对某些 php 脚本的访问

我想从 access.log 中排除对某些 php 脚本的访问。为此,我执行了以下操作:

 location ~ ^/lib/exe/(jquery|taskrunner|css|js)\.php$ {
        access_log off; 
        #access_log /var/log/nginx/test_access.log;
        try_files /dev/null/1 @processphp;
    }   

    location ~ \.php$ {
        try_files /dev/null/1 @processphp;
    }   

    location @processphp {
        if (!-f $request_filename) { return 404; }
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param REDIRECT_STATUS 200;
    
        # send to fastcgi
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        include snippets/fastcgi-php.conf;    
    }

但不幸的是,它没有起作用。 /lib/exe/jquery.php /lib/exe/js.php /lib/exe/css.php /lib/exe/taskrunner.php 仍然在access.log中。

请告诉我,我怎样才能做我想做的事?

答案1

我想我成功了!

http {
...
    map $request_filename $log {
        default 1;
       "~/(jquery|js|css|taskrunner)\.php" 0;
    }
...

server {
    ...
    location ~ \.php$ {
            access_log /var/log/nginx/access.log combined if=$log;
            if (!-f $request_filename) { return 404; }
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param REDIRECT_STATUS 200;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
            include snippets/fastcgi-php.conf;
    }
...

但如果有人提供更好的选择我会很高兴!

文档:

相关内容