S
S
Stanislav2017-09-14 12:16:25
Nginx
Stanislav, 2017-09-14 12:16:25

How to process a GET request in nginx?

I am very unfamiliar with Nginx, please help =(
There are links like

img.domain.ru/o/99/image.jpg
img.domain.ru/o/99/image.jpg?width=500&height=500

In the configuration, I'm trying to make it so that if the width and height parameters are passed to GET, then crop the image, if there are none, then display the original image.
server {
        listen       80;
        server_name  img.domain.ru;

        location ~* \.(gif|jpg|png)$ {
       proxy_pass http://imageserver;
             image_filter crop $arg_width $arg_height;
             error_page 415 = /empty;
        }

        # Error handler
        location = /empty {
            empty_gif;          # Respond with empty image
        }
}

# Backend image server
server {
        listen       8082;
        server_name  localhost;
        root /var/www/img.domain.ru;

        rewrite  ^/(.*)$   /$1  last;
}

# Upstream
upstream imageserver {
    server localhost:8082;
}

With this configuration, pictures with a GET request are displayed, without it they are not, since $arg_width and $arg_height are not defined. I tried to make conditions with if but there was an error in the logs ("image_filter" directive is not allowed here in)
How to process such requests correctly?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Mukovoz, 2017-09-14
@ms-dred

location ~* \.(gif|jpg|png)$ {
 proxy_pass http://imageserver;
 if ($arg_width != "") { return 412; }
 error_page 412 = @img;
 error_page 415 = /empty;
}
location @img { image_filter crop $arg_width $arg_height; }

Perhaps there is not enough elegance and maybe you will have to tweak it a little for your configuration, but I think you will understand the direction)

B
Boris Korobkov, 2017-09-14
@BorisKorobkov

Make other URLs. For example:

img.domain.ru/o/99/image.jpg
img.domain.ru/o/99/800/600/image.jpg

and two locations.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question