J
J
JunDevTest2018-10-01 12:31:18
Nginx
JunDevTest, 2018-10-01 12:31:18

Is it possible to fix this Nginx rule? If so, how?

Hello.
There is an Nginx rule, it should work exactly like this. Naturally, it doesn't work, because in the configuration of this web server it is forbidden to insert the logical construct If inside the Location , as well as vice versa. In any case, the built-in checker ( nginx -t ) pointed me to this oversight and illiteracy.

location ~* ^.+\.(jpeg|jpg|png|bmp)$ {
...
    if ($img_extension = ".webp"){
        location ~ /image-content/(.*) {
            add_header 'Content-Type' 'image/webp';
            try_files $uri /image-content/webp/image-content/${1}${img_extension} /webp-gen.php?source=$document_root$request_uri&image-content=image-content&$args;
        }
    }
...
}

What's going on here? (What should happen)
0. If the $img_extension variable contains the value ".webp", then the condition is executed.
1. The server receives a request like https://example.org/image-content/uploads/test_ima... .
2. The server looks for the test_image.png.webp file at the uri /image-content/uploads/webp/test_image.png.webp .
3. Set the header "image/webp" .
4. If it finds it, it returns a response with this file.
5. If uri /image-content/uploads/webp/test_image.png.webp does not have it, then redirection to uri is performed/webp-gen.php?source=$document_root$request_uri&image-content=image-content&$args .
The sections at /image-content/uploads/webp/ mirror the sections at /image-content / uploads/ except for the webp directory .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Karpion, 2018-10-04
@Karpion

Maybe check the location in the conditions? Something like this:
if ($img_extension = ".webp" and location = ...)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question