I am trying to create a combined "super cache" -> file -> module pattern with try_files ("super cache" term taken from wordpress super cache as it is a similar method).

The files that are applicable for caching are located inside the "cache" folder.

The file should be served if it exists in one of these locations.

  1. /$cache$uri
  2. /$cache$uri/
  3. $uri
  4. $uri/

1.cache -> 2.file -> 3.pattern

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

Example with address 'domain.com/css/style588.css'

  1. look for /srv/www/domain.com/public_html/cache/css/style588.css
  2. look for /srv/www/domain.com/public_html/cache/css/style588.css/index.html & .php
  3. look for /srv/www/domain.com/public_html/css/style588.css
  4. look for /srv/www/domain.com/public_html/css/style588.css/index.html & .php
  5. if not found : /srv/www/domain.com/public_html/index.php

I have tried to make it work with the config below. What am i doing wrong?

included domain.conf from nginx.conf

server {
    ## index index.html index.php (defined in http block, works)

    server_name domain.com;
    root        /srv/www/domain.com/public_html;

    ## cache directory ##
    set $cache_dir 'cache';

    ## no cache if post ##
    if ($request_method = POST) {
        set $cache_dir 'null dir';

    ## 1.cache -> 2.file -> 3.pattern ##
    location / {
        try_files /$cache_dir$uri /$cache_dir$uri/ $uri $uri/ /index.php;

    ## .php (set cgi.fix_pathinfo=0 in php.ini, especially if php is on another machine) ##
    location ~ \.php$ {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

Examples i tried with above config

domain.com: (403 Forbidden)

public_html/index.php is not served.


domain.com/non_existing_folder: (OK)

public_html/index.php is served.


domain.com/folder_exists_in_cache/: (OK)

public_html/cache/folder_exists_in_cache/index.html is served.


domain.com/folder_exists_in_cache: (redirect) (no trailing slash)

Redirect header 301 permanent. public_html/cache/folder_exists_in_cache/index.html is served.

but url displays as domain.com/cache/folder_exists_in_cache/


uri domain.com/cache/existing_empty_folder: (403 Forbidden)

public_html/index.php is not served.


How do i avoid 403 forbidden (and the redirects when there is no trailing slash but the folder exists in cache?)

Have tried setting permissions on folders, with the same result.

Thanks, for your help!


This should work:

    location ~ \.php$ {
        try_files $uri =404;
    location = / {
        try_files /nonexistent /index.php?q=$uri&$args;
    location / {
        try_files $cache_dir$uri $cache_dir$uri/index.html $uri $uri/index.html /index.php?q=$uri&$args;
| improve this answer | |
  • Thanks for the reply. the "= /" location works, but there are still some of the same issues. I'll investigate / test further in the weekend. I'll try to get a better understanding of location, try_files and rewrite and post the answer if i figure it out. – Milou Jan 25 '13 at 15:01

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.