0

I'm using nginx with PHP-FPM on a ISPConfig3 server.

I put the following rewrite-rule in my nginx-directives (to make prettier links in Pydio):

location ~ \.php$ {
        try_files @php;
    }

location @php {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9026;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
    }

proxy_set_header X-Accel-Mapping /var/www/XXXYYY.com/pydio/data/=/data/;

location /conf/       { deny all; }
location /data/       { internal; }

location /data/public/ {
    try_files $uri $uri.php =404 last;
}

I want URLs in pydio.XXXYYY.com/data/public/* to have a .php-extension added. This rule finds the files without the .php in the address bar but now they are downloaded instead of executed.

Since I use ISPConfig3, the rewrites for .php-files (to have them executed by PHP-FPM) is above the stated part. But I thought adding "last" should take care of that.

What else could I try? Thank you!

Michal
  • 15,429
  • 10
  • 73
  • 104
basbebe
  • 567
  • 1
  • 9
  • 25

1 Answers1

1

First of all, you misunderstand the try_files directive. There's no "last" argument and it doesn't work like you think. Please, check the documentation: http://nginx.org/r/try_files. It's technical documentation, read it literally, every word has meaning.

To solve your problem you have to remove two last arguments from try_files:

try_files $uri $uri.php =404 last;

should be replaced with:

try_files $uri $uri.php;
VBart
  • 14,714
  • 4
  • 45
  • 49
  • Hi @VBart, it now looks like this: `location /data/public/ { try_files $uri $uri/ $uri.php; } ` I saw many examples on the internet that used the `last`directive. I now see that it is wrong. But why doesn't the =404 at the end work? – basbebe Apr 09 '14 at 12:07
  • 1
    The `last` parameter you have seen in the `rewrite` directive. See the docs: http://nginx.org/r/rewrite , but `try_files` is different directive with different syntax, you can't apply parameters to it from other directives. – VBart Apr 09 '14 at 12:25
  • The `=404` at the last position means that if no files exist then the `404` error response will be triggered. But note, that it only if it stays at the end of directive, all intermediate positions are handled as paths to files... so if you after `=404` write something else, then a file with the name `=404` will be checked. – VBart Apr 09 '14 at 12:28
  • thank you for your clarification! Why doesn't `try_files $uri $uri.php =404`work then? I thought it should work and only redirect to "404" if it doesn't find a file. But now the php-file is being downloaded when I have this set… – basbebe Apr 09 '14 at 12:34
  • 1
    Nginx cannot execute php code, but it can proxy requests (using fastcgi protocol like in your case) for some other daemon that can. But the `fastcgi_pass` directive is only specified in your `@php` location, while your `location /data/public/` serves files. – VBart Apr 09 '14 at 17:17
  • 1
    The docs on `try_files` states clear about how the directive works: _Checks the existence of files in the specified order and uses the first found file for request processing; the processing is performed in the current context._ And also: _If none of the files were found, an internal redirect to the uri specified in the last parameter is made._ @ http://nginx.org/r/try_files – VBart Apr 09 '14 at 17:20
  • 1
    In case of `try_files $uri $uri.php;` - the `$uri` parameter is a file to check, and the `$uri.php` parameter is an URI to redirect if the check failed. But in your `try_files $uri $uri.php =404`, both `$uri` and `$uri.php` are just files to check. – VBart Apr 09 '14 at 17:24
  • Sorry to ask again here: Just like in the second example in the docs I thought that `try_files $uri $uri.php =404` would check if `$uri.php` existed and – if it didn't – redirect to a 404. I still don't understand why php-files are being downloaded (the redirect works) if `=404` is set. Sorry, I guess I'm being dumb… – basbebe Apr 10 '14 at 08:01
  • 1
    Because __the processing is performed in the current context__ and in the `location /data/public/` context you have no `fastcgi_pass`, so `.php` files in that context are processed like any other static files (.png, .jpg, .css, .js.. etc). Nginx knows nothing about PHP, it just works exactly how it was configured. – VBart Apr 10 '14 at 08:27