Francis Daly
March 09, 2016 08:26AM
On Wed, Mar 09, 2016 at 11:31:08AM +0000, Peter Molnar wrote:

Hi there,

> I'm facing some strange, unexpected regex behaviour in my setup.

I think the answer is at http://nginx.org/r/rewrite and the "last" flag.

What do you want to happen if the incoming request is for
/wp-content/cache/file-180x180.jpg?

What have you configured nginx to do if the incoming request is for
/wp-content/cache/file-180x180.jpg?


> location ~ "^(?:(?!.*/files/.*-[0-9]{2,4}x[0-9]{2,4}).)*\.jpe?g$" {
> rewrite ^/files(.*) /wp-content/files$1 last;
> allow 127.0.0.1;
> deny all;
> }
>
> location ~ "^/files/(.*)$" {
> try_files /wp-content/cache/$1 /wp-content/files/$1 @filesmagic;
> }
>
> location @filesmagic {
> rewrite "^/files/(.*?)-[0-9]{2,4}x[0-9]{2,4}\.jpg$"
> /wp-content/cache/$1-180x180.jpg last;
> }

> The goal of the first rule is to block access to original, unresized
> files in a WordPress setup.

The first rule matches a lot more requests than just those ones.

> This is what should happen:
> http://domain.com/files/large_original_image.jpg
> - full size image, should be blocked

That is, request should match the first location and be processed there.

"Processed" is "rewrite and start again"; or "do not rewrite and allow
or deny". "Start again" will get it back to this location, but the
rewrite will not happen the second time around because the new request does
not match the rewrite.

> http://domain.com/files/large_original_image-1280x1280.jpg
> - resized image, file exists, should be served

That is, request should match the second location, and one of the first
two try_files arguments should cause it to be served from the filesystem.

> http://domain.com/files/large_original_image-800x800.jpg
> - resized image, file does not exist, smaller file should be served

That is, request should match the second location, and the final try_files
argument should cause it to be handled in the third location.

But in that location, the rewrite will happen and the whole thing starts
again.

Now the request is for /wp-content/cache/large_original_image-180x180.jpg,
which matches the first location.

In the first location, the rewrite does not match and so the request is
allowed or denied.


As I see it, you could either add a location{} to match your
/wp-content/cache/ requests; or your @filesmagic rewrite could be to
/files/$1-180x180.jpg, so that it will not match the first location.

There probably are other ways too.

Good luck with it,

f
--
Francis Daly francis@daoine.org

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

unexpected location regex behaviour

Peter Molnar March 09, 2016 06:32AM

Re: unexpected location regex behaviour

Francis Daly March 09, 2016 08:26AM

Re: unexpected location regex behaviour

Peter Molnar March 09, 2016 09:32AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 306
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