nginx 关闭access.log 不成功

查看:845
本文介绍了nginx 关闭access.log 不成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

nginx 配置文件nginx.conf

user  www www;


worker_processes 8;

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

error_log  /home/wwwlogs/nginx_error.log  crit;

pid        /usr/local/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65536;

events
    {
        use epoll;
        worker_connections 65536;
    }

http
    {
        include       mime.types;
        default_type  application/octet-stream;

        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        open_file_cache max=102400 inactive=20s;
        open_file_cache_valid 30s;
        open_file_cache_min_uses 1;

        sendfile on;
        tcp_nopush     on;

        keepalive_timeout 90;

        tcp_nodelay on;

        fastcgi_connect_timeout 1200;
        fastcgi_send_timeout 1200;
        fastcgi_read_timeout 1200;
        fastcgi_buffer_size 256k;
        fastcgi_buffers 16 256k;
        fastcgi_busy_buffers_size 512k;
        fastcgi_temp_file_write_size 1024k;
        fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2
                keys_zone=TEST:10m
                inactive=5m;
                         


        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types       text/plain application/x-javascript text/css application/xml;
        gzip_vary on;
        gzip_proxied        expired no-cache no-store private auth;
        gzip_disable        "MSIE [1-6]\.";

        #limit_zone  crawler  $binary_remote_addr  10m;

        server_tokens off;
        #log format
        #log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
                 #'$status $body_bytes_sent "$http_referer" '
                 #'"$http_user_agent" $http_x_forwarded_for';
        
        # 关闭access.log
        access_log /dev/null;

server
    {
        listen       80;
        server_name aaa.abc.com;
        index index.html index.htm index.php;
        root  /home/wwwroot/abc.com;

        location ~ ^/(abc_status)$ 
            {
                include fcgi.conf;
                fastcgi_pass unix:/tmp/php-cgi.sock;
            }
        location ~ .*\.(php|php5)?$
            {
                try_files $uri =404;
                fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_index index.php;
                include fcgi.conf;
            }

        location /status {
            stub_status on;
            #access_log /dev/null;
        }
        location /guide{
            rewrite ^([^\.]*)/wenda/(\w+)/(\w+)\.html$ $1/index.php?r=wenda/$2/$3 last;
            rewrite ^([^\.]*)/([a-zA-Z]+)/([a-zA-Z]+)\.html$ $1/index.php?r=$2/$3 last;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
            {
                expires      30d;
            }

        location ~ .*\.(js|css)?$
            {
                expires      12h;
            }
    
    # 关闭access.log
    access_log off;

    # access_log  /home/wwwlogs/access.log  access;
    }
include vhost/*.conf;
}


所有子站点配置文件都没有配置access_log off

以下是一个子站点配置文件vhost/app3.abc.com.conf

server
    {
        listen       80;
        server_name app3.abc.com;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /home/wwwroot/abc.com/guide;
        location ~ .*\.(php|php5)?$
            {
                try_files $uri =404;
                fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_index index.php;
                include fcgi.conf;
            }
        location /{

            rewrite ^([^\.]*)/wenda/(\w+)/(\w+)\.html$ $1/index.php?r=wenda/$2/$3 last;

            #rewrite ^([^\.]*)/([a-zA-Z]+)/([a-zA-Z]+)\.html$ $1/index.php?r=$2/$3 last;        
                        
            if (!-e $request_filename) {
                return 404;
            }
        }
        
        location /ueditor{
            autoindex on;
            }        
        
        location ~ .*\.(gif|jpeg|png|bmp|swf)$
            {
                expires      30d;
            }

        location ~ .*\.(js|css)?$
            {
                expires      12h;
            }

        #access_log  /home/wwwlogs/visit.log  access;
        error_log  /home/wwwlogs/wenda_error.log  crit;
        }

有很多个子站点配置文件,都没有配置access_log off

通过上述配置之后,每次把nginx/logs/access.log日志文件删除后,第二天一来必定产生新的access.log日志文件,有1G左右大小。公司的需求是关闭nginx的access.log功能,也就是不让nginx记录访问日志。谁有更好的解决方案吗?非常感谢!

解决方案

答案已经找到。

其实网上说的 access_log /dev/null; 只是针对nginx的error_log而言的。如果想关闭error_log,那么就用error_log /dev/null(看老外是这么做的)。

但是想关闭access_log的话,直接在nginx.conf的http模块中配置access_log off;即可。需要注意的是,已经继承了http模块的那些模块不要再设置access_log off;了。 比如,我已经在http模块中设置了access_log off;,那么就不要在http模块所包含的server模块或location模块中设置access_log off;,以及虚拟主机目录(vhost)下的配置文件也不要添加access_log off;

纯生产环境实战经验。

这篇关于nginx 关闭access.log 不成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆