主配置文件 /etc/nginx/nginx.conf 中的log_format main 定义了日志的格式
nginx内置变量: https://github.com/aminglinux/nginx/blob/master/rewrite/variable.md
虚拟机配置文件中的 access_log /var/log/nginx/host.access.log main; 路径可以改,激活并放到配置文件的最下方
此处把日志路径改为 access_log /data/logs/bbs.access.log
selinux 开启状态,需要修改日志文件的上下文,查询nginx可以访问的上下文
[root@CentOS-1 ~]# semanage fcontext -l | grep nginx ==========查询一个服务的上下文
/etc/nginx(/.)? all files system_u:object_r:httpd_config_t:s0
/var/run/nginx. all files system_u:object_r:httpd_var_run_t:s0
/var/lib/nginx(/.)? all files system_u:object_r:httpd_var_lib_t:s0
/var/log/nginx(/.)? all files system_u:object_r:httpd_log_t:s0 =====此处为日志文件格式
/usr/share/nginx/html(/.)? all files system_u:object_r:httpd_sys_content_t:s0
/var/opt/rh/rh-nginx18/log(/.)? all files system_u:object_r:httpd_log_t:s0
/etc/opt/rh/rh-nginx18/nginx(/.)? all files system_u:object_r:httpd_config_t:s0
/usr/lib/systemd/system/nginx. regular file system_u:object_r:httpd_unit_file_t:s0
/var/opt/rh/rh-nginx18/lib/nginx(/.)? all files system_u:object_r:httpd_var_lib_t:s0
/var/opt/rh/rh-nginx18/run/nginx(/.)? all files system_u:object_r:httpd_var_run_t:s0
/usr/sbin/nginx regular file system_u:object_r:httpd_exec_t:s0
[root@CentOS-1 ~]# chcon -R --reference=/var/log/nginx /data/logs/
[root@CentOS-1 ~]# ll -Z /data/logs/bbs.access.log
-rwxrwxrwx. root root system_u:object_r:httpd_log_t:s0 /data/logs/bbs.access.log 此时nginx服务可以访问这个日志文件了
日志里面不记录静态文件:
在访问日志里,过滤掉一些图片、js、css类的请求日志,因为这样的请求日志没什么用,而且会占用很大的磁盘空间
配置方式
在虚拟机配置文件里增加配置:
location ~* .(png|jpeg|gif|js|css|bmp|flv)$
{
access_log off;
}
补充:
tail -f /data/logs/bbs.access.log //-f选型可以动态查看一个文件的内容
可以清空一个文件内容
~* 表示不区分大小写的匹配 后面跟正则表达式 .表示任意一个字符