NGINX try_files + alias directives

We could not get it to work by specifying root within the location block. The solution for us was to use alias instead. Note that it is necessary repeat the location’s path twice in the try_files directive, and then also in the .php configuration block:

server {
    server_name localhost;
    root /app/frontend/www;

    location /backend/ {

        alias /app/backend/www/;

        # serve static files direct + allow friendly urls
        # Note: The seemingly weird syntax is due to a long-standing bug in nginx: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /backend//backend/index.php?$args;

        location ~ /backend/.+\.php$ {
            include fastcgi_params;
            fastcgi_buffers 256 4k;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_param HTTPS $proxied_https;
            fastcgi_pass phpfiles;
        }

    } # / location

}

Source: nginx/conf.d/app.conf from the debian-php-nginx stack in the docker-stack project

Leave a Comment