Search code examples
nginxaliasproxypass

Nginx Proxy_pass try_files drop to location handler


I'm running into small hick up with try_files in combination with proxy_pass (or alias for that matter).

Current config:

location /staticsrv {
    alias /var/www/static/trunk/;
    #proxy_pass http://static.localtest.nl/;
}
location ~ ^/staticsrv/images/gallery/(.*)$ {
    try_files $uri @img_proxy;
}
location @img_proxy {
    rewrite ^(.*)$ /index.php/?c=media&m=index&imag=$uri;
}

However for every file it gets dropped to the rewrite rule as it doesn't exist. Is there a "trick" (read correct config) to fix my misfortune? Or is it just not possible? Both domains will eventually be on the same server so we can work with alias and proxy_pass.

Thanks in advance


Solution

  • Your location ~ ^/staticsrv/images/gallery/(.*)$ needs a root or an alias to construct a local path for try_files to try. Also, you do not necessarily need a regular expression here:

    location /staticsrv/images/gallery/ {
        alias /var/www/static/trunk/images/gallery/;
        try_files $uri @img_proxy;
    }
    location @img_proxy {
        rewrite ^ /index.php/?c=media&m=index&imag=$uri last;
    }
    

    proxy_pass will not work with try_files as one deals with remote content and the other with local content.

    I try to avoid using alias and try_files in the same location block because of this open bug.

    A possible work around would be to use another intermediate URI that closely matches the document root:

    location /staticsrv/images/gallery/ {
        rewrite ^/staticsrv(.+)$ /trunk$1 last;
    }
    location /trunk {
        internal;
        root /var/www/static;
        try_files $uri @img_proxy;
    }
    location @img_proxy {
        rewrite ^ /index.php/?c=media&m=index&imag=$uri last;
    }