1

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 ?

3
  • Can you expand on what you're trying to achieve please. Caching static files it serves directly is pointless. Caching PHP is easy, but that's an "upstream / backend" in your terminology. Your "what I want" doesn't make sense, why would Nginx call itself then get a cached result? Nginx can call PHP directly and cache the results no problem. – Tim Mar 17 '17 at 19:19
  • This is not a reasonable business practice because it accomplishes nothing. – user5870571 Mar 20 '17 at 19:05
  • it accomplishes something if nginx is serving files that are on a mounted resource – Brimstedt Aug 15 '18 at 13:05
1

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

are you sure you need cache for local files? reason for cache is:

A web cache sits in between a client and an “origin server”, and saves copies of all the content it sees. If a client requests content that the cache has stored, it returns the content directly without contacting the origin server. This improves performance as the web cache is closer to the client, and more efficiently uses the application servers because they don’t have to do the work of generating pages from scratch each time.

From Nginx doc

Maybe you can use proxy_cache directive example on the same link, but I didn't tested it

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.