Firstly, I created proxy pass for the site. Test location is there for testing cache.
# the site
http {
...
proxy_cache_path /tmp/nginx-cache levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;
}
http {
...
proxy_cache_path /tmp/nginx-cache levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;
}
server {
listen 8080;
...
access_log /var/log/nginx/access.log;
location /test {
return 200 “It’s now $time_local\n”;
}
location /test {
return 200 “It’s now $time_local\n”;
}
}
# the proxy
server {
listen 80 default_server;
# the proxy
server {
listen 80 default_server;
root /var/www/;
index index.html index.htm;
server_name example.com;
charset utf-8;
location / {
include proxy_params;
include proxy_params;
proxy_pass http://localhost:8080;
proxy_cache my_cache;
proxy_cache_revalidate on;
proxy_cache_min_uses 3;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_background_update on;
proxy_cache_lock on;
proxy_cache_valid 200 15s;
}
}
proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
The cache server is already confgured to cache responses with status code 200 for 15 seconds. This typically results in cache updates every 15 or 16 seconds.
proxy_cache_valid 200 15s;
Once per second, we send an HTTP request to the address. The response does not change until the caches on the server expire. This happens every 15 or 16 seconds
$ while sleep 1; do curl http://example.com/test; done
...
It’s now 23/Oct/2019:15:11:12 +0000
It’s now 23/Oct/2019:15:11:12 +0000
It’s now 23/Oct/2019:15:11:12 +0000
It’s now 23/Oct/2019:15:11:28 +0000
It’s now 23/Oct/2019:15:11:28 +0000
It’s now 23/Oct/2019:15:11:28 +0000
^C
We can also inspect the logs on the origin server to confrm that it is receiving
a request only every 15 or 16 seconds
/var/log/nginx/access.log
...
127.0.0.1 - - [23/Oct/2019:15:11:12 +0000] "GET /test HTTP/1.0" 200 38 "-" "curl/7.58.0"
127.0.0.1 - - [23/Oct/2019:15:11:28 +0000] "GET /test HTTP/1.0" 200 38 "-" "curl/7.58.0"
a request only every 15 or 16 seconds
/var/log/nginx/access.log
...
127.0.0.1 - - [23/Oct/2019:15:11:12 +0000] "GET /test HTTP/1.0" 200 38 "-" "curl/7.58.0"
127.0.0.1 - - [23/Oct/2019:15:11:28 +0000] "GET /test HTTP/1.0" 200 38 "-" "curl/7.58.0"
Комментариев нет:
Отправить комментарий