了解Nginx安装部署以及虚机主机配置后,我们再来看看 log文件夹中的相关文件。

[root@NFS /]# cd /application/nginx/logs/
[root@NFS logs]# ll
total 36
-rw-r--r-- 1 root root 17949 Dec  1 14:58 access.log
-rw-r--r-- 1 root root  8355 Dec  1 14:58 error.log
-rw-r--r-- 1 root root     5 Dec  1 14:57 nginx.pid
[root@NFS logs]# 
  • error.log          #系统错误日志
    可通过查看 nginx.conf.default 文件 和官网。
    [root@NFS /]# head -n 10 /application/nginx/conf/nginx.conf.default 
    
    #user  nobody;
    worker_processes  1;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    [root@NFS logs]# 
    



    错误日志的,默认情况下不指定也没有关系,因为nginx很少有错误日志记录的。但有时出现问题时,是有必要记录一下错误日志的,方便我们排查问题。
       error_log 级别分为 debug, info, notice, warn, error, crit  默认为crit 。
       该级别在日志名后边定义格式如下:
       error_log  /yourPath/error.log crit;  
       
       crit 记录的日志最少,而debug记录的日志最多。
    如果nginx遇到一些问题,比如502比较频繁出现,但是看默认的error_log并没有看到有意义的信息,那么就可以调一下错误日志的级别,当你调成error级别时,错误日志记录的内容会更加丰富。
    配置如下:
    [root@NFS logs]# vim ../conf/nginx.conf
    
    worker_processes  1;
    error_log       logs/error.log error;      #添加这一行,不添加也会默认到/logs/error.log文件中
       。
       。
       。

     
  • access.log                 #用户访问日志文件
    查看 nginx.conf.default 文件 ,在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  logs/access.log  main;
    
    
    
    可通过 nginx.org 官网变量查看
    
    access_log  logs/access.log  main;                    --- 调用定义格式信息,生成访问日志
       $remote_addr       10.0.0.1           --- 访问客户端的源地址信息
       $remote_user          -               --- 访问客户端认证用户信息   ???
       [$time_local]                         --- 显示访问时间
       $request        GET / HTTP/1.1        --- 请求行信息
       $status              304              --- 状态码信息(304状态码利用缓存显示页面信息)
       $body_bytes_sent                      --- 服务端响应客户端的数据大小信息
       $http_referer                         --- 记录链接到网站的域名信息  ???
       $http_user_agent                      --- 用户访问网站客户端软件标识信息
                                                 用户利用客户端浏览器测试访问时,win10默认浏览器会有异常问
       $http_x_forwarded_for                 --- 反向代理相关配置
    配置如下(最终配置):
    [root@NFS logs]# vim ../conf/nginx.conf
    
    error_log       logs/error.log error;
    
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
    
        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  logs/access.log  main;
    
        include     extra/www.conf;
        include     extra/bbs.conf;
        include     extra/blog.conf;
    }
    
  • nginx.pid           #程序pid文件,记录程序的进程号

error.log和access.log配置完成后,重启nginx服务,测试。

[root@NFS logs]# /application/nginx/sbin/nginx -s reload
[root@NFS logs]# curl www.daydayup.com
10.0.0.31 www.daydayup.com
[root@NFS logs]# curl bbs.daydayup.com
10.0.0.31 bbs.daydayup.com
[root@NFS logs]# 

查看访问日志,新增了两条记录:

172.16.1.31 - - [03/Dec/2019:15:52:19 +0800] "GET / HTTP/1.1" 200 27 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "-"
172.16.1.31 - - [03/Dec/2019:15:52:24 +0800] "GET / HTTP/1.1" 200 27 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "-"
[root@NFS logs]#

模拟错误,查看错误日志:

[root@NFS logs]# /application/nginx/sbin/nginx 
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
[root@NFS logs]# 
[root@NFS logs]# 
[root@NFS logs]# 
[root@NFS logs]# cat error.log 
2019/12/01 15:52:11 [notice] 2016#0: signal process started
2019/12/01 16:10:27 [emerg] 2097#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/12/01 16:10:27 [emerg] 2097#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/12/01 16:10:27 [emerg] 2097#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/12/01 16:10:27 [emerg] 2097#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/12/01 16:10:27 [emerg] 2097#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/12/01 16:10:27 [emerg] 2097#0: still could not bind()
[root@NFS logs]# 


附:
日志要进行切割
1. 利用shell脚本实现日志切割

[root@web01 scripts]# vim cut_log.sh
#!/bin/bash
data_info=$(date +%F-%H:%M)

mv /application/nginx/logs/www_access.log /application/nginx/logs/access.log.$data_info
/application/nginx/sbin/nginx -s reload


2.在定时任务中添加脚本

[root@NFS logs]# crontab -e
* */6 * * * /bin/sh /server/scripts/cut_log.sh &>/dev/null
最后修改于 2019-12-04 09:58:04
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇