実践nginx〜メルカリの場合〜
by
Tatsuhiko Kubo
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
࣮ફnginx ʙϝϧΧϦͷ߹ʙ Tatsuhiko Kubo@cubicdaiya ALM@2015/04/21
Slide 2
Slide 2 text
ࣗݾհ • ٱอୡ(Tatsuhiko Kubo) • bokko@cubicdaiya • Software Engineer in Infrastructure Engineering • Mercari, Inc. • Favorites: Go, C, nginx
Slide 3
Slide 3 text
OSS࡞ͬͨΓίϯτϦϏϡʔτͨ͠Γ
Slide 4
Slide 4 text
Agenda ϝϧΧϦͰͷnginxͷ׆༻ࣄྫʹ͍ͭͯ
Slide 5
Slide 5 text
nginx • ੈքͰೋ൪ʹར༻͞Ε͍ͯΔOSSͷHTTPαʔό • C10Kʹ͑ΒΕΔΞʔΩςΫνϟ • Πϕϯτۦಈ • ϊϯϒϩοΩϯάI/O • ඇಉظI/O • ܰྔͰߴ
Slide 6
Slide 6 text
ϝϧΧϦͰΑ͋͘Δޫܠ ࣮ࡍʹ͋Δఔू͍ͯ͠·͢
Slide 7
Slide 7 text
Agenda • Reverse proxy • SSL termination • SPDY gateway • L7 load balancer
Slide 8
Slide 8 text
Agenda • Reverse proxy • SSL termination • SPDY gateway • L7 load balancer
Slide 9
Slide 9 text
ϦόʔεϓϩΩγͱͯ͠ͷnginx • ϦΫΤετͷϩΪϯά • ΞΫηε੍ޚ • ίϯςϯπͷѹॖɾΩϟογϡ • όοϑΝϦϯά • etc…
Slide 10
Slide 10 text
nginx.confͷઃఆྫ server { listen 443 ssl spdy; server_name xxx.yyy; # ϓϩΩγઃఆ proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Connection ""; proxy_http_version 1.1; proxy_buffers 50 8k; # ੩తίϯςϯπnginxͰฦ͢ location ~ /(styles|js|images)/ { root /usr/share/zabbix; expires 30d; } # Zabbix(༻ͷgoogle_auth_proxy)ϓϩΩγ location / { proxy_pass http://google_auth_proxy_for_zabbix; } }
Slide 11
Slide 11 text
લஈʹnginxΛஔ͘ϝϦοτ • HTTP or HTTPSαʔόʹඞཁͳλεΫΛҰ௨Γ͜ͳͤΔ • ΞϓϦέʔγϣϯαʔό(e.g. Unicorn)ʹΓͳ͍ػೳΛิ • ωοτϫʔΫϨΠςϯγͷվળ • KeepAlive • gzipѹॖ • TLS Session (Cache | Tickets)ɺOCSP Stapling • SPDY • etc…
Slide 12
Slide 12 text
Agenda • Reverse proxy • SSL termination • SPDY gateway • L7 load balancer
Slide 13
Slide 13 text
HTTPS௨৴ͷߴԽ • ҰൠʹHTTPS௨৴ͰTCP 3-way handshakeʹՃ͑ͯ TLS 3-way handshake͕ൃੜ͢ΔͷͰHTTP௨৴ΑΓ͘ͳΔ • HTTPS௨৴ߴԽͷͨΊͷࡾछͷਆث • TLS Session Cache • TLS Session Tickets • OCSP Stapling
Slide 14
Slide 14 text
TLS Session Cache • TLSϋϯυγΣΠΫͷηογϣϯใΛαʔόʹ Ωϟογϡ • nginxͰڞ༗ϝϞϦ্ʹΩϟογϡ͞ΕΔ • ࣍ճͷTLSϋϯυγΣΠΫΛলུ • CPUͷϦιʔεͷݮϨΠςϯγͷղফʹޮՌ͕͋Δ
Slide 15
Slide 15 text
TLS Session Cache with nginx
Slide 16
Slide 16 text
TLS Session Tickets • ҉߸Խͨ͠ηογϣϯใ(νέοτ)ΛΫϥΠΞϯ τʹ͢ • νέοτΛݩʹTLSηογϣϯΛ࠶։ • HTTPSαʔόෳͰηογϣϯใΛڞ༗Ͱ͖Δ • εϚϗͩͱαϙʔτ͍ͯ͠Δ͕গͳ͍…
Slide 17
Slide 17 text
TLS Session Tickets with nginx
Slide 18
Slide 18 text
OCSP Stapling • OCSPʹΑΔSSLূ໌ॻͷࣦޮ֬ೝΛαʔόଆͰߦͬ ͯΩϟογϡ • ΫϥΠΞϯτଆͰΔͱTLSϋϯυγΣΠΫ࣌ʹϨΠ ςϯγ͕ൃੜ͢Δ • ͬͺΓεϚϗͩͱ͋Μ·ΓରԠͯ͠ͳ͍ • Google Chrome for iOSͩͱରԠͯͨ͠
Slide 19
Slide 19 text
OCSP Stapling with nginx ssl_stapling on; ssl_stapling_verify on; ssl_trusted_certificate /etc/nginx/ssl/xxx.yyy.ocsp.crt; resolver xxx.xxx.xxx.xxx valid=30s; resolver_timeout 5s;
Slide 20
Slide 20 text
Agenda • Reverse proxy • SSL termination • SPDY gateway • L7 load balancer
Slide 21
Slide 21 text
SPDY with nginx
Slide 22
Slide 22 text
Ͱɺ࣮ࡍͷޮՌͱݴ͏ͱ
Slide 23
Slide 23 text
TLS Session (Cache|Tickets) ಋೖλΠϛϯά
Slide 24
Slide 24 text
TLS Session (Cache|Tickets) ಋೖλΠϛϯά
Slide 25
Slide 25 text
SPDY 41%:ಋೖͨ͠ͷϐʔΫ
Slide 26
Slide 26 text
͏ͪΐͬͱ۩ମతͳྫ
Slide 27
Slide 27 text
ຊ͔Βւ֎ͷZabbix dashboardʹΞΫηε
Slide 28
Slide 28 text
Client • MacBookPro • Google Chrome • HTTP/2༗ޮ • ϒϥβΩϟογϡৗʹແޮˣ
Slide 29
Slide 29 text
Server(ॳظঢ়ଶ) • Apache(prefork) + mod_php • தZabbix • KeepAlive Off • gzipѹॖແޮ • TLS Session Cache & Tickets༗ޮ
Slide 30
Slide 30 text
Server(ॳظঢ়ଶ) "QBDIF 1PSU HTTPS Server (PPHMF"VUI1SPYZ "QBDIF 1PSU SSLऴ Ϣʔβೝূ Zabbix
Slide 31
Slide 31 text
ύϑΥʔϚϯε(ॳظঢ়ଶ) ໊߲ ܭଌ લճ ͱͷൺֱ ॳظঢ়ଶ ͱͷൺֱ 3FDFJWFESFRVFTUT %BUB5SBOTGFS ,# -PBE5JNF TFD %0.$POUFOU-PBE FE5JNF TFD
Slide 32
Slide 32 text
νϡʔχϯά ͦͷ1 KeepAlive On KeepAliveΛ༗ޮʹ͢Δ
Slide 33
Slide 33 text
ύϑΥʔϚϯε ໊߲ ܭଌ લճ ͱͷൺֱ ॳظঢ়ଶ ͱͷൺֱ 3FDFJWFESFRVFTUT %BUB5SBOTGFS ,# ,# ,# -PBE5JNF TFD TFD TFD %0.$POUFOU-PBE FE5JNF TFD TFD TFD
Slide 34
Slide 34 text
νϡʔχϯά ͦͷ2 AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE text/js AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/json-rpc gzipѹॖΛ༗ޮʹ͢Δ
Slide 35
Slide 35 text
ύϑΥʔϚϯε ໊߲ ܭଌ લճ ͱͷൺֱ ॳظঢ়ଶ ͱͷൺֱ 3FDFJWFESFRVFTUT %BUB5SBOTGFS ,# ,# ,# -PBE5JNF TFD TFD TFD %0.$POUFOU-PBEF E5JNF TFD TFD TFD
Slide 36
Slide 36 text
νϡʔχϯάͦͷ3 "QBDIF 1PSU HTTPS Server (PPHMF"VUI1SPYZ "QBDIF 1PSU SSLऴ Ϣʔβೝূ Zabbix OHJOY 1PSU SSLऴΛnginxͰߦ͏
Slide 37
Slide 37 text
νϡʔχϯάͦͷ3 # nginx.conf # in main context worker_processes auto; tcp_nopush on; keepalive_timeout 65s; open_file_cache max=1000 inactive=20s; ssl_session_cache shared:SSL:30m; gzip on; gzip_comp_level 9; gzip_types text/css text/plain text/js text/javascript application/javascript application/json-rpc; # in event context accept_mutex_delay 100ms; # in event context ੩తϑΝΠϧશ෦nginxͰ৴͢Δ
Slide 38
Slide 38 text
ύϑΥʔϚϯε ໊߲ ܭଌ લճ ͱͷൺֱ ॳظঢ়ଶ ͱͷൺֱ 3FDFJWFESFRVFTUT %BUB5SBOTGFS ,# ,# ,# -PBE5JNF TFD TFD TFD %0.$POUFOU-PBEF E5JNF TFD TFD TFD
Slide 39
Slide 39 text
νϡʔχϯάͦͷ4 listen 443 ssl spdy; SPDY/3.1Λ༗ޮʹ͢Δ
Slide 40
Slide 40 text
ύϑΥʔϚϯε ໊߲ ܭଌ લճ ͱͷൺֱ ॳظঢ়ଶ ͱͷൺֱ 3FDFJWFESFRVFTUT %BUB5SBOTGFS ,# ,# ,# -PBE5JNF TFD TFD TFD %0.$POUFOU-PBEF E5JNF TFD TFD TFD ࠷ऴతʹWebϖʔδͷϩʔυ͕࣌ؒ4ඵ͔Β1ඵʹ
Slide 41
Slide 41 text
Agenda • Reverse proxy • SSL termination • SPDY gateway • L7 load balancer
Slide 42
Slide 42 text
OHJOY
Slide 43
Slide 43 text
൚༻ϓογϡ௨γεςϜ PS CBUDIαʔό
Slide 44
Slide 44 text
·ͱΊ • nginx • ϝϧΧϦͰαʔϏεɺࣾγεςϜͰ͍ΖΜ ͳՕॴʹڬΜͰ׆༻͍ͯ͠·͢ • L7ϩʔυόϥϯαʔɺϦόϓϩɺSSLऴαʔ όͱͯ͠ͱͯ༏ल • ࠓޙར༻Օॴ͕૿͑Δ༧ఆ