proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=app_cache:10m;
proxy_cache_key "$scheme$request_method$host$request_uri";
server {
listen [::]:80 default_server;
listen 80 default_server;
server_name _;
location / {
add_header X-Cache-Status $upstream_cache_status;
proxy_pass http://localhost:9001;
proxy_cache app_cache;
proxy_cache_bypass $http_no_cache;
}
}
# server 01
server {
listen [::]:9001;
listen 9001;
server_name _;
# nginx will use Cache-Control and expires to manage cache
add_header Cache-Control "public";
expires 30d;
add_header Content-Type "text/plain";
return 200 "hello world: server 01";
}
docker run --detach --name nginx-cached-proxy \
--volume "${PWD}/cached-proxy.conf:/etc/nginx/conf.d/default.conf" \
--publish "8080:80" \
--workdir "/usr/share/nginx/html/" \
nginx
curl -SsD- http://localhost:8080/
curl -SsD- http://localhost:8080/
curl -SsD- -H "No-Cache: 1" http://localhost:8080/
docker stop nginx-cached-proxy
docker rm nginx-cached-proxy