访问日志

[Access.log]
log_format  main  '$remote_addr $remote_user [$time_local] "$request" $http_host '
                  '$status $upstream_status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" $ssl_protocol $ssl_cipher $upstream_addr '
                  '$request_time $upstream_response_time';

变量名

变量名称 变量描述 举例说明
$remote_addr 客户端地址 113.140.15.90
$remote_user 客户端用户名称 -
$time_local 访问时间和时区 18/Jul/2012:17:00:01 +0800
$request 请求的URI和HTTP协议 “GET /pa/img/home/logo-alipay-t.png HTTP/1.1”
$http_host 请求地址,即浏览器中你输入的地址(IP或域名) img.alipay.com10.253.70.103
$status HTTP请求状态 200
$upstream_status upstream状态 200
$body_bytes_sent 发送给客户端文件内容大小 547
$http_referer 跳转来源 https://cashier.alipay.com…/”
$http_user_agent 用户终端代理 “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; | SV1; GTB7.0; .NET4.0C;
$ssl_protocol SSL协议版本 TLSv1
$ssl_cipher 交换数据中的算法 RC4-SHA
$upstream_addr 后台upstream的地址,即真正提供服务的主机地址 10.228.35.247:80
$request_time 整个请求的总时间 0.205
$upstream_response_time 请求过程中,upstream响应时间 0.002

错误说明

错误信息 错误说明
“upstream prematurely(过早的) closed connection” 请求uri的时候出现的异常,是由于upstream还未返回应- 答给用户时用户断掉连接造成的,对系统没有影响,可以忽略
“recv() failed (104: Connection reset by peer)” (1)服务器的并发连接数超过了其承载量,服务器会将其中- 一些连接Down掉; (2)客户关掉了浏览器,而服务器还在给客户端发送数据; (3)浏览器端按了Stop
“(111: Connection refused) while connecting to upstream” 用户在连接时,若遇到后端upstream挂掉或者不通,会收到该错误
“(111: Connection refused) while reading response header from upstream” 用户在连接成功后读取数据时,若遇到后端upstream挂掉或者不通,会收到该错误
“(111: Connection refused) while sending request to upstream” Nginx和upstream连接成功后发送数据时,若遇到后端upstream挂掉或者不通,会收到该错误
“(110: Connection timed out) while connecting to upstream” nginx连接后面的upstream时超时
“(110: Connection timed out) while reading upstream” nginx读取来自upstream的响应时超时 
“(110: Connection timed out) while reading response header from upstream” nginx读取来自- upstream的响应头时超时
“(110: Connection timed out) while reading upstream” nginx读取来自upstream的响应时超时
“(104: Connection reset by peer) while connecting to upstream” upstream发送了RST,将连接重置
“upstream sent invalid header while reading response header from upstream” upstream发送的响应头- 无效
“upstream sent no valid HTTP/1.0 header while reading response header from upstream” upstream发送的响应头无效
“client intended to send too large body” 用于设置允许接受的客户端请求内容的最大值,默认值是1M,client发送的body超过了设置值
“reopening logs” 用户发送kill  -USR1命令
“gracefully shutting down”, 用户发送kill  -WINCH命令
“no servers are inside upstream” upstream下未配置server
“no live upstreams while connecting to upstream” upstream下的server全都挂了
“SSL_do_handshake() failed” SSL握手失败
“SSL_write() failed (SSL:) while sending to client”  
“(13: Permission denied) while reading upstream”  
“(98: Address already in use) while connecting to upstream”  
“(99: Cannot assign requested address) while connecting to upstream”  
“ngx_slab_alloc() failed: no memory in SSL session shared cache” ssl_session_cache大小不够等原- 因造成
“could not add new SSL session to the session cache while SSL handshaking” ssl_session_cache大小不够等原因造成
“send() failed (111: Connection refused)”  

常用指令

X-Accel-Redirect:

后端服务器返回,携带url,nginx会做内部重定向,只能访问internal的location。 可以通过proxy_ignore_headers配置禁掉。

proxy_intercept_errors:

指定对>=300的错误码,是直接返回给客户端,还是重定向到error_page中处理,默认关闭,即直接返回。

sendfile:

指定是否使用sendfile系统调用来传输文件。 sendfile系统调用在两个文件描述符之间直接传递数据(完全在内核中操作),从而避免了数据在内核缓冲区和用户缓冲区之间的拷贝,操作效率很高,被称之为零拷贝。

tcp_nopush:

当使用sendfile函数时,tcp_nopush才起作用,它和指令tcp_nodelay是互斥的。 tcp_nopush = on 会设置调用tcp_cork方法,这个也是默认的,结果就是数据包不会马上传送出去,等到数据包最大时,一次性的传输出去,这样有助于解决网络堵塞。

tcp_nodelay:

设置套接字的TCP_NODELAY = on 选项禁用Nagle 算法,立即响应请求。

keepalive:

  • 1,与client的长连接:默认开启。
http {
    keepalive_timeout  120s 120s;
    keepalive_requests 10000;
}
  • 2,与server的长连接:默认为http1.0短连接
    upstream  BACKEND {
        ****
        keepalive 300;       
    }
    location /  {
        proxy_http_version 1.1; //http1.1支持长连接
        proxy_set_header Connection "";//重置client过来的header
    }

upstream

max_fails=number

设定Nginx与服务器通信的尝试失败的次数。在fail_timeout参数定义的时间段内,如果失败的次数达到此值,Nginx就认为服务器不可用。在下一个fail_timeout时间段,服务器不会再被尝试。 失败的尝试次数默认是1。设为0就会停止统计尝试次数,认为服务器是一直可用的。 你可以通过指令proxy_next_upstream、fastcgi_next_upstream和 memcached_next_upstream来配置什么是失败的尝试。 默认配置时,http_404状态不被认为是失败的尝试。

fail_timeout=time

设定服务器被认为不可用的时间段以及统计失败尝试次数的时间段。在这段时间中,服务器失败次数达到指定的尝试次数,服务器就被认为不可用。默认情况下,该超时时间是10秒。

location

修饰符

  • = 表示精确匹配。只有请求的url路径与后面的字符串完全相等时,才会命中。
  • ~ 表示该规则是使用正则定义的,区分大小写。
  • ~* 表示该规则是使用正则定义的,不区分大小写。
  • ^~ 表示如果该符号后面的字符是最佳匹配,采用该规则,不再进行后续的查找。

匹配过程

对请求的url序列化。例如,对%xx等字符进行解码,去除url中多个相连的/,解析url中的.,..等。这一步是匹配的前置工作。 location有两种表示形式,一种是使用前缀字符,一种是使用正则。如果是正则的话,前面有~或~*修饰符。 具体的匹配过程如下:

    1. 首先先检查使用前缀字符定义的location,选择最长匹配的项并记录下来。
    1. 如果找到了精确匹配的location,也就是使用了=修饰符的location,结束查找,使用它的配置。
    1. 然后按顺序查找使用正则定义的location,如果匹配则停止查找,使用它定义的配置。
    1. 如果没有匹配的正则location,则使用前面记录的最长匹配前缀字符location。

基于以上的匹配过程,我们可以得到以下两点启示: 1, 使用正则定义的location在配置文件中出现的顺序很重要。因为找到第一个匹配的正则后,查找就停止了,后面定义的正则就是再匹配也没有机会了。 2, 使用精确匹配可以提高查找的速度。例如经常请求/的话,可以使用=来定义location。

优先级

(location =) > (location 完整路径) > (location ^~ 路径) > (location ,* 正则顺序) > (location 部分起始路径) > (/)

rewrite

  • last – 基本上都用这个Flag。
  • break – 中止Rewirte,不在继续匹配
  • redirect – 返回临时重定向的HTTP状态302
  • permanent – 返回永久重定向的HTTP状态301

last和break最大的不同在于:break是终止当前location的rewrite检测,而且不再进行location匹配 - last是终止当前location的rewrite检测,但会继续重试location匹配并处理区块中的rewrite规则。

注意事项

  • 1,nginx默认不转发带下划线的属性,定义属性时需要注意!
  • 2,部分指令如proxy_set_header,一旦location中被调用,则外部该指令的所有调用都失效,不是简单的覆盖。