如果上游启动,则Nginx绕过缓存;如果关闭,则使用缓存 [英] Nginx bypass cache if upstream is up and use cache if down

查看:133
本文介绍了如果上游启动,则Nginx绕过缓存;如果关闭,则使用缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在上游启动时(最大年龄1)绕过高速缓存,而在下游运行时(proxy_cache_use_stale)使用高速缓存,我创建了以下配置:

To bypass cache if upstream is up (max-age 1) and use cache if down (proxy_cache_use_stale) I created following config:

proxy_cache_path   /app/cache/ui levels=1:2 keys_zone=ui:10m max_size=1g inactive=30d;
server {
    ...
    location /app/ui/config.json {
        proxy_cache ui;
        proxy_cache_valid 1d;
        proxy_ignore_headers Expires;           
        proxy_hide_header Expires;
        proxy_hide_header Cache-Control;
        add_header Cache-Control "max-age=1, public";
        proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
        add_header X-Cache-Status $upstream_cache_status;
        add_header X-Cache-Date $upstream_http_date;
        proxy_pass http://app/config.json;
    }
}

但是,当上游关闭并且客户端仅获得504网关超时时,不使用高速缓存.我已经阅读了以下文章:

But cache is not used when upstream is down and client only gets 504 Gateway Timeout. I've already read following articles:

https://nginx.org/ru/docs/http/ngx_http_proxy_module .html#proxy_cache_use_stale

如何配置NginX仅在后端关闭(5xx响应代码)时才提供缓存的内容?

https://serverfault.com/questions/752838/nginx -use-proxy-cache-if​​-backend-is-down

而且它不符合我的预期.感谢您的帮助.

And It does not work as I expect. Any help is appreciated.

推荐答案

让我们讨论一个由两个服务器组成的非常简单的设置.一个正在运行的apache2提供一个简单的html页面.另一个正在运行的nginx可以将代理反向转换为第一个.

Let's discuss a really simple setup with two servers. One running apache2 serving a simple html page. The other running nginx that reverse proxies to the first one.

http {
[...]

  proxy_cache_path /var/lib/nginx/tmp/proxy levels=2:2 keys_zone=one:10m inactive=48h max_size=16g use_temp_path=off;

  upstream backend {
    server foo.com;
  }

  server {
  [...]
    location / {
      proxy_cache           one;
      proxy_cache_valid     200 1s;
      proxy_cache_lock      on;

      proxy_connect_timeout 1s;
      proxy_cache_use_stale error timeout updating http_502 http_503 http_504;

      proxy_pass http://backend/
    }
  }
}

此设置对我有用.最重要的区别是proxy_cache_valid 200 1s;,这意味着将仅缓存带有http代码200的响应,并且仅在1秒钟内有效.这确实意味着对特定资源的第一个请求将从后端获取并放入缓存中.对同一资源的任何进一步请求将在高速缓存中持续一整秒钟.之后,第一个请求将再次转到后端,依此类推,等等.

This setup works for me. The most important difference is the proxy_cache_valid 200 1s; It means that only responses with http code 200 will be cached, and will only be valid for 1 second. Which does imply that the first request to a certain resource will be get from the backend and put in the cache. Any further request to that same resource will be served from the cache for a full second. After that the first request will go to the backend again, etc, etc.

proxy_cache_use_stale是您方案中的重要部分.它基本上说在什么情况下,尽管proxy_cache_valid指定的时间已经过去,它仍应提供缓存的版本.因此,在这里,您必须决定在哪种情况下仍要从缓存中提供服务.

The proxy_cache_use_stale is the important part in your scenario. It basically says in which cases it should still serve the cached version although the time specified by proxy_cache_valid has already passed. So here you have to decided in which cases you still want to serve from cache.

指令的参数与proxy_next_upstream相同.

您将需要这些:

error:如果服务器仍在运行,但没有响应,或者响应不正确.

error: In case the server is still up, but not responding, or is not responding correctly.

timeout:连接到服务器,请求或响应超时.这也是为什么要将proxy_connect_timeout设置为较低的原因.默认值为60秒,这对于最终用户来说很长.

timeout: connecting to the server, requesting or response times out. This is also why you want to set proxy_connect_timeout to something low. The default is 60s and is way to long for an end-user.

updating:已经在请求新内容. (不是真正需要的,但从性能的角度来看更好.)

updating: there is already a request for new content on it's way. (not really needed but better from a performance point of view.)

http_xxx参数对您没有多大帮助,当该后端服务器关闭时,无论如何您将永远不会获得任何响应.

The http_xxx parameters are not going to do much for you, when that backend server is down you will never get a response with any of these codes anyway.

但是在我的现实生活中,后端服务器也是nginx,它代理本地主机上的不同端口.因此,当nginx运行正常时,但所有这些后端均处于关闭状态时,参数http_502http_503http_504不再有用,因为这些正是我将收到的http代码.

In my real life case however the backend server is also nginx which proxies to different ports on the localhost. So when nginx is running fine, but any of those backends is down the parameters http_502, http_503 and http_504 are quit useful, as these are exactly the http codes I will receive.

我不想从缓存中提供http_403http_404http_500.当文件被禁止(403)或在后端不再被禁止(404)或脚本出错(500)时,这是有原因的.但这就是我的看法.

The http_403, http_404 and http_500 I would not want to serve from cache. When a file is forbidden (403) or no longer on the backend (404) or when a script goes wrong (500) there is a reason for that. But that is my take on it.

这篇关于如果上游启动,则Nginx绕过缓存;如果关闭,则使用缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆