Nginx – Using regex matches from nginx location in try_files directive

nginxnginx-location

I have the following location blocks in a nginx server block config:

location /eci-new/public {
    try_files  $uri   $uri/ /eci-new/public/index.php?$query_string;
}

location /eci-integration/public {
    try_files  $uri   $uri/ /eci-integration/public/index.php?$query_string;
}

it works, but I will need to add more locations in the future, I'd like to reduce that to only one location block that use a uri segment dinamicaly to apply the try_files directive config like this:

location ~ /(.*)/public {
    try_files  $uri   $uri/ /$1/public/index.php?$query_string;
}

But I'm getting this message in the console in chrome when try to load the page.

Resource interpreted as Document but transferred with MIME type application/octet-stream: "http://33.33.33.33/eci-new/public/rms/product-normalization".

How can I make the nginx config work correctly? and send the request to the php procesor?

Best Answer

Because you have change your prefix location to a regex location, it now conflicts with your php location. So the /eci-new/public/index.php URI is not being interpreted as a php script.

Move the php location (something like location ~* \.php) so that it is above your new regex location, and therefore takes priority for the php scripts.

Also, you might want to tighten up your regex and add a ^ at the beginning:

location ~ ^/(.*)/public { ... }

See this document for details.

Related Topic