Vocabulary dictionary

Kanji dictionary

Grammar dictionary

Sentence lookup

test
 

Forums - [HELP] nginx rewrite issues (image server)

Top > renshuu.org > Developer corner



avatar
マイコー
Level: 264

renshuu's image server does, among other things, redirecting from png/jpg to webp when supported, as well as on-the-fly resizing of files (different pixel densities for different devices).

It works, but it's skipping a number of steps, causing caching to not work all the time, and the downloads to be a bit slower than they should be. All the issues are inside of nginx.

For nginx, my http block contains this (the formatting is right in the file, but less than ideal in here, sorry!):

map $http_accept $jpg { default ".jpg"; "~image/webp" ".webp"; }

This allows me to rewrite the file extension to the appropriate type


My location block looks like this (heads and log files ignored)

location ~ ^/.*\.jpg.*?$ {

rewrite ^(.+?).jpg(.*)$ $1$jpg last;

}

If the URI is /img/wpics/27105.jpg (one of the vocabulary images),


Expected (after rewrite):

/img/wpieces/27105.webp (if webp is accepted, otherwise no change)


Actual:

.webp

-->In other words, that $1 is not getting the (.+?) value.



However, if I change the rewrite line to the following

rewrite ^(.+?).jpg(.*)$ $1 last;

It will give me /img/wpics/27105 (as expected)

It's almost as if the two variables are somehow interfering with one another.

I've tried variations: $jpg$1, $1&$jpg <-- in all cases, if $jpg is in there, then $1 doesn't output anything. I feel like the map processing is causing issues somehow. Here is the rewrite debug output:

2024/04/04 06:02:34 [notice] 1145540#1145540: *92329376 "^(.+).jpg(.*)$" matches "/img/wpics/27105.jpg", client: xxx.xxx.xx.xx, server: itest.renshuu.org, request: "GET /img/wpics/27105.jpg HTTP/2.0", host: "itest.renshuu.org"
2024/04/04 06:02:34 [debug] 1145540#1145540: *92329376 http map started
2024/04/04 06:02:34 [debug] 1145540#1145540: *92329376 http script var: "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"
2024/04/04 06:02:34 [debug] 1145540#1145540: *92329376 http map: "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7" ".webp"
2024/04/04 06:02:34 [debug] 1145540#1145540: *92329376 http script capture: ""
2024/04/04 06:02:34 [debug] 1145540#1145540: *92329376 http script var: ".webp"
2024/04/04 06:02:34 [debug] 1145540#1145540: *92329376 http script regex end



I feel that somehow, the map is breaking the rewrite flow, but I cannot figure out why.

1
1 month ago
Report Content
avatar
マイコー
Level: 264

So, in short, I gave up - writing all of this let me figure out another way around it. Instead of trying to rewrite the ending, I switched to an if() statement with an addition on the end.

if ( $jpg = ".webp" ) {
rewrite ^(.+\.jpg)(.*)$ $1.webp last;
}

1
1 month ago
Report Content
avatar

if ( $jpg = ".webp" ) {
rewrite ^(.+\.jpg)(.*)$ $1.webp last;
}

Maybe I am confused, but the condition looks wrong. It should be checking for jpg, not webp, right?

0
1 month ago
Report Content
avatar
Valdemar Busk
Level: 462

So maybe I don't fully understand, but wouldn't this:

rewrite ^(.+\.jpg)(.*)$ $1.webp last;

lead to "/img/wpics/27105.jpg.webp"?

I think this should yield "/img/wpics/27105.webp", assuming that that's what's intended?

rewrite ^(.+)\.jpg$ $1.webp last;

Also, I assume that the map gets evaluated in the rewrite's inner part, so the $1 would be referencing the regex from inside the map condition "~image/webp". I think it should be possible to either replace the map condition with "image/webp", which is probably not helpful, or bind the map variable to a temporary variable before the rewrite to dereference the map value. There might be other ways, but these are the two I came up with on the spot.

location ~ ^/.*\.jpg.*?$ {

set $local_jpg $jpg;

rewrite ^(.+?)\.jpg(.*)$ $1$local_jpg last;

}

0
1 month ago
Report Content
avatar
マイコー
Level: 264

You are right - my change ended up being a switch from

change .jpg to .webp

to

change .jpg to .jpg.webp

It ended up making it a bit more flexible, because if the file does not exist, the node.js server can more easily see the original .jpg file that is trying to be called, and made it easier for me to blend the code so it can ultimately handle .png and .jpg

That set command is an interesting one - I didn't even think of copying it over like that. If I need to get around it again next time, I'll definitely give that a shot!

0
1 month ago
Report Content
Getting the posts




Top > renshuu.org > Developer corner


Loading the list
Lv.

Sorry, there was an error on renshuu! If it's OK, please describe what you were doing. This will help us fix the issue.

Characters to show:





Use your mouse or finger to write characters in the box.
■ Katakana ■ Hiragana