Nginx生产环境日志配置示例,保姆级!

2023-05-0114:38:26服务器及运维Comments1,430 views字数 2906阅读模式

在一个企业级项目中,我们不但要完成业务模块的开发,同时还会做一些系统方面的操作,记录系统运行状态或者客户端的请求信息。来帮助系统进行安全防护、系统升级等相关的场景。今天就来聊聊Nginx的日志文件配置。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

Nginx是一款高性能、支持反向代理、文件流传输的web服务软件,被广泛使用。在一些小型项目中,对于Nginx的日志,都是采用文件存储,要查看信息,直接登录服务器进行查看。但是随着系统的升级、规模的变大,例如分布式应用系统,想要查看日志信息是非常难得,因为你不知道某个请求具体落在那个节点上,针对这种情况,当下最流行的是使用ELK这么一套系统来实现。大致流程如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

Nginx生产环境日志配置示例,保姆级!文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

Nginx服务器日志相关指令主要有两条:一条是log_format,用来设置日志格式;另外一条是access_log,用来指定日志文件的存放路径、格式和缓存大小,可以参加ngx_http_log_module。一般在Nginx的配置文件的日志配置(/usr/local/nginx/conf/nginx.conf)。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

log_format指令用来设置日志的记录格式,它的语法如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

log_format name format {format ...}

其中name表示定义的格式名称,format表示定义的格式样式。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

log_format有一个默认的、无须设置的combined日志格式设置,相当于Apache的combined日志格式,其具体参数如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

log_format combined '$remote_addr-$remote_user [$time_local]'
‘"$request"$status $body_bytes_sent’
 ‘"$http_referer" "$http_user_agent"’

也可以自定义一份日志的记录格式,不过要注意,log_format指令设置的名称在配置文件中是不能重复的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

假设将Nginx服务器作为Web服务器,位于负载均衡设备、Squid、Nginx反向代理之后,不能获取到客户端的真实IP地址了。原因是经过反向代理后,由于在客户端和Web服务器之间增加了中间层,因此Web服务器无法直接拿到客户端的IP。通过$remote_addr变量拿到的将是反向代理服务器的IP地址。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

但是,反向代理服务器在转发请求的HTTP头信息中,可以增加X-Forwarded-For信息,用以记录原有的客户端IP地址和原来客户端请求的服务器地址。这时候,要用log_format指令设置日志格式,让日志记录X-Forearded-For信息中的IP地址,即客户的真实IP。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

例如,创建一个名为mylogformat的日志格式,再$http_x_forwarded_forlog_for变量记录用户的X_Forwarded-For IP 地址:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

log_format mylogformat '$http_x_forwarded_for_$remote_user [$time_local]'
‘"$request"$status $body_bytes_sent’
 ‘"$http_referer" "$http_user_agent"’

在日志格式样式中,变量和http_x_forwarded_for用于记录IP地址;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

1、$remote_user用于记录远程客户端用户名称;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

2、$time_local用于记录访问时间与时区;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

3、$request用于记录请求URL与HTTP协议;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

4、$status用于记录请求状态,例如成功时状态为200,页面找不到时状态为404;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

5、$body_bytes_sent用于记录发送客户端的文件主体内容大小;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

6、$http_referer用于记录是从哪个页面链接访问过来的;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

7、$http_user_agent用于记录客户浏览器的相关信息。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

一般来说:nginx的log_format有很多可选的参数用于指示服务器的活动状态,默认的是:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

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

想要记录更详细的信息需要自定义设置log_format,具体可设置的参数格式及说明如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

参数说明示例
$remote_addr客户端地址211.28.65.253
$remote_user客户端用户名称--
$time_local访问时间和时区18/Jul/2012:17:00:01 +0800
$request请求的URI和HTTP协议"GET /article-10000.html HTTP/1.1"
$http_host请求地址,即浏览器中你输入的地址(IP或域名)www.wang.com 192.168.100.100
$statusHTTP请求状态200
$upstream_statusupstream状态200
$body_bytes_sent发送给客户端文件内容大小1547
$http_refererurl跳转来源https://www.baidu.com/
$http_user_agent用户终端浏览器等信息"Mozilla/4.0
$ssl_protocolSSL协议版本TLSv1
$ssl_cipher交换数据中的算法RC4-SHA
$upstream_addr后台upstream的地址,即真正提供服务的主机地址10.10.10.100:80
$request_time整个请求的总时间0.205
$upstream_response_time请求过程中,upstream响应时间0.002

如下是在nginx的LB代理层使用过的一个配置(nginx.conf中配置):文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

log_format  main  '$remote_addr $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '$http_user_agent $http_x_forwarded_for $request_time $upstream_response_time $upstream_addr $upstream_status';

然后在nginx.conf文件或vhosts/*.conf文件中的access_log日志中指定级别为main。如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

access_log  logs/wiki_access.log main;
error_log   logs/wiki_error.log;

重启nginx服务后生效。日志截取如下(可以从日志中看到代理到后端哪台机器上的哪个端口上,负载访问的状态值等都能看到)。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/38109.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunwei/38109.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定