Rewrite before regex location

Joyce Babu joyce at
Wed May 4 22:52:11 UTC 2016

Hi Francis,

Thank you for the response.

Possibly using "location ^~ /test/" would work?
> You may want to rewrite to /test.php (with the leading slash), though.
> Although, better might be to just fastcgi_pass directly, rather than
> rewriting.
> Something like (untested)
>   location ^~ /test/ {
>     fastcgi_param SCRIPT_FILENAME $document_root/test.php;
>     fastcgi_param QUERY_STRING q=$uri;
>     include fastcgi_params;
>     fastcgi_pass;
>   }
> although you would want to change the QUERY_STRING line to match what
> you need; and you may need to switch the position of the "include",
> depending on how your fastcgi server handles repeated params.

There are over 300 rewrites under 54 location blocks.

I tried using ^~ as you suggested. Now the rewrite is working correctly,
but the files are not executed. The request is returning the actual PHP
source file, not the HTML generated by executing the script.

> > location ~ [^/]\.php(/|$) {
> >     fastcgi_split_path_info ^(.+?\.php)(/.*)$;
> >
> >     set $fastcgi_script_name_custom $fastcgi_script_name;
> >     if (!-f $document_root$fastcgi_script_name) {
> >         set $fastcgi_script_name_custom "/cms/index.php";
> >     }
> I suspect that it should be possible to do what you want to do there,
> with a "try_files". But I do not know the details.

There is a CMS engine which will intercept all unmatched requests and check
the database to see if there is an article with that URI. Some times it has
to match existing directories without index.php. If I use try_files, it
will either lead to a 403 error (if no index is specified), or would
internally redirect the request to the index file (if it is specified),
leading to 404 error. The if condition correctly handles all the
non-existing files.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the nginx mailing list