nginx - files match location for directory

Viewed 228

I have the following nginx-config:

server {
    listen 80;

    location /app {
        alias /var/www/html/public;
        index index.php;
        error_log /var/log/nginx/project_error.log;
        access_log /var/log/nginx/project_access.log;
        try_files $uri @nested;

        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass php:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME /var/www/html/public/index.php;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    }

    location @nested {
        rewrite /app/(.*)$ /app/index.php?/$1 last;
    }

    # all other requests go to service
    location / {
        proxy_pass http://service:8080;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

and its not working as intended:

GET /app-theme.css results in 404 Not Found from nginx - so its matched by the location /app which is not desired - that request should be answered by the last location, the service.

However, when changing to location /app/ the request is answered correctly, but all my requests into /app/ do not work anymore.

Maybe I have a wrong approach. It want to be served every request into /app/ from a directory, which also needs to serve php-files (in this case only the index.php, since thats the entry into the framework). Everything else should go into the service.

What am I missing?

1 Answers

The alias directive works in conjunction with the location directive. See this document.

If you add a trailing / to the location value, you should do the same to the alias value.

For example:

location /app/ {
    alias /var/www/html/public/;
    ...
}
Related