Nginx cache without upstream / backend

cachenginxreverse-proxy

I know, nginx can act as a reverse proxy to cache files if provided with a backend.

The thing is, I already use nginx as a web-server, serving files (statics and php), and I want to know if it's possible for nginx to act as a proxy_cache with his own files ?
I know it'll not work if I put the nginx itself as an upstream.

The classic thing is :
nginx => apache backend => cached_result

What I want :
nginx => same nginx instance => cached_result

Is it possible ? And if so, how ?

Best Answer

Assuming that there is some need to cache local files I just went and added the following configuration to an nginx box and it seems to work fine straight out of the box:

proxy_cache_path /data/nginx/cache keys_zone=one:128m;
server {
  listen 127.0.0.1:80;
  server_name _;
  root /usr/share/nginx/local;
  location / {
    autoindex on;
    allow all;
    satisfy any;
    try_files $uri $uri/ =404;
  }
}

server {
  listen ipv4.addr:443 ssl http2;
  listen [ipv6addr]:443 ssl http2;
  server_name _;
  root /usr/share/nginx/html;
  include /etc/nginx/ssl.conf;
  location /local/ {
    allow all;
    satisfy any;
    proxy_cache one;
    proxy_cache_key "$host$request_uri";
    proxy_cache_min_uses 1;
    proxy_cache_methods GET HEAD;
    proxy_pass http://127.0.0.1:80/;
  }
}