Welcome! Log In Create A New Profile

Advanced

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

Maxim Dounin
April 21, 2016 09:40AM
Hello!

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:

/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 Dounin
http://nginx.org/

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Subject Author Posted

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

rahulgupta20nov April 21, 2016 02:05AM

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

Maxim Dounin April 21, 2016 09:40AM

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

rahulgupta20nov April 21, 2016 11:18PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 209
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready