故障描述:
在添加Nginx的子配置文件后报错误nginx: [emerg] unknown log format "main"
无法重新加载,仔细查看配置没有语法错误经过调试才发现是定义log_format的时候写到HTTP模块最下面,导致子配置文件无法识别。
错误的写法
include /opt/app/nginx/conf/conf.d/*.conf; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /opt/logs/nginx/access.log main; }
我是先引入了子配置文件然后才定义日志格式,所以报无法识别
解决方法:
将log_format 写到http开头
http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /opt/logs/nginx/access.log main;