要查看 Nginx 的 log 文件,先要查看 Nginx 的配置文件位置,使用以下命令查看
1 2 3 4 5
| nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
|
以上输出代表 Nginx 所使用的配置文件是 /etc/nginx/nginx.conf。
在 Nginx 中,日志文件分为 access.log 和 error.log,分别代表访问 nginx 的日志和错误日志。在配置文件中会以以下的方式去配置日志路径:
1 2
| error_log logs/error.log info; access_log logs/access.log info;
|
如果配置文件里面路径是以相对路径去显示,那么日志文件的路径就以 编译参数指定路径 + 配置文件里的相对路径。
如果是绝对路径,那么日志文件就会生成到这个路径里:
1 2
| error_log /var/nginx/logs/error.log info; access_log /var/nginx/logs/access.log info;
|
那么,编译参数指定路径是怎么获取的呢?通过以下命令获取:
1 2 3 4 5
| nginx -V 2>&1 | grep --color -- '--prefix'
configure arguments: --with-cc-opt='-g -O2 -ffile-prefix-map=/build/nginx-aSXKE0/nginx-1.18.0=. -flto=auto -ffat-lto-objects -flto=auto -ffat-lto-objects -fstack-protector-strong -Wformat-Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -flto=auto -ffat-lto-objects -flto=auto -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-compat --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_r ealip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --add-dynamic-module=/build/nginx-aSXKE0/nginx-1.18.0/debian/modules/http-geoip2 --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_sub_module
|
该命令会筛选--prefix的字符串并高亮显示,这样就可以获取到 Nginx 的 prefix 编译参数,然后再与上一步配置文件中的相对路径进行拼接,这样就获取到了 Nginx 的日志全路径;
如果得出的路径还没有日志,则可以获取 --http-log-path 和 --error-log-path 这 2 个编译参数值,也是查找路径之一;
如果通过包管理器(如 apt/yum)安装 Nginx,默认会标准化路径(如 /var/log/nginx/),直接遵循该规则即可。
如果配置文件中使用了绝对路径(如 /var/log/error.log),日志文件一定会生成到该路径;如果使用了相对路径(如 logs/error.log),则实际路径是 编译时的 –prefix + 相对路径。
配置文件中的绝对路径优先级最高,编译参数无法覆盖它。