在两个服务器配置之间共享 nginx 缓存

在两个服务器配置之间共享 nginx 缓存

我正在尝试在同一台服务器上的两个服务器配置中使用一个 Nginx 缓存实例。

它是否安全并且受 Nginx 支持?

配置正在运行。但我不确定一致性

在文档中没有书面

proxy_cache_path /home/mycache levels=1:2 keys_zone=mycache:90m max_size=200G inactive=15d;

server {
   server_name server1;
   ...
   location / {
          proxy_temp_path /home/temp;
          proxy_cache mycache;
          proxy_cache_key $uri; # only URI
          expires 50d;
          proxy_pass        http://blabla;
   }
}

server {
   server_name server2;
   ...
   location / {
          proxy_temp_path /home/temp;
          proxy_cache mycache;
          proxy_cache_key $uri; # only URI
          expires 50d;
          proxy_pass        http://blabla;
   }
}

答案1

假设两个地址:server1server2都指向同一个应用程序

配置如下:

proxy_cache_key $uri;

对于两个地址:server1server2

将创建一个缓存文件

在第一次输入时,无论地址如何,都会创建一个缓存文件,该文件将用作两个地址的缓存

我的实验室配置:

proxy_cache_path /tmp levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;

server {
   listen       80;
   server_name server1.com;
   add_header X-Proxy-Cache $upstream_cache_status;
   location / {
          proxy_cache my_cache;
          proxy_cache_key $uri; # only URI
          proxy_cache_valid 200 10m;
          proxy_pass http://192.168.0.101:8080;
   }
}

server {
   listen       80;
   server_name server2.com;
   add_header X-Proxy-Cache $upstream_cache_status;
   location / {
          proxy_cache my_cache;
          proxy_cache_key $uri; # only URI
          proxy_cache_valid 200 10m;
          proxy_pass http://192.168.0.101:8080;
   }
}

例如

对 server1.com 的第一个请求 [清空缓存]

curl -X GET -I server1.com
HTTP/1.1 200 OK
Server: nginx/1.17.6
Date: Sun, 29 Dec 2019 15:32:51 GMT
Content-Type: text/html
Content-Length: 45
Connection: keep-alive
Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
ETag: "2d-432a5e4a73a80"
X-Proxy-Cache: MISS
Accept-Ranges: bytes

空缓存确认标头X-Proxy-Cache: MISS

MISS – 响应未在缓存中找到,因此从源服务器获取。响应可能已被缓存。

缓存:

cat tmp/9/7d/6666cd76f96956469e7be39d750cc7d9
{^▒mF#^▒▒▒yXL"2d-432a5e4a73a80"
KEY: /
HTTP/1.1 200 OK
Date: Sun, 29 Dec 2019 15:32:51 GMT
Server: Apache/2.4.41 (Unix)
Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
ETag: "2d-432a5e4a73a80"
Accept-Ranges: bytes
Content-Length: 45
Connection: close
Content-Type: text/html

<html><body><h1>It works!</h1></body></html>

对 server1.com 的第二个请求

curl -X GET -I server1.com
HTTP/1.1 200 OK
Server: nginx/1.17.6
Date: Sun, 29 Dec 2019 15:32:56 GMT
Content-Type: text/html
Content-Length: 45
Connection: keep-alive
Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
ETag: "2d-432a5e4a73a80"
X-Proxy-Cache: HIT
Accept-Ranges: bytes

以上来自 nginx 缓存:X-Proxy-Cache: HIT

命中——响应包含直接来自缓存的有效、新鲜的内容。

现在首先向 server2.com 发出请求:)

curl -X GET -I server2.com
HTTP/1.1 200 OK
Server: nginx/1.17.6
Date: Sun, 29 Dec 2019 15:33:01 GMT
Content-Type: text/html
Content-Length: 45
Connection: keep-alive
Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
ETag: "2d-432a5e4a73a80"
X-Proxy-Cache: HIT
Accept-Ranges: bytes

第一个请求到 server2.com,答案来自访问地址 server1.com 时创建的缓存

相关内容