Answer the question
In order to leave comments, you need to log in
Why is rewrite not working in nginx?
Task: distribute static through nginx.
Problem: image paths are passed as GET parameters.
I can’t understand why rewrite doesn’t work, I get into the regular match.
Source url
/_next/image?url=/assets/blog/18_01_21/rectangle.jpg&w=1920&q=75
/assets/blog/18_01_21/rectangle.jpg
location /_next/image {
rewrite_log on;
if ($args ~* "url=/_next/static(.*\.(png|jpg|bmp))") {
rewrite ^http://$host/static/$1 last;
}
if ($args ~* "url=/assets/(?<path>.*\.(png|jpg|bmp))") {
rewrite ^http://$host/assets/$path last;
}
}
location /assets {
return 200;
try_files $uri $uri/ =404;
}
Answer the question
In order to leave comments, you need to log in
Avoid rewrite and if in Nginx config.
I recommend reading:
About using regexp in map nginx
Exploring the features of map in nginx
Variable $arg_
location /_next/image {
return 301 http://$host$arg_url;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question