3

I'm trying to use one Nginx cache instance in two servers configs. on same server.

Is it safe and supported by Nginx?

Config is working. But I'm not sure about the consistency

In documentation nothing is written

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;
   }
}
  • just run a single dedicated caching nginx instance that would be used by the both others. – poige Jan 2 at 10:09
5
+50

assuming that both addresses: server1 and server2 are point to the same application

with configuration:

proxy_cache_key $uri;

for both addresses: server1 and server2

one cahe files will be created

at the first entry, regardless of the address, one cache file will be created, this one file will be used as cache for both addresses

my lab config:

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;
   }
}

e.g

first request to server1.com [empty cache]

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

an empty cache confirms the header X-Proxy-Cache: MISS

MISS – The response was not found in the cache and so was fetched from an origin server. The response might then have been cached.

cache:

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>

second request to 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

and abve is from nginx cache: X-Proxy-Cache: HIT

HIT – The response contains valid, fresh content direct from the cache.

now first request to 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

first request to to server2.com, and the answer is from the cache that was created when visiting the address server1.com

|improve this answer|||||
  • 1
    For me, the biggest question, how it will be processed by cache manager. On the file system, it has to work well. But proxy_cache_path also used in-memory tables. I'm afraid of conflicts on hight load. I plan to use it on a 1Gbit image cache server – demon101 Dec 29 '19 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.