Linux - 关于nginx配置文件开启访问日志,但是,没有写入日志文件及内容的问题

一、情景再现

1、配置文件/nginx/conf/nginx.conf
worker_processes  1;

events {
    worker_connections  1024;
}


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

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    include sites-enabled/*.conf;
}
2、单个项目配置文件/nginx/conf/sites-enabled/jean.conf
server {
  listen 80;

  server_name www.jean.com;
  root /mine/serve/project;

  location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
    access_log off;
    expires max;
  }

  access_log  logs/access.jean.log  main;

  location / {
        index index.html;
        autoindex on;
        autoindex_exact_size on;
        autoindex_localtime on;
  }

  location ~ /\.ht {
    deny  all;
  }

  location ~ \.sh$ {
    deny  all;
  }
}
3、测试配置
[root@192 sbin]# ./nginx -t
nginx: the configuration file /mine/serve/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /mine/serve/nginx/conf/nginx.conf test is successful
4、访问域名
正常输出!
5、查看日志
没有生成日志文件
[root@192 logs]# ls
access.log  error.log  nginx.pid

二、发现问题

1、当个项目配置文件/nginx/conf/sites-enabled/jean.conf
#可疑代码行
=-----------------------------------------------------=
  location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
    access_log off;
    expires max;
  }
=-----------------------------------------------------=
注意:以上代码的意思是,所有访问是jpg|jpeg|gif|png、css|js|ico、html文件,都不写入日志,缓存都是保持最大!
2、解决问题
注释掉“access_log off;”这一行:“# access_log off;”,然后,重启nginx服务
并且,将nginx.conf文件,定义log_format的命令行打开(去掉前面的“#”)
=-----------------------------------------------------------------------=
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';
=-----------------------------------------------------------------------=
3、再次访问域名,查看日志
果然,生成日志
[root@192 logs]# ls
access.jean.log  access.log  error.log  nginx.pid
[root@192 logs]# ll
总用量 12
-rw-r--r--. 1 root root  194 11月 23 09:58 access.jean.log
-rw-r--r--. 1 root root    0 11月 23 03:12 access.log
-rw-r--r--. 1 root root 1442 11月 23 09:58 error.log
-rw-r--r--. 1 root root    4 11月 23 03:12 nginx.pid
有内容写入!!

4、可能有人会说,将“access_log  logs/access.jean.log  main;”放到【#可疑代码行】的上面,不行吗?不行,会创建一个access.jean.log文件,但是,不会写入内容!

三、问题总结

关于日志报错,一般有两个问题:
1、是开启日志,却没有生成日志,这种情况,首先,确认你有没有开启日志命令行,如“access_log  logs/access.jean.log  main;”,有,那就看你主配置文件nginx.conf和项目配置文件"/nginx/conf/sites-enabled/jean.conf"有没有"access_log off;"的命令行,若有,注释掉,重启nginx服务,一般没问题。
2、报“nginx: [emerg] "log_format" directive is not allowed here in /mine/serve/nginx/conf/sites-enabled/jean.conf:13”这种错误,通常"log_format"的定义必须在nginx.conf文件里,这个默认是注释的,开启即可,关于这个格式,你也可以在nginx.conf配置文件自定义,然后,在你的项目配置文件使用。
3、另外,日志格式"log_format"的定义,不能在项目配置文件,如“/nginx/conf/sites-enabled/jean.conf”中定义,否则,就会报错!
发布了59 篇原创文章 · 获赞 2 · 访问量 5561

猜你喜欢

转载自blog.csdn.net/LDR1109/article/details/103894875