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

FUJIMIのFastly活用事例集

 FUJIMIのFastly活用事例集

Fastly User Meetup #4 の登壇資料です

https://fastly-community.connpass.com/event/241481

E654ce93d23159c059fa5cf1b1347bfa?s=128

Kono Junya

March 29, 2022
Tweet

More Decks by Kono Junya

Other Decks in Technology

Transcript

  1. '6+*.*ͷ'BTUMZ׆༻ࣄྫू

  2. ++ -FBE8FCBOE&EHF&OHJOFFS 5XJUUFS!LPOPKVOZB (JU)VC!LPOPKVOZB [FOOEFW!KK ࣗݾ঺հ

  3. #VTJOFTTʷ'6+*.*

  4. '6+*.*͸໿໰ͷ8&#ඒ༰෼ੳ͔Β ύʔιφϥΠζ͢ΔϏϡʔςΟʔϒϥϯυͰ͢ɻ '6+*.*ͷࣄۀ঺հ ถࠃISNFαϓϦϝϯτΞυόΠβʔࢿ֨Λ࣋ͪɺ ػೳੑදࣔ৯඼ݕఆڠձͷཧࣄΛ຿ΊΔαϓϦϝϯτͷઐ໳ՈΛ ΞυόΠβʔͱܴͯ͑͠ɺFUJIMIಠࣗͷඒ༰෼ੳΛ։ൃɺ࣮ݱɻ 2019೥3݄ͷαϓϦϝϯτϦϦʔεޙɺ໿40ສਓͷ෼ੳ৘ใΛ΋ͱʹ 2020೥3݄ʹඒ༰෼ੳΛΞοϓσʔτɻ

  5. '6+*.*͸໿໰ͷ8&#ඒ༰෼ੳ͔Β ύʔιφϥΠζ͢ΔϏϡʔςΟʔϒϥϯυͰ͢ɻ '6+*.*ͷࣄۀ঺հ

  6. 5FDIOPMPHZʷ'6+*.*

  7.  1SPHSFTTJWF3FMFBTF  -1ͷϦμΠϨΫτXJUI$!&  1SFWJFX؀ڥ "HFOEB

  8. 1SPHSFTTJWF3FMFBTF

  9. 'BTUMZʷ'6+*.* #SPXTFS

  10. 'BTUMZʷ'6+*.* if(req.url.path ~ “^/xxx”) { set req.backend = Origin2; }

    YYY ʹର͢ΔϦΫΤετʹରͯ͠$MPVE3VOΛPSJHJOͱ͢Δ
  11. 'BTUMZʷ'6+*.* ஈ֊తʹҠߦ͢ΔͨΊʹ7$-Λੜ੒͢Δ ʷ

  12. 'BTUMZʷ'6+*.* #SPXTFS $VTUPN7$- ஈ֊తʹҠߦ

  13. 'BTUMZʷ'6+*.* *NBHF0QUJNJ[FS τοϓϖʔδͰݕূ -B[ZMPBE༗Γͷϖʔδ'7ಡΈࠐΈ࣌఺ͰɺL#ͷ࡟ݮ -BSHFTU$POUFOUGVM1BJOU͸TͷύϑΥʔϚϯε޲্ if(req.url.ext ~ “(?i)^(gif|png|jpe?g)$”) { set

    req.url = req.url.path “?auto=webp”; set req.http.Cache-Control = “public, max-age=…”; set req.http.Surrogate-Key = “image”; }
  14. 'BTUMZʷ'6+*.* *NBHF0QUJNJ[FS if(req.url.ext ~ “(?i)^(gif|png|jpe?g)$”) { if(req.http.Save-Data) { set req.url

    = req.url.path “?quality=440”; } } 'BTUMZ*NBHF0QUJNJ[FSΛ࢖͏ར఺  ($1ͳͲʹରͯ͠BTTFUTΛEFQMPZͤͣʹ͢Ή  ্هͷΑ͏ʹIUUQGJFMEͰ෼ذΛ͢Δ͜ͱ͕Ͱ͖Δ  4VSSPHBUF,FZʹΑΔDBDIFͷΠϯελϯτύʔδ͕ߦ͑Δ
  15. -1ͷϦμΠϨΫτXJUI$!&

  16. $PNQVUF!&EHFʷ'6+*.* IUUQTQSGVKJNJNFYYY ޿ࠂ༻ͷϥϯσΟϯάϖʔδ GVKJNJNFͰ഑৴͍ͨ͠ʜ ·͔͢͡🤔 ϦμΠϨΫτͲ͏͠Α͏͔ͳʜ -1ͷϦμΠϨΫτXJUI$!&

  17. $PNQVUF!&EHFʷ'6+*.* -1ͷϦμΠϨΫτXJUI$!& GET /pt101 HTTP/2 Host: pr.fujimi.me HTTP/2 308 Location:

    https://fujimi.me/lp/protein 3FRVFTU 3FTQPOTF let path = req.get_path(); let path = match path { path if path.contains(“pt”) => “/protein”, _ => path, }; Ok(Response::from_status(StatusCode::PERMANENT_REDIRECT).with_header( header::LOCATION, format!(“https://fujimi.me/lp{}”, path), )) src/main.rs
  18. 1SFWJFX؀ڥ

  19. $PNQVUF!&EHFʷ'6+*.* 1SFWJFX؀ڥ a͜ͷλεΫΑΖͽ aঝ஌ʂʂ 1. %FW a֬ೝ͠·͢ʂʂ aͰ͖ͨʂ 1. %FW

    8SJLFΛ࢖ͬͯ λεΫΛνέοτ؅ཧ νέοτͱ13͕΄΅
  20. $PNQVUF!&EHFʷ'6+*.* 1SFWJFX؀ڥ a֬ೝ͠·͢ʂʂ aͰ͖ͨʂ 1. %FW 1VMM3FRVFTUΛϚʔδ 1.͕֬ೝ͢ΔͨΊʹ 13ΛNBJOCSBODIʹϚʔδ͠ͳ͍ͱ͍͚ͳ͍ʜ ໰୊఺

     1.͕ϨϏϡʔͯ͠μϝͩͬͨ৔߹ɺSFWFSUPSमਖ਼13Λ͋͛ͳ͍ͱ͍͚ͳ͍  ະ׬੒ͳ΋ͷΛNBJOCSBODI΁Ϛʔδ͠ͳ͚Ε͹ͳΒͳ͍
  21. $PNQVUF!&EHFʷ'6+*.* 1SFWJFX؀ڥ ֤1VMM3FRVFTUʹԠͯ͡αʔόʔ্ཱ͕͕ͪΕ͹ NBJOCSBODIʹϚʔδ͢Δඞཁ͕ͳ͍ʂʂʂ a1SFWJFX؀ڥര஀

  22. $PNQVUF!&EHFʷ'6+*.* 1SFWJFX؀ڥ QSQSFWJFXFYBNQMFDPN QSEFWYYYDMPVEBSVOBQQ

  23. $PNQVUF!&EHFʷ'6+*.* 1SFWJFX؀ڥ if host == “preview.example.com" { return response_preview_list_app(); }

    src/main.rs
  24. $PNQVUF!&EHFʷ'6+*.* 1SFWJFX؀ڥ ஫ҙ఺  1VSHF63-͸ଘࡏ͠ͳ͍  QSFYBNQMFDPNͷΑ͏ʹಛఆͷ)PTU͸ࢦఆͰ͖ͳ͍ 4VSSPHBUF,FZΛઃఆͯ͠ύʔδͰ͖ΔΑ͏ʹ͢Δʂ

  25. $BTF4UVEZʹܝࡌ͞Ε·ͨ͠🎉 ࠓ೔ͷ͓࿩ͷҰ෦ΛΑΓৄ͓͘͠࿩͍ͯ͠ΔͷΛ $BTF4UVEZʹܝࡌ͍͍ͯͨͩͨ͠ͷͰੋඇΈ͍ͯͩ͘͞ʂ ˜'BTUMZ *OD"MM3JHIUT3FTFSWFE 'BTUMZ ͷ $%/ͱ$PNQVUF!&EHFͰ '6+*.*ެࣜαΠτΛڧԽ ςϨϏ$.Ͱ໿ഒʹ૿͑ͨΞΫηεʹ

    ઃఆมߋͳ͠ͰରԠ ඒ༰෼ੳ͔Βͭ͘Δ͋ͳͨͷͨΊͷύʔιφϥΠζϏϡʔςΟϒϥϯυͰ͋Δ'6+*.*Λ த৺ʹࣄۀΛల։͢ΔτϦίגࣜձࣾïҎԼɺτϦίðɻެࣜαΠτͰ͋ΔGVKJNJNFʹɺ 'BTUMZͷ$%/ɺΠϝʔδΦϓςΟϚΠβʔɺ͓Αͼ$PNQVUF!&EHFΛ࠾༻ɻ Ωϟογϡͷώοτ཰ˋҎ্ɺΩϟογϡΧόϨοδˋΛ࣮ݱ͢Δͱͱ΋ʹɺ ςϨϏ $.ͷެ։ʹΑΔഒҎ্ͷΞΫηεͷ֦େʹ΋αʔόʔ؀ڥͷઃఆΛมߋ͢Δ͜ͱͳ͘ ରԠͰ͖Δ࢓૊ΈΛ࣮ݱͨ͠ɻ 'BTUMZ τϦίגࣜձࣾ $"4&456%: ໊ࣾāτϦίגࣜձࣾ ॅॴā˟ ɹɹɹ ౦ژ౎৽॓۠৽॓େڮޚԓϏϧ' 63-ɿIUUQTUSJDPUJODDPN ೥݄ʹઃཱɻඒ༰෼ੳʹΑΓύʔιφϥΠζ͞Εͨα ϓϦϝϯτɺϓϩςΠϯɺϑΣΠεϚεΫΛఏڙ͢ΔϏϡʔ ςΟϒϥϯυʮ'6+*.*ʯͷࣄۀΛల։ɻ ೥݄ʹ͸ɺ ϧϛω৽॓ళʹॳͷৗઃళΛΦʔϓϯͨ͠ɻ ໊ࣾʹ͸ɺ͓٬༷ʹѪ͞ΕΔϓϩμΫτΛ૑Γɺເதʹͳͬ ͯ΄͍͠ͱ͍͏ʮྂʯͱɺ͓٬༷ͷ੠ΛͭͻͱͭஸೡʹฤΈ ্͛ɺਓʹدΓఴͬͨϓϩμΫτΛ૑Δͱ͍͏ϑϥϯεޠͷ ʮUSJDPUʢฤΈ෺ʣʯ ͷͭͷ૝͍͕ࠐΊΒΕ͍ͯΔɻ w $%/ ಋೖͱ 3JPUKT ͔Β /FYUKT ΁ͷҠߦͷ ͭΛ໨తʹ 'BTUMZ Λ࠾༻ ɹʮࢲΒ͍͠ඒ͠͞ͰɺࢲΛ΋ͬͱ޷͖ʹͳΔ⼧ͱ͍͏ϛογϣϯʹج͖ͮӡӦ͢Δ '6+*.* GVKJNJNF ͸Ŋ໿ʙ໰ͷඒ༰ʹؔ͢Δઃ໰ʹ౴͑Δ͜ͱͰɺͦͷ෼ੳ ݁Ռ͔ΒύʔιφϥΠζ͞ΕͨΦϦδφϧͷαϓϦϝϯτ΍ϑΣΠεϚεΫɺϓ ϩςΠϯΛߪೖͰ͖ΔެࣜαΠτͰ͋Δɻओʹʙ୅ͷঁੑʹࢧ࣋͞ΕɺΞΫ ςΟϒϢʔβʔ਺͸ສϢʔβʔҎ্ɻ௚ۙΧ݄ͷϖʔδϏϡʔï 17 ðͷฏۉ ͸ɺສ17Λ௒͍͑ͯΔɻ ɹ౰ॳɺGVKJNFNF Ͱ͸ίϯςϯπ഑৴ωοτϫʔΫ $%/ ͕ಋೖ͞Ε͍ͯͳ͔ͬ ͨɻ͔͠͠ɺ೥݄ʹλϨϯτΛى༻ͨ͠ςϨϏ$. ͷެ։͕ܾ·ͬͨ͜ͱ͔ Βɺ$%/ͳ͠ͷ··ςϨϏ$.Λެ։͢Δ͜ͱ͸ɺαʔόʔͷෛՙ૿େʹΑΔγες ϜఀࢭͷϦεΫ͕ߴ͔ͬͨɻ·ͨɺςϨϏ$.ͷެ։௚લͷ೥݄ʹ৽੡඼Ͱ͋ ΔϓϩςΠϯΛϦϦʔε͍͕ͯͨ͠ɺٕज़తͳ໰୊͔Β$%/ͷಋೖ͕ඞཁͩͬͨɻ ɹ-FBE 8FC &OHJOFFS ͷՏ໺७໵ࢯ͸ɺ⼦αϓϦϝϯτͱϑΣΠεϚεΫͷΞϓϦ έʔγϣϯ͸ɺ3JPUKT ͱ͍͏গ͠ݹ͍ +BWB4DSJQU ͷϑϨʔϜϫʔΫͰ࣮૷͞Ε ͍ͯͨͷͰ͕͢ɺ ॊೈͳઃܭʹͳ͍ͬͯͳ͍ͨΊɺͦ͜ʹϓϩςΠϯͷ৽نϓϥο τϑΥʔϜΛ௥Ճ͢Δͷ͸։ൃମݧ͕Α͘ͳ͍ͱߟ͑·ͨ͠ɻͦ͜Ͱ /FYUKT ͱ 5ZQF4DSJQUʹΑΔΞϓϦέʔγϣϯ։ൃʹҠߦ͍ͨ͠ͱࢥ͍ͬͯ·ͨ͠ʯͱ࿩͢ɻ ɹαϓϦϝϯτͱϑΣΠεϚεΫͷΞϓϦέʔγϣϯΛ 3JPUKT ͔Β /FYUKTʹҰ ؾʹॻ͖׵͑Δͱ։ൃͱ2" ඼࣭อূ ʹ޻਺͕͔͔ͬͯ͠·͏ɻ ͦ͜Ͱɺผͷαʔ όʔͰϓϩςΠϯͷΞϓϦέʔγϣϯΛ։ൃ͠ͳ͕ΒɺGVKJNJNF͔ΒΞΫηεͰ ͖ΔΑ͏ʹ͢Δ͜ͱ͕ඞཁͩͬͨɻՏ໺ࢯ͸ɺ ⼦'BTUMZ Ͱ͋Ε͹ɺ7$- 7BSOJTI $POpHVSBUJPO -BOHVBHF Λ࢖͏͜ͱͰɺ ͜ΕΛ࣮ݱͰ͖Δͱߟ͑·ͨ͠ʯ ͱ࿩͢ɻ ɹݕ౼ͷ݁Ռɺ೥݄ʹ 'BTUMZ ͷ࠾༻Λܾఆɻ࠾༻ͷཧ༝ΛՏ໺ࢯ͸ɺʮલ ৬Ͱ 'BTUMZ Λ࢖ͬͨ͜ͱ͕͋ΔͷͰ͕͢ɺ'BTUMZ ͷڧΈͰ͋Δ 7$- Λར༻ͨ͠ ॊೈͳઃܭ͕Մೳͳ͜ͱΛධՁ͠·ͨ͠ɻ·ͨɺ৽͍͠ΞϓϦέʔγϣϯΛϦϦʔε ͨ͠ͱ͖ʹɺΩϟογϡΛύʔδ͠ͳ͚Ε͹ͳΒͳ͍ͷͰ͕͢ɺ'BTUMZ Ͱ͋Ε͹Π ϯελϯτύʔδͰߴ଎ʹΩϟογϡΛύʔδͰ͖Δ͜ͱ΋࠾༻ΛܾΊͨཧ༝ͷͭ Ͱͨ͠ʯͱ࿩͍ͯ͠Δɻ
  26. 8FBSFIJSJOH aશ৬छืूதͰ͢ʂ IUUQTIFSQDBSFFSTWUSJDPU

  27. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠