Nginx学习笔记——日志(log_format)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012525096/article/details/82937735

日志配置

/etc/nginx/nginx.conf中有log_format的配置。

    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  /var/log/nginx/access.log  main;

含义是配置了一个名为main的日志格式化的规则,应用在了access_log的日志上。

Nginx变量

(1)HTTP请求变量:arg_PARAMETER、http_HEADER(客户端发给服务器的)、sent_http_HEADER(服务器发给客户端的)
(2)内置变量:Nginx内置的
(3)自定义变量:自己定义的

测试

如增加一个http_user_agent在最前面,改成如下的log_format

    log_format  main  '$http_user_agent $remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

终端中执行nginx -c /etc/nginx/nginx.conf -t,进行配置文件的检查。
执行nginx -c /etc/nginx/nginx.conf -s reload,进行重加载配置文件,访问本地服务器,使用vim /var/log/nginx/access.log查看日志文件。

猜你喜欢

转载自blog.csdn.net/u012525096/article/details/82937735