try_files php

☣Merlin merlincorey at dc949.org
Thu May 27 22:52:51 MSD 2010


On Thu, May 27, 2010 at 11:13 AM, Marcos Neves <marcos.neves at gmail.com> wrote:
> How can I reproduce this behavior:
>
> with a request to /foo/bar
>
> try file: foo/bar
> try index.htm: foo/bar/
> try file: foo/bar.htm
> try file: foo/bar.html
> try parse php: foo/bar.php
> try file: /layout.htm
> try file: /layout.html
> try parse php: /layout.php
> return 404 if not found.
>
> I try this:
>
>    location / {
>      try_files $uri $uri/ $uri.htm $uri.html $uri.php layout.htm
> /layout.html /layout.php;
>    }
>
> Everything works, except PHP that returns as text file showing the source code.
>
> ps: Is there a blog post that explain exactly how location, if,
> rewrite and try_files works?
> The docs are not so clear about how the flow works.
>
> Marcos Neves
>
> _______________________________________________
> nginx mailing list
> nginx at nginx.org
> http://nginx.org/mailman/listinfo/nginx
>

As someone else mentioned it serves the file because that is what you
asked it to do.  You will probably need something more convoluted
along the following lines, but I don't guarantee it is correct (to
whit, I don't think the try_files in @second is correct but I copied
your line):

upstream backend {
  server 127.0.0.1:9000;
}
server {
  listen 80;
  server_name supercomplicatedphpcrap;
  root /path/to/root;

  location / {
    try_files $uri $uri/ $uri.htm $uri.html @first;
  }

  location @first {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $uri.php
    fastcgi_pass backend;
    fastcgi_intercept_errors on;
    error_page 404 =@second;
  }

  location @second {
    try_files layout.htm /layout.html /layout.php;
  }
}

-- Merlin



More information about the nginx mailing list