0

I have Nginx installed as a front end proxy for Apache, where Apache is servicing PHP and Nginx is servicing static files. I'm having a problem configuring Nginx to cache the output from PHP into a static file. I've tried with proxy_cache, but obviously I'm doing something wrong.

Here is my base configuration:

    server {
            listen   80;

            root /var/www/web;
            index index.php index.html index.htm;

            server_name web.com;

            location / {
                    try_files $uri $uri/ /index.php;
            }

            # cache static files
            location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
                    expires 365d;
                    access_log off;
                    add_header Cache-Control public;
            }

            location ~ \.php$ {
                    proxy_pass http://127.0.0.1:8080;

                    # Set header to be what we requested
                    proxy_set_header        Host    $host;
                    # Set proxy_cache expiry time
                    proxy_cache_valid  200 302  5m;
                    proxy_cache_valid  404      1m;
                    proxy_cache_valid  301      1h;
                    # Need this for snooping with tcpdump (turns off upstream compression)
                    proxy_set_header        Accept-Encoding  "";
                    # Set real IP header (needed for client IP detection in apache)
                    proxy_set_header  X-Real-IP  $remote_addr;
                    # Explicitly allow critical headers
                    proxy_pass_header Set-Cookie;
                    # Prevent 304 responses being returned from Apache and cached by nginx
                    proxy_set_header If-None-Match "";
            }

            location ~ /\.ht {
                    deny all;
            }
    }
SlasherZ
  • 77
  • 1
  • 10
  • 1
    Did you set up the proxy_cache_path? See the answer to this similar question: http://serverfault.com/questions/30705/how-to-set-up-nginx-as-a-caching-reverse-proxy – Brian P Aug 30 '13 at 21:07

1 Answers1

0

set proxy_cache_path in your http{} - context and define your zone and parameters

http { 
...

proxy_cache_path /tmp/cache/blue blue:100m ...;
proxy_cache_path /tmp/cache/white white:100m  ...;

... 
}

set proxy_cache zone/off in corresponding server/location-context and your options as well

e.g. define your cache pathes globally and activate/deactivate for each server/location

location /blue {


proxy_cache blue;
proxy_cache_...


}

location /white {


proxy_cache white;
proxy_cache_...


}


location /red {


proxy_cache off;
proxy_cache_...


}

beware, your cached files will be in binary format

  • I'm still not able to get the cache working..the cache directory is always empty. (I've checked users and permissions on the directory, everything is ok). Here is my configuration: https://gist.github.com/SvenAlHamad/6397956 – SlasherZ Aug 31 '13 at 12:45
  • your config seems ok. do you have any entries in your error.log? /var/cache must be readable by www-data (chmod 755) and /var/cache/nginx must be writable by www-data (chown). – that guy from over there Aug 31 '13 at 14:21
  • everything is fine there...but I noticed one strange thing. When I open mysite/index.php the cache is not created, so I decided to create a test file, text.php and inside I just wrote 'hello'. When I open mysite/test.php the cache file is created inside /var/cache/nginx Here are the headers http://imgur.com/Ei5ri6v http://imgur.com/4RgDUnn – SlasherZ Aug 31 '13 at 15:40
  • does the cache-control-header comes from apache too? http://stackoverflow.com/a/6077195. oh, and could you please change your answer to "how to configure proxy_cache?" – that guy from over there Aug 31 '13 at 15:54
  • i asked to change the title of your question, not answer :) – that guy from over there Aug 31 '13 at 16:20
  • I've managed to get the same response headers on index.php as on test.php using proxy_hide_header..but still cache file is not created :( The test.php script creates the cache files normally. I managed to figure out that if I call 'session_start();' in PHP, then cache file is not created...if I remove the session_start, cache file is created. – SlasherZ Aug 31 '13 at 20:05
  • hmmm, no clue. maybe you ask on the [nginx-forum](http://forum.nginx.org/) – that guy from over there Sep 01 '13 at 09:55