only .json file not load using rewrite or internal redirection cycle while internally redirecting

Maxim Dounin mdounin at
Thu Apr 21 13:38:02 UTC 2016


On Thu, Apr 21, 2016 at 02:05:24AM -0400, rahulgupta20nov wrote:

> Hi,
> In nginx conf file I have written:-
>     location /hello {
>         alias /var/www/html/hello/some_path/www;
>         try_files $uri $uri/ /helllo/some_path/www/index.html;
>     }
>     location ~ /hello/(.*)\.(css|js|html|eot|svg|ttf|woff|ico|png|map|json)
> {
>         try_files $uri $uri/ /hello/some_path/www/$1.$2;
>     }
> So when I request a json file eg:-
> http://localhost/hello/language/locale-en-us.json
> it should be redirect to 
> http://localhost/hello/some_path/www/language/locale-en-us.json
> But it not redirect to mentioned path.

Your understanding of how regular expressions work is incorrect.  
The "/hello/language/locale-en-us.json" URI, when matched by the 
regular expression specified, will match at:


That's because:

- Matching full string is not required unless anchors are 
  explicitly used.

- Between alternative branches first one is preffered in NFA 
  algorithm as used by PCRE (an hence nginx), and "js" in your 
  regex will match.

In this case, correct fix would be to use explicit anchors, "^" at 
the start and "$" at the end:

    location ~ ^/hello/(.*)\.(css|js|html|eot|svg|ttf|woff|ico|png|map|json)$ {

Only "$" is required to fix the problem with "js" vs. "json", 
but "^" is also needed to prevent the regex from matching 
".../hello..." in the middle of other unrelated URIs, and unlikely 
it's something you want to happen.

If you want to understand regular expressions better, consider 
Jeffrey Friedl's excellent book "Mastering Regular Expressions",

Maxim Dounin

More information about the nginx mailing list