nginx优化参考

参考链接:http://blog.sina.com.cn/s/blog_4f9fc6e10102uxib.html

计算访问路径频度

awk '{print $7}' access.log|sort | uniq -c |sort -n -k 1 -r|more |grep /路径 

 ps

print & $0
print 是awk打印指定内容的主要命令
awk '{print}'  /etc/passwd   ==   awk '{print $0}'  /etc/passwd  
awk '{print " "}' /etc/passwd                                           //不输出passwd的内容,而是输出相同个数的空行,进一步解释了awk是一行一行处理文本
awk '{print "a"}'   /etc/passwd                                        //输出相同个数的a行,一行只有一个a字母
awk -F":" '{print $1}'  /etc/passwd 
awk -F: '{print $1; print $2}'   /etc/passwd                   //将每一行的前二个字段,分行输出,进一步理解一行一行处理文本
awk  -F: '{print $1,$3,$6}' OFS="	" /etc/passwd        //输出字段1,3,6,以制表符作为分隔符

【1】接口请求时间

1、request_time
官网描述:request processing time in seconds with a milliseconds resolution; time elapsed between the first bytes were read from the client and the log write after the last bytes were sent to the client 。
指的就是从接受用户请求的第一个字节到发送完响应数据的时间,即包括接收请求数据时间、程序响应时间、输出
响应数据时间。
 
2、upstream_response_time
官网描述:keeps times of responses obtained from upstream servers; times are kept in seconds with a milliseconds resolution. Several response times are separated by commas and colons like addresses in the $upstream_addr variable
 
是指从Nginx向后端(php-cgi)建立连接开始到接受完数据然后关闭连接为止的时间。
 
从上面的描述可以看出,$request_time肯定比$upstream_response_time值大,特别是使用POST方式传递参数时,因为Nginx会把request body缓存住,接受完毕后才会把数据一起发给后端。所以如果用户网络较差,或者传递数据较大时,$request_time会比$upstream_response_time大很多。
 
所以如果使用nginx的accesslog查看php程序中哪些接口比较慢的话,记得在log_format中加入$upstream_response_time。

【2】其他参数

参数具体含义如下:

$remote_addr :与$http_x_forwarded_for用以记录客户端的ip地址;

$remote_user :记录客户端用户的名称;

$time_local  :访问时间及时区;

$request     :请求的URL与HTTP协议;

$status     :记录请求状态

$body_bytes_sent:记录发送给客户端文件主体内容大小;

$http_referer:用来记录从那个页面链接访问过来的;

$http_user_agent:记录客户端浏览器的相关信息

access_log /usr/local/nginx/var/log/access.log  main ;   这句话是日志文件存放的位置



这里是默认的配置,但是有的时候我们需要自己配置我们的nginx日志格式,下面给出一些常用的参数配置。

$bytes_sent :客户端发送的字节数

$request_length:客户端请求的长度

$http_host   :客户端请求的地址请求地址,即浏览器中你输入的地址(IP或域名)

$upstream_status:upstream状态

$upstream_addr   :后台upstream的地址,即真正提供服务的主机地址 

$request_time        : 整个请求的总时间 

$upstream_response_time:请求过程中,upstream响应时间 

$request_body   :POST数据
原文地址:https://www.cnblogs.com/daysn/p/10443939.html