August 2017
Intermediate to advanced
278 pages
8h 53m
English
In order to enable the cache, first we need to define where to store the cached files. This needs to be set outside the server block directive and is best stored in the main nginx.conf file. Here's the directive required:
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=cache:2m
Then, we can create a block directive to use this cache:
server {
listen 80;
server_name cached.nginxcookbook.com;
access_log /var/log/nginx/cache-access.log combined;
location / {
proxy_pass http://localhost:8080;
proxy_cache cache;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
}
}
Read now
Unlock full access