首页 > 开发 > php > 正文

如何在nginx中忽略某些php动态请求的log

2017-09-06 15:07:20  来源:网友分享

我的服务器是使用nginx搭建的,因为业务需要有一些请求是ajax定时刷新的,这部分请求产生的access_log对于我们来说完全是浪费空间,所以我想把它过滤掉,查了一些资料发现有这么个思路

location ^~ /index.php/test {    access_log off;}

因为我是用pathinfo做路由,所以rewrite以后的路径实际上这样,但是配置好以后发现,确实是不产生日志了,但是访问这个路径的php解析直接不执行了,也就是它跟我的php fastcgi配置产生了冲突

location ~ .*\.php(\/.*)*$ {    include fastcgi.conf;    fastcgi_param  PATH_INFO $fastcgi_script_name;    fastcgi_pass  127.0.0.1:9000;}

我想到了一种解决方案,那就是把动态解析的配置拷贝一份过去,比如

location ^~ /index.php/test {    access_log off;    include fastcgi.conf;    fastcgi_param  PATH_INFO $fastcgi_script_name;    fastcgi_pass  127.0.0.1:9000;}

但是这样看起来太臃肿了,如果我要忽略多个路径,那配置文件岂不是会很长,不知道大家有什么好办法。

按Ajian说的方法配置的nginx.conf
server {        listen       80;        server_name  example.com www.example.com;        root         /home/www/example/;        index        index.html index.htm index.php;        if (!-e $request_filename) {            rewrite ^(.*)$ /index.php$1 last;        }        set $my_log /usr/local/nginx/logs/example.log;        #if ($request_uri ~ ^/index.php/test) {        #       set $my_log /dev/null;        #}        location ~ .*\.php(\/.*)*$ {            include fastcgi.conf;            fastcgi_param  PATH_INFO $fastcgi_script_name;            fastcgi_pass  127.0.0.1:9000;        }        access_log /$my_log;    }

解决方案

我还没有实验 不过你可以把
location ^~ /index.php/test 修改成
location ~ /index.php/test
因为location如果匹配^~ 只会执行当前location区域 不会再执行其它了

一个hack的办法 哈哈 应该是满足你的需求了

server{    listen 80;    server_name www.test.com;    index index.php;    root /data0/www/web/;    set $logfile /var/log/test_access.log ;    if ($request_uri ~ ^/index.php/test){        set $logfile /dev/null ;}    location ~ .*\.php(\/.*)*$    {        fastcgi_pass  127.0.0.1:9000;        fastcgi_index index.php;        include fcgi.conf;    }   location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$    {        expires      30d;    }    location ~ .*\.(js|css)?$    {        expires      10d;    }      error_log   /var/log/test_err.log;    access_log /$logfile;}

因为location肯定会停止的 所以能够匹配的只有if语句 但是access_log不能放在if语句里面 所以可以用设置变量的方法将access_log日志存放的位置发生变化 如果是test就存放到/dev/null 如果需要的就存放到正常的日志里面 就可以了。