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

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.

Posted at Nginx Forum:
https://forum.nginx.org/read.php?2,266308,266308#msg-266308

Thanks Maxim D.,

This solution is absolutely working fine. I am suffering from last 2
month
from this problem. I didn’t notice I have done regular express mistake.
Thanks for solution. I will make better Regular Expression Concept.

Posted at Nginx Forum:
https://forum.nginx.org/read.php?2,266308,266335#msg-266335

Hello!

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

    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:

/hello/(language/locale-en-us).(js)on

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”,
http://regex.info/.


Maxim D.
http://nginx.org/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs