一 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. 如何修改git commit的author信息

    本地有多个git账号时,容易发生忘记设置项目默认账号,最后以全局账号提交的情况,其实对代码本身并无影响,只是提交记录里显示的是别的名字稍显别扭. 举个例子:  查看提交日志,假设以a(a@email. ...

  2. ssh -i 密钥文件无法登陆问题

    一.用ssh 带密钥文件登录时候,发生以下报错 [root@99cloud1 ~]# ssh -i hz-keypair-demo.pem centos@172.16.17.104The authen ...

  3. 客官,来看看AspNetCore的身份验证吧

    开篇 这段时间潜水了太久,终于有时间可以更新一篇文章了. 通过本篇文章您将Get: Http的一些身份验证概念 在AspNetCore中实现身份验证方案 JWT等概念的基础知识 使用Bearer To ...

  4. onunload对应的js代码为什么不能执行?和onbeforeunload的区别?

    为什么onunload对应的js代码不能执行? 为什么onbeforeunload才可以在离开页面时执行相应的js代码? 1.onunload和onbeforeunload都是在离开页面或者刷新页面的 ...

  5. 【数位dp】CF 55D Beautiful numbers

    题目 Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer n ...

  6. pycharm远程连接vagrant虚拟机中mariadb数据库

    1.虚拟机数据库设置--重启数据库 (1)vi /etc/my.cnf bind = 0.0.0.0 (2).远程不能用root用户连,得新建用户 select host,user from mysq ...

  7. .netcore 网站启动后 502.5

    网站启动后,报错 HTTP Error 502.5 - ANCM Out-Of-Process Startup Failure 请检查安装的.netcore runtime版本和hosting版本是否 ...

  8. .gitkeep文件

    git 默认不会对空文件夹进行追踪: 但某些项目某些文件夹对整体框架是必不可少的,就算是空也得有: 怎么办呢?在这个文件夹下添加一个[.gitkeep]文件,这样就可以同步该文件夹了. (完)

  9. 小熊派4G开发板初体验

    开发板硬件资源介绍 前阵子小熊派发布了一款超高性价比的4G开发板(19.8元包邮),但是板子仅限量1000套.小熊派官方给我送了一块,我们一起来学习学习: 板子做得小巧精致,控制核心用的是移远的EC1 ...

  10. SpringCloud Alibaba (四):Dubbo RPC框架

    Dubbo简介 Apache Dubbo |ˈdʌbəʊ| 是一款高性能.轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现.致 ...