V
V
Vyacheslav2021-10-10 10:23:05
Nginx
Vyacheslav, 2021-10-10 10:23:05

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

Target url /assets/blog/18_01_21/rectangle.jpg

nginx config piece:
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;
}

The output is always 404, writes to error.log open() "@[email protected]/_next/image" failed (2: No such file or directory)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Karabanov, 2021-10-10
@Firik67

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 question

Ask a Question

731 491 924 answers to any question