登录  /  注册
access_log - 如何在nginx中忽略某些php动态请求的log
php中文网
发布: 2016-06-06 20:52:02
原创
719人浏览过

我的服务器是使用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;
    }
登录后复制
登录后复制

回复内容:

我的服务器是使用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 如果需要的就存放到正常的日志里面 就可以了。

相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学