Hauv yuav luag txhua lub vev xaib uas siv cov duab, yuav tsum tau tsim cov ntawv luam me me ntawm cov duab no, thiab feem ntau muaj ntau hom ntawv rau cov duab ntxiv.
Ntxiv qhov ntev tshiab rau daim ntawv thov uas twb muaj lawm kuj ua rau qee qhov mob taub hau. Yog li lub luag haujlwm:
Hom phiaj
Cia peb pom cov npe ntawm cov kev xav tau:
- Tsim cov duab ntxiv ntawm ib hom ntawv ya yam tsis tau qhia txog kev ua haujlwm ntxiv rau hauv daim ntawv thov txhua lub sijhawm thaum daim ntawv thov muaj nyob;
- Cov duab ntxiv tsis tas yuav tsum tau tsim rau txhua qhov kev thov;
- Tshem tawm lub peev xwm los tsim cov duab ntxiv ntawm cov hom tsis tau teev tseg.
Kuv yuav piav qhia lub ntsiab lus kawg, vim nws me ntsis contradicts thawj taw tes. Yog tias peb ua cov cim ntawm ib qho duab qhib, ces muaj peev xwm ua rau muaj kev tawm tsam ntawm qhov chaw los ntawm kev tsim ntau tus neeg thov kom hloov pauv cov duab rau hauv ib qho tsis kawg ntawm cov qauv, yog li qhov tsis zoo no yuav tsum raug kaw.
nginx installation configuration
Txhawm rau daws cov kev xav tau saum toj no peb xav tau cov txheej txheem nginx hauv qab no:
ngx_http_image_filter_module - rau resizing cov duab;ngx_http_proxy_module - rau caching;ngx_http_secure_link_module - tiv thaiv spam;
Cov qauv ngx_http_image_filter_module ΠΈ ngx_http_secure_link_module tsis tau teeb tsa los ntawm lub neej ntawd, yog li lawv yuav tsum tau teev nyob rau ntawm theem teeb tsa kev teeb tsa nginx:
phoinix@phoinix-work:~/src/nginx-0.8.29
$ ./configure --with-http_secure_link_module --with-http_image_filter_module
nginx configuration
Peb ntxiv ib qho tshiab rau peb tus tswv tsev configuration qhov chaw thiab general cache parameters:
...
proxy_cache_path /www/myprojects/cache levels=1:2 keys_zone=image-preview:10m;
...
server {
...
location ~ ^/preview/([cir])/(.+) {
# Π’ΠΈΠΏ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ
set $oper $1;
# ΠΠ°ΡΠ°ΠΌΠ΅ΡΡΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ ΠΈ ΠΏΡΡΡ ΠΊ ΡΠ°ΠΉΠ»Ρ
set $remn $2;
# ΠΡΠΎΠΊΡΠΈΡΡΠ΅ΠΌ Π½Π° ΠΎΡΠ΄Π΅Π»ΡΠ½ΡΠΉ Ρ
ΠΎΡΡ
proxy_pass http://myproject.ru:81/$oper/$remn;
proxy_intercept_errors on;
error_page 404 = /preview/404;
# ΠΠ΅ΡΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅
proxy_cache image-preview;
proxy_cache_key "$host$document_uri";
# 200 ΠΎΡΠ²Π΅ΡΡ ΠΊΠ΅ΡΠΈΡΡΠ΅ΠΌ Π½Π° 1 Π΄Π΅Π½Ρ
proxy_cache_valid 200 1d;
# ΠΎΡΡΠ°Π»ΡΠ½ΡΠ΅ ΠΎΡΠ²Π΅ΡΡ ΠΊΠ΅ΡΠΈΡΡΠ΅ΠΌ Π½Π° 1 ΠΌΠΈΠ½ΡΡΡ
proxy_cache_valid any 1m;
}
# ΠΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌ ΠΎΡΠΈΠ±ΠΊΡ
location = /preview/404 {
internal;
default_type image/gif;
alias /www/myprojects/image/noimage.gif;
}
...
}
...
Peb kuj ntxiv ib tus tswv tsev tshiab rau kev teeb tsa:
server {
server_name myproject.ru;
listen 81;
access_log /www/myproject.ru/logs/nginx.preview.access_log;
error_log /www/myproject.ru/logs/nginx.preview.error_log info;
# Π£ΠΊΠ°Π·ΡΠ²Π°Π΅ΠΌ ΡΠ΅ΠΊΡΠ΅ΡΠ½ΠΎΠ΅ ΡΠ»ΠΎΠ²ΠΎ Π΄Π»Ρ md5
secure_link_secret secret;
# ΠΡΠΈΠ±ΠΊΠΈ ΠΎΡΠΏΡΠ°Π²Π»ΡΠ΅ΠΌ ΠΎΠ½Π° ΠΎΡΠ΄Π΅Π»ΡΠ½ΡΠΉ location
error_page 403 404 415 500 502 503 504 = @404;
# location ΠΠ»Ρ ΡΠΈΠ»ΡΡΡΠ° size
location ~ ^/i/[^/]+/(.+) {
# Π³ΡΡΠ·Π½ΡΠΉ Ρ
Π°ΠΊ ΠΎΡ ΠΠ³ΠΎΡΡ Π‘ΡΡΠΎΠ΅Π²Π° *
alias /www/myproject.ru/images/$1;
try_files "" @404;
# ΠΡΠΎΠ²Π΅ΡΡΠ΅ΠΌ ΠΏΡΠ°Π²ΠΈΠ»ΡΠ½ΠΎΡΡΡ ΡΡΡΠ»ΠΊΠΈ ΠΈ md5
if ($secure_link = "") { return 404; }
# ΠΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΠΌ ΡΠΎΠΎΡΠ²Π΅ΡΡΠ²ΡΡΡΠΈΠΉ ΡΠΈΠ»ΡΡΡ
image_filter size;
}
# ΠΠΎ Π°Π½Π°Π»ΠΎΠ³ΠΈΠΈ ΠΎΡΡΠ°Π»ΡΠ½ΡΠ΅ location Π΄Π»Ρ Π΄ΡΡΠ³ΠΈΡ
ΡΠΈΠ»ΡΡΡΠΎΠ²
location ~ ^/c/[^/]+/(d+|-)x(d+|-)/(.+) {
set $width $1;
set $height $2;
alias /www/myproject.ru/images/$3;
try_files "" @404;
if ($secure_link = "") { return 404; }
image_filter crop $width $height;
}
location ~ ^/r/[^/]+/(d+|-)x(d+|-)/(.+) {
set $width $1;
set $height $2;
alias /www/myproject.ru/images/$3;
try_files "" @404;
if ($secure_link = "") { return 404; }
image_filter resize $width $height;
}
location @404 { return 404; }
}
Yog li ntawd, cov duab ntxiv tuaj yeem tau txais los ntawm kev sib txuas hauv qab no:
myproject.ru/preview/i [md5]/[path_to_image]myproject.ru/preview/c [md5]/[size]/[path_to_image]myproject.ru/preview/r [md5]/[size]/[path_to_image]
* try_files - rhiab rau qhov chaw thiab cov cim Lavxias, yog li kuv yuav tsum tau ua tus ntoo khaub lig nrog alias.
Siv hauv daim ntawv thov web
Ntawm qib kev thov web, koj tuaj yeem ua cov txheej txheem hauv qab no (Perl):
sub proxy_image {
use Digest::MD5 qw /md5_hex/;
my %params = @_;
my $filter = {
size => 'i',
resize => 'r',
crop => 'c'
}->{$params{filter}} || 'r';
my $path = ($filter ne 'i' ?
( $params{height} || '_' ) . 'x' . ( $params{width} || '_' ) . '/' :
()
) . $params{source};
my $md5 = md5_hex( $path . 'secret' );
$path = '/preview/' . $filter . '/' . $md5 . '/' . $path;
return $path;
}
my $preview_path = &proxy_image(
source => 'image1.jpg',
height => 100,
width => 100,
filter => 'resize'
);
Txawm hais tias kuv kuj xav kom suav qhov ntau thiab tsawg saib ua ntej.
Lub ncuav
Thaum rho tawm cov duab qub, saib ua ntej, ib txwm, yuav tsis raug tshem tawm ntawm lub cache kom txog thaum lub cache tsis raug cai, thiab hauv peb rooj plaub, kev saib ua ntej tuaj yeem muaj nyob rau ib hnub tom qab tshem tawm, tab sis qhov no yog lub sijhawm siab tshaj plaws.
Tau qhov twg los: www.hab.com