Nginx報錯nginx: [emerg] unknown log format "main"
故障描述:
在新增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;
解決nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed錯誤
重新啟動伺服器,訪問web服務發現無法瀏覽啦!登陸伺服器之後進到nginx使用./nginx -s reload重新讀取配置檔案,發現報nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)錯誤,進到logs檔案發現的確沒有nginx.pid檔案
[[email protected] sbin]# ./nginx -s reload
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
解決方法:
[[email protected] nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
使用nginx -c的引數指定nginx.conf檔案的位置
[[email protected] nginx]# cd logs/
[[email protected] logs]# ll
總用量 12
-rw-r--r-- 1 root root 1246 12月 9 18:10 access.log
-rw-r--r-- 1 root root 516 12月 10 15:39 error.log
-rw-r--r-- 1 root root 5 12月 10 15:38 nginx.pid
看nginx.pid檔案已經有了。