Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 ''

  1. look for /srv/www/
  2. look for /srv/www/ & .php
  3. look for /srv/www/
  4. look for /srv/www/ & .php
  5. if not found : /srv/www/

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)

    root        /srv/www/;

    ## 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 (403 Forbidden)

public_html/index.php is not served.

- (OK)

public_html/index.php is served.

- (OK)

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

- (redirect) (no trailing slash)

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

but url displays as


uri (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!

share|improve this question

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;
share|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 posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.