html tool

2018年5月9日星期三

nginx - upstream_cache_status 和 upstream_status



https://www.cnblogs.com/kevingrace/p/8185218.html

$upstream_cache_status
显示缓存的状态
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
nginx在web应用上的占用率越来越高,其带的模块也越来越来。nginx_cache算是一个,虽和专业的cache工具相比略逊一筹,但毕竟部署简单,不用另装软件
和资源开销,所以在web cache中也占了比重不小的一席。不过像squid和varnish等cache软件都自带的有cache查看工具,而且还可以方便的在http header上
显示出是否命中。nginx主要还是做web使用。所以想要得出命中率的大小,还需要通过日志进行统计,不过想要增加header查看倒很简单
  
1)在http header上增加命中显示
nginx提供了$upstream_cache_status这个变量来显示缓存的状态,我们可以在配置中添加一个http头来显示这一状态,达到类似squid的效果。
location  / {
        proxy_redirect          off;
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout   180;
        proxy_send_timeout      180;
        proxy_read_timeout      180;
        proxy_buffer_size       128k;
        proxy_buffers           4 128k;
        proxy_busy_buffers_size 128k;
        proxy_temp_file_write_size 128k;
        proxy_cache cache;
        proxy_cache_valid 200 304 1h;
        proxy_cache_valid 404 1m;
        proxy_cache_key $uri$is_args$args;
        add_header  Nginx-Cache "$upstream_cache_status";
        proxy_pass http://backend;
    }
  
  
而通过curl或浏览器查看到的header如下:
HTTP/1.1 200 OK
Date: Mon, 22 Apr 2013 02:10:02 GMT
Server: nginx
Content-Type: image/jpeg
Content-Length: 23560
Last-Modified: Thu, 18 Apr 2013 11:05:43 GMT
Nginx-Cache: HIT
Accept-Ranges: bytes
Vary: User-Agent
  
  
$upstream_cache_status包含以下几种状态:
·MISS 未命中,请求被传送到后端
·HIT 缓存命中
·EXPIRED 缓存已经过期请求被传送到后端
·UPDATING 正在更新缓存,将使用旧的应答
·STALE 后端将得到过期的应答
=======================================================================================================================
nginx比较强大,可以针对单个域名请求做出单个连接超时的配置. 可以根据业务的:
 
proxy_connect_timeout :后端服务器连接的超时时间_发起握手等候响应超时时间
proxy_read_timeout:连接成功后,等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)
proxy_send_timeout :后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据
$upstream_status
前端服务器的响应状态。

没有评论:

发表评论