K
K
Kirill Firsov2011-03-09 22:50:20
Perl
Kirill Firsov, 2011-03-09 22:50:20

nginx config how?

Hello.
We need to make a location rule, but I'm not familiar with nginx / perl.
When accessing /original/$ID. jpg|png|gif
It is necessary that the image be returned along the path
original/
the last character md5($ID) /
the first 2 characters md5($ID) /
3rd and 4th character md5($ID). jpg|png|gif

Answer the question

In order to leave comments, you need to log in

5 answer(s)
I
Ivan, 2011-03-10
@Isis

wiki.nginx.org/EmbeddedPerlModule
Something like this (didn't test it, but it should seem to work):
nginx.conf:

http {
    perl_require myhandler.pm;
    ...
    server {
        ...
        location ~* /original/\.(gif|jpg|png)$ {
            perl myhandler::handler;
        }
    }
}

myhandler.pm:
packagemyhandler;
use nginx;
use Digest::MD5 qw(md5_hex);

sub-handler {
  my $r = shift;
  if ($r->filename=~m|/original/(.*?)\.(jpg|gif|png)|) {
      my $id_md5=md5_hex($1);
      my $filename="/original/".substr($id_md5,31,1)."/".substr($id_md5,0,2)."/".substr($id_md5,2,2).". ".$2;
      $r->sendfile($filename);
  }
  return OK;
}
 
one;
__END__

K
kekekeks, 2011-03-09
@kekekeks

Put this logic into a script, and then serve the image through internal redirects .

I
IlVin, 2011-03-10
@IlVin

The function of calculating MD5 in the nginx config is only in ephemeral plans ... but for now it is impossible to calculate MD5 from something (without a third-party module).
It’s a completely different matter if suddenly $ID itself turns out to be an MD5 sum ...
Then (the code was not tested):
location /original/ {
rewrite ^/original/([0-9a-f][0-9a-f])([0- 9a-f]+)([0-9a-f])\. (jpg|png|gif)$ /original/$3/$1/$1$2$3.$4 last;
}
Read the documentation for more details:
sysoev.ru/nginx/docs/http/ngx_http_rewrite_module.html

O
ozware, 2011-03-09
@ozware

Maybe this will help something: sysoev.ru/nginx/docs/http/ngx_http_secure_link_module.html
IMHO, you can't do this in nginx, you need to do something differently

I
IlVin, 2011-03-10
@IlVin

Remembered! Now I will teach the bad...
Here catap.ru/blog/2009/05/13/nginx-crc32_name-and-md5_name/ patch to nginx, which calculates the MD5 amount, please read all the comments on the link before applying it...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question