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

Nginxのお話 画像配信・APIキャッシュ利用事例 2011/11/11 Webサーバ勉強会

Nginxのお話 画像配信・APIキャッシュ利用事例 2011/11/11 Webサーバ勉強会

PHPを使うには、ApacheをPreforkで動作させる必要があります。そういった環境で画像配信を行うと必要な同時接続数がとても多くなるため効率的ではありません。そこで、Nginxを併用した高速静的ファイル配信方法を紹介します。
2つ目のトピックには、APIリクエストをNginxにて透過的にプロキシすることでコンテンツキャッシュを行う仕組みを紹介します。

Kentaro Yoshida

November 11, 2011
Tweet

More Decks by Kentaro Yoshida

Other Decks in Technology

Transcript

  1. #1 ApacheͱNginxͷ࿈ܞͰߴ଎ը૾഑৴ upstream backend-apache { server 127.0.0.1:80; } server {

    listen 8080; server_name _; access_log off; location / { log_not_found off; expires 12h; root /var/www/site; try_files /$host$uri /$host/web/$uri @unicorn; } location @unicorn { log_not_found off; expires 12h; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://backend-apache; } }
  2. upstream backend-apache { server 127.0.0.1:80; } server { listen 8080;

    server_name _; access_log off; location / { log_not_found off; expires 12h; root /var/www/site; try_files /$host$uri /$host/web/$uri @unicorn; }
  3. location / { log_not_found off; expires 12h; root /var/www/site; try_files

    /$host$uri /$host/web/$uri @unicorn; } location @unicorn { log_not_found off; expires 12h; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://backend-apache; } }
  4. #2 NginxʹΑΔதؒΩϟογϡαʔό proxy_cache_path /dev/shm/nginx-cache/test levels=1 keys_zone=test-cache:2m max_size=500m inactive=1h; upstream api-backends

    { server 127.0.0.1:80; } server { listen 192.168.50.20:80; server_name hoge.jp; access_log off; error_log /var/log/nginx/error.log; proxy_set_header Host $host; location /service-api/ { proxy_cache test-cache; proxy_cache_valid 200 4h; proxy_cache_valid any 1m; proxy_pass http://api-backends; } }
  5. proxy_cache_path /dev/shm/nginx-cache/test levels=1 keys_zone=test-cache:2m max_size=500m inactive=1h; upstream api-backends { server

    127.0.0.1:80; } server { listen 192.168.50.20:80; server_name hoge.jp; access_log off; error_log /var/log/nginx/error.log; proxy_set_header Host $host; location /service-api/ {
  6. server { listen 192.168.50.20:80; server_name hoge.jp; access_log off; error_log /var/log/nginx/error.log;

    proxy_set_header Host $host; location /service-api/ { proxy_cache test-cache; proxy_cache_valid 200 4h; proxy_cache_valid any 1m; proxy_pass http://api-backends; } }
  7. # strace -p 10088 Process 10088 attached - interrupt to

    quit epoll_wait(19, {}, 512, 2909) = 0 unlink("/dev/shm/nginx-cache/foo-api/e/69/844b59da41ddaa0d9dd66147e2a0769e") = 0 unlink("/dev/shm/nginx-cache/foo-api/e/ca/219b1560899d1d33c5ab30b21420acae") = 0 nanosleep({0, 200000000}, NULL) = 0 unlink("/data/nginx/cache/foo-image/7/56/a0f7597a0916c5e03422f8f1168ab567") = 0 unlink("/data/nginx/cache/foo-image/2/ae/f9d0d8722d114b7f57748ec28aaa0ae2") = 0 epoll_wait(19, {}, 512, 10000) = 0 nginx cache managerͷಈ͖