一 Nginx请求简介

1.1 请求头部

对于HTTP而言,客户端负责发起request请求,服务端负责response响应。
request:包括请求行、请求头部、请求数据;
response:包括状态行、消息报头、响应正文。
  1 [root@nginx ~]# curl -v www.odocker.com
2 * About to connect() to www.odocker.com port 80 (#0) #关于本次连接信息
3 * Trying 113.31.119.149...
4 * Connected to www.odocker.com (113.31.119.149) port 80 (#0)
5 > GET / HTTP/1.1 #HTTP版本
6 > User-Agent: curl/7.29.0 #客户端信息
7 > Host: www.odocker.com #请求的服务端主机
8 > Accept: */* #如上为请求
9 >
10 < HTTP/1.1 200 OK #返回http版本
11 < Server: nginx/1.16.1 #服务端Web类型
12 < Date: Fri, 06 Mar 2020 13:09:40 GMT #日期时间
13 < Content-Type: text/html #返回的类型
14 < Content-Length: 13 #长度
15 < Last-Modified: Thu, 05 Mar 2020 11:12:26 GMT #日期时间
16 < Connection: keep-alive #长连接
17 < ETag: "5e60de9a-d" #Etag
18 < Accept-Ranges: bytes #大小单位
19 <
20 <h1>www</h1> #具体内容
21 * Connection #0 to host www.odocker.com left intact

二 日志配置

2.1 日志相关配置

nginx日志相关涉及的配置有:
access_log:访问日志;
log_format:日志格式;
rewrite_log:重定向日志;
error_log:错误日志;
open_log_file_cache、log_not_found、log_subrequest。
nginx具备非常灵活的日志记录模式,每个级别的配置可以有各自独立的访问日志。
日志格式通过log_format命令来定义。ngx_http_log_module:用于定义请求日志格式。

2.2 access_log配置

语法:
  • access_log path [format [buffer=size [flush=time]]];
  • access_log path format gzip[=level] [buffer=size] [flush=time];
  • access_log syslog:server=address[,parameter=value] [format];
  • access_log off; #不记录日志
默认值: access_log logs/access.log combined;
使用默认combined格式记录日志:access_log logs/access.log 或 access_log logs/access.log combined;
配置段: http, server, location, if in location, limit_except
参数解释:
  • gzip:压缩等级。
  • buffer:设置内存缓存区大小。
  • flush:保存在缓存区中的最长时间。

2.3 log_format配置

语法:
  • log_format name string ……;
默认值: log_format combined "……";
配置段: http
释义:name表示格式名称,string表示等义的格式。log_format有一个默认的无需设置的combined日志格式,相当于apache的combined日志格式。
示例1:
  1 ……
2 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
3 '$status $body_bytes_sent "$http_referer" '
4 '"$http_user_agent"';
5 ……
示例2:
  1 ……
2 log_format proxy '$remote_addr - $remote_user [$time_local] "$request" '
3 '$status $body_bytes_sent "$http_referer" '
4 '"$http_user_agent" "$http_user_agent" ';
5 ……
配置相关变量释义:
$remote_addr:表示客户端地址;
$remote_user:表示http客户端请求Nginx认证的用户名;
$time_local:Nginx通用日志格式下的本地时间;
$request:request请求行,请求的URL、GET等方法、HTTP协议版本;
$request_length:请求的长度;
$request_time:请求处理时间,单位为秒,精度为毫秒;
$status:response返回状态码;
$body_bytes_sent:发送给客户端的字节数,不包括响应头的大小,即服务端响应给客户端body信息大小;
$http_referer:http上一级页面,即从哪个页面链接访问过来的,用于防盗链、用户行为分析;
$http_user_agent:http头部信息,记录客户端浏览器相关信息;
$connection:连接的序列号;
$connection_requesta:当前通常一个连接获得的请求数量;
$msec:日志写入时间,单位为秒,精度为毫秒;
$pipe:如果请求是通过HTTP流水线(pipelined)发送,pipe值为‘p’,否则为“.”;
$http_x_forwarded_for:http请求携带的http信息。
提示:如果nginx位于负载均衡器,squid,nginx反向代理之后,web服务器无法直接获取到客户端真实的IP地址了。 $remote_addr获取反向代理的IP地址。反向代理服务器在转发请求的http头信息中,可以增加X-Forwarded-For信息,用来记录客户端IP地址和客户端请求的服务器地址。

2.4 open_log_file_cache配置

语法:
  • open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
  • open_log_file_cache off;
默认值:open_log_file_cache off; #关闭open_log_file_cache
配置段:http,server,location
作用:对于每一条日志记录,都将是先打开文件,再写入日志,然后关闭。可以使用open_log_file_cache来设置日志文件缓存(默认是off)。
参数释义:
max:设置缓存中的最大文件描述符数量,如果缓存被占满,采用LRU算法将描述符关闭。
inactive:设置存活时间,默认是10s。
min_uses:设置在inactive时间段内,日志文件最少使用多少次后,该日志文件描述符记入缓存中,默认是1次。
valid:设置检查频率,默认60s。
off:禁用缓存。
示例1:
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;

2.5 log_not_found配置

语法:log_not_found on | off;
默认值:log_not_found on;
配置段:http,server,location
作用:是否在error_log中记录不存在的错误,默认是,即记录。

2.6 log_subrequest配置

语法:log_subrequest on | off;
默认值:log_subrequest off;
配置段:http,server,location
作用:是否在access_log中记录子请求的访问日志,默认否,即不记录。

2.7 rewrite_log配置

语法: rewrite_log on | off;
默认值:rewrite_log off;
配置段:http,server,location,if
作用:由ngx_http_rewrite_module模块提供的。用来记录重写日志的,对于调试重写规则建议开启。启用时将在error log中记录notice级别的重写日志。

2.8 error_log配置

语法:error_log file | stderr | syslog:server=address[,parameter=value] [debug | info | notice | warn | error | crit | alert | emerg];
默认值:error_log logs/error.log error;
配置段:main,http,server,location
作用:配置错误日志。

三 状态监控

3.1 配置监控

Nginx状态监控使用--with-http_stub_status_modele编译模块,语法:
语法:stub_status on | off;
默认值:stub_status off;
配置段:server,location
示例01:
  1 [root@nginx01 ~]# vi /etc/nginx/conf.d/status.conf
2 server {
3 server_name status.linuxds.com;
4
5 error_page 404 403 500 502 503 504 /error.html;
6 location = /error.html {
7 root /usr/share/nginx/html;
8 }
9
10 location / {
11 root /usr/share/nginx/blog;
12 index index.html;
13 }
14 location /ok {
15 alias /usr/share/nginx/yes;
16 index index.html;
17 }
18 location /mystatus {
19 stub_status on;
20 access_log off;
21 }
22 }
  1 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf
2 [root@nginx01 ~]# nginx -s reload
浏览器访问:http://status.linuxds.com/mystatus
释义:
  • Active connections:当前活跃的连接数。
  • server:表示Nginx启动到现在共处理了90个连接。
  • accepts:表示Nginx启动到现在共成功创建90次握手。
  • handled requests:表示总共处理了19次请求。
  • Reading:Nginx读取到客户端的 Header 信息数。
  • Writing:Nginx返回给客户端的 Header 信息数。
  • Waiting:Nginx开启keep-alive长连接情况下, 既没有读也没有写, 建立连接情况。
请求丢失数=(握手数-连接数)可以看出,本次状态显示没有丢失请求。

004.Nginx日志配置及状态监控的更多相关文章

  1. nginx日志配置

    nginx日志配置 http://www.ttlsa.com/linux/the-nginx-log-configuration/ 日志对于统计排错来说非常有利的.本文总结了nginx日志相关的配置如 ...

  2. (转)nginx日志配置指令详解

    这篇文章主要介绍了nginx日志配置指令详解,nginx有一个非常灵活的日志记录模式,每个级别的配置可以有各自独立的访问日志,需要的朋友可以参考下日志对于统计排错来说非常有利的.本文总结了nginx日 ...

  3. nginx日志配置指令详解

    这篇文章主要介绍了nginx日志配置指令详解,nginx有一个非常灵活的日志记录模式,每个级别的配置可以有各自独立的访问日志,需要的朋友可以参考下日志对于统计排错来说非常有利的.本文总结了nginx日 ...

  4. Nginx https加密以及nginx日志配置与管理

    Nginx https加密以及nginx日志配置与管理 使用Nginx的优点Nginx作为WEB服务器,Nginx处理静态文件.索引文件.自动索引的效率非常高.Nginx作为代理服务器,Nginx可以 ...

  5. nginx日志配置[转]

     * * * sh /home/zyf/sh/cut_nginx_log.sh 这样就每天的0点1分把nginx日志重命名为日期格式,并重新生成今天的新日志文件。 日志对于统计排错来说非常有利的。本文 ...

  6. Nginx日志配置及日志切割

    日志配置 日志对于统计排错来说非常有利的.本文总结了nginx日志相关的配置如access_log.log_format.open_log_file_cache.log_not_found.log_s ...

  7. 死磕nginx系列-nginx日志配置

    nginx access日志配置 access_log日志配置 access_log用来定义日志级别,日志位置.语法如下: 日志级别: debug > info > notice > ...

  8. Nginx日志配置与切割

    访问日志主要记录客户端访问Nginx的每一个请求,格式可以自定义.通过访问日志,你可以得到用户地域来源.跳转来源.使用终端.某个URL访问量等相关信息. Nginx中访问日志相关指令主要有两条,一条是 ...

  9. nginx别名配置,状态配置,include优化

    一.nginx帮助参数 下面是关于/application/nginx/sbin/nginx 的参数帮助 [root@A conf]# /application/nginx/sbin/nginx -h ...

随机推荐

  1. weblogic高级进阶之查看日志

    域的日志位于 D:\Oracle\Middleware\user_projects\domains\base_domain\servers\AdminServer\logs 名字是base_domai ...

  2. django drf插件(一)

    复习 """ 1.vue如果控制html 在html中设置挂载点.导入vue.js环境.创建Vue对象与挂载点绑定 2.vue是渐进式js框架 3.vue指令 {{ }} ...

  3. Python自学——pygame安装

    本片文章介绍pygame的安装方法.一则跟广大初学者分享经历,二则做个自我总结. pygame是python的库文件,跟一般的应用软件安装方法不太一样.我电脑上的python版本是python3.7, ...

  4. [javascript]js原型链以及原型链继承

    基础的三个要素: 函数 ,函数实例,实例原型. 实例原型相当于 父类, 函数相当于构造函数 举例: class Fn extends Fn.prototype{ } 实例: let f = new F ...

  5. mfc 中unicode 字符和字符串的使用

    在MFC或SDK程序中,不需要进行任何关于unicode的设置,记住下面两个宏,保你程序一路畅通: 用TCHAR/TCHAR*代替char/char* 及wchar/wchar*用TEXT(" ...

  6. 博弈论Nim取子问题,困扰千年的问题一行代码解决

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是算法与数据结构专题26篇文章,我们来看看一个新的博弈论模型--Nim取子问题. 这个博弈问题非常古老,延续长度千年之久,一直到20世纪 ...

  7. 【实践】如何利用tensorflow的object_detection api开源框架训练基于自己数据集的模型(Windows10系统)

    如何利用tensorflow的object_detection api开源框架训练基于自己数据集的模型(Windows10系统) 一.环境配置 1. Python3.7.x(注:我用的是3.7.3.安 ...

  8. int c, int ndigit[10]; ++ndigit[c-'0'];

    for example c-'0' is an integer expression with a value between 0and 9 corresponding to the characte ...

  9. vue项目chunk包loading失败解决办法

    错误截图: 解决方法: // loading chunk 出错处理 router.onError((error) => { const pattern = /Loading chunk (\d) ...

  10. iOS应用千万级架构开篇

    一款好的APP架构,是需要适应复杂的业务场景的.当然它也是可以监控的,比如性能.卡顿等.你写的每一行代码,测试都可以查看到,并测试覆盖到. 一直很想分享一下,一个大型的APP都做了些什么事情,这些事情 ...