Upgrade to Pro — share decks privately, control downloads, hide ads and more …

実践ngx_small_light入門

 実践ngx_small_light入門

at 画像変換Night

Tatsuhiko Kubo

February 18, 2015
Tweet

More Decks by Tatsuhiko Kubo

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ • ٱอୡ඙(Tatsuhiko Kubo) • bokko@cubicdaiya • Software Engineer in

    Infrastructure Engineering • Mercari, Inc. (ࡢ೥ͷࠓࠒ͸·ͩpixiv, Inc.Ͱͨ͠) • Go / C / mruby / nginx / ngx_(lua|mruby) / etc…
  2. ngx_small_lightͰͰ͖Δ͜ͱ • ը૾ͷϦαΠζ / Ϋϩοϓ / ߹੒ / ճస •

    ϑΥʔϚοτม׵(e.g. PNG -> JPEG) • ϒϥʔ / (Ξϯ)γϟʔϓ౳ͷՃ޻ॲཧ • etc…
  3. Setup cd ngx_small_light # enable ImageMagick ./setup # enable ImageMagick

    and Imlib2 ./setup —-with-imlib2 # enable ImageMagick and GD ./setup —-with-gd # enable ImageMagick and Imlib2 and GD ./setup —-with-imlib2 —with—gd
  4. Quick Start # server context in nginx.conf small_light on; location

    ~ small_light[^/]*/(.+)$ { set $file $1; rewrite ^ /$file; } ݩը૾ͷ63* JNHJNBHFKQH ม׵༻ͷ63* TNBMM@MJHIU EX EI JNHJNBHFKQH
  5. σΟϨΫςΟϒҰཡ σΟϨΫςΟϒ໊ ղઆ small_light small_lightͷon/off small_light_pattern_define αϜωΠϧੜ੒ύλʔϯΛఆٛ small_light_getparam_mode GETύϥϝʔλͰαϜωΠϧੜ੒ύλʔϯΛఆٛ small_light_material_dir

    ߹੒ૉࡐͷσΟϨΫτϦΛࢦఆ small_light_imlib2_temp_dir imlib2͕ੜ੒͢ΔςϯϙϥϦϑΝΠϧͷύε small_light_buffer Content-Length͕ۭͷ৔߹ʹ֬อ͢ΔϝϞϦόοϑΝͷαΠζ
  6. αϜωΠϧੜ੒ύλʔϯΛఆٛ # in server context small_light_pattern_define medium dw=600,dh=400; small_light_pattern_define small

    dw=300,dh=200; /small_light(dw=300,dh=200)/img/image.jpg /small_light(p=small)/img/image.jpg → ▪ઃఆ ▪URI
  7. جຊతͳύϥϝʔλ ύϥϝʔλ໊ ղઆ dw, dh ੜ੒͢ΔαϜωΠϧͷ෯ͱߴ͞(e.g. dw=300, dh=200) of ੜ੒͢ΔαϜωΠϧͷϑΥʔϚοτ(jpg

    / gif / png / webp) q ը࣭(e.g. 95) p small_light_pattern_defineͰఆٛ͞ΕͨαϜωΠϧύλʔϯΛ୅ೖ e ը૾ม׵ϥΠϒϥϦΛࢦఆ(imagemagick / imlib2 / gd)
  8. small_light_material_dir # not trailing slash small_light_material_dir /var/materials; # there is

    the icon.jpg in /var/materials. small_light_pattern_define embed dw=300,dh=200,embedicon=icon.jpg,ix=10,iy=15; /small_light(p=embed)/img/image.jpg + = ▪URI ▪ઃఆ
  9. small_light_imlib2_temp_dir # default setting small_light_imlib2_temp_dir /tmp 1 2 0; ▪ઃఆ

    • Imlib2͕อଘ͢ΔςϯϙϥϦϑΝΠϧͷύε • Imlib2͸ϑΝΠϧ୯ҐͰ͔͠ը૾Λѻ͑ͳ͍ • ઃఆͷॻࣜ͸proxy_temp_pathͱҰॹ
  10. small_light_buffer # default setting small_light_imlib2_temp_dir /tmp 1 2 0; ▪ઃఆ

    • ֤ϦΫΤετຖʹ֬อ͢ΔϝϞϦόοϑΝͷαΠζ • ௨ৗ͸ར༻͞Εͳ͍(Content-Length෼֬อ͢Δ) • ྫ͑͹ɺϓϩΩγઌͷαʔόͷϨεϙϯεʹ Content-Lengthؚ͕·Ε͍ͯͳ͍৔߹ʹར༻͢Δ # default setting small_light_buffer 1m;
  11. ֎෦αʔό(e.g. S3)ʹ͋Δը૾Λ ngx_small_lightͰϦαΠζ # server context in nginx.conf small_light on;

    location ~ small_light[^/]*/(.+)$ { set $file $1; rewrite ^ /$file; } location ~ /img { proxy_pass http://external_host; } /small_light(dw=300,dh=200)/img/image.jpg ▪ઃఆ ▪URI
  12. Quick Start location ~ \.(png|gif|jpe?g)$ { image_filter resize 150 150;

    } (mod|ngx)_small_lightͷΑ͏ͳ ม׵༻URIͷ֓೦͕ͳ͍
  13. go-thumber • pixivۘ੡ը૾ม׵ϓϩΩγαʔό • ੍໿ͱੑೳ • JPEG & Resize only

    • High Speed & High Quality • ಛఆͷ৚݅ԼͰઈେͳޮՌΛൃش͢Δ
  14. ngx_small_lightͷϨεϙϯεΛ nginxͰΩϟογϡ͢Δ؆୯ͳྫ proxy_cache_path /tmp/cache levels=1:2 key_zone=images:5m inactive=1h max_size=10G; server {

    server_name origin_server; … location ~ / { proxy_cache images; proxy_cache_valid 200 1h; proxy_cache_key “$scheme://$host$request_uri$is_args$args”; proxy_pass http://small_light_server; } } server { server_name small_light_server; … small_light on; location ~ small_light[^/]*/(.+)$ { set $file $1; rewrite ^ /$file; } }