2

I have to update a nginx host so all requests to an alias are rewritten to /alias_path/index.php?q=$uri. But now all the assets are no longer available.

This is my current configuration. And I'm getting closer, but when I am uncommenting the last location, the assets are no longer available.

    location /csv-import {
            alias /var/www/csv-import/public;

            location ~ \.php$ {
                    include snippets/fastcgi-php.conf;
                    fastcgi_param  SCRIPT_FILENAME        $request_filename;
                    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            }

            try_files $uri $uri/ =404;

            #location ~ ^/csv-import/(.*)$ {
                    #alias /var/www/csv-import/public;
                    #try_files $uri $uri/ /csv-import/index.php?q=$1;
            #}

            error_log /var/log/nginx/csv-import.error.log;
            access_log /var/log/nginx/csv-import.access.log;
    }

The file I wan't to reach is /var/www/csv-import/public/index.php. All Urls like example.com/csv-import/some/url should be rewritten to example.com/csv-import/index.php?q=some/url but assets like example.com/csv-import/css/app.css should be available under /var/www/csv-import/public/css/app.css.

I'm shure there is a solution that works perfect but I couldn't come up with it.

Thomas Venturini
  • 3,500
  • 4
  • 34
  • 43

1 Answers1

1

You do not need another location block. The usual method is to change the default action of the try_files statement. But because of this issue, an if block may be simpler:

location ^~ /csv-import {
    alias /var/www/csv-import/public;

    if (!-e $request_filename) {
        rewrite ^/csv-import/(.*)$ /csv-import/index.php?q=$1 last;
    }

    location ~ \.php$ {
        ...
    }

    error_log /var/log/nginx/csv-import.error.log;
    access_log /var/log/nginx/csv-import.access.log;
}

The if block replaces the try_files statement. The ^~ operator avoids any ambiguity with other location blocks. See this caution on the use of if.

Richard Smith
  • 45,711
  • 6
  • 82
  • 81