Reverse Proxy ããªãå¿ è¦ã
フロントエンジニアに知ってもらいたいリバースプロキシの重要性 | RickyNews ãã®è¨äºãç®ã«å ¥ã£ã¦èªãã§ã¿ãããªãã»ã©ãæ¨ä»ã¯ Reverse Proxy ã¯ä¾¿å©ãª L7 ã«ã¼ã¿ã¼çãªãã®ã¨ãã¦èªèããã¦ããã®ã ãªãã¨æã£ããURL ã® Rewrite ããVirtualHost äºã ã確ãã« Reverse Proxy ã®ä¾¿å©ãªå´é¢ã§ã¯ããä¸æ¹ãããã㯠Nginx ãªã©ã® Reverse Proxy ã§ãªããã°å®è£ ãä¸å¯è½ãã¨è¨ãã°ãããªãã¨ã¯ãªããã®ã§ãããã
èªå㯠Reverse Proxy ã¯ããããããµã¼ãã¼/ã¤ã³ãã©çãªå´é¢ã§ãã®å½¹å²ãæãã¦ãããä»æ´ä½ãã¨ãããã®ã§ãããããæ¼ä¼ã¿ãã¦ãæéãããã®ã§ç°¡åã«æ¸ãã¦ã¿ããã
Reverse Proxy ã¯Webã·ã¹ãã å ¨ä½ã®ãªã½ã¼ã¹æé©åã®ããã®ãã¼ã
Reverse Proxy ã®ã¤ã³ãã©çãªè¦ç¹ã§ã®å½¹å²ã¯ãWebã·ã¹ãã å ¨ä½ã®ç³»ã«ããããªã½ã¼ã¹å ¨ä½æé©ã®ããã®é¨åãã§ããããªãã®ãã£ã¡ã・・・ããããç³æããªãã§ã
æ¹ã㦠Reverse Proxy ã¯
- Reverse Proxy ãããã³ãã¨ã³ã
- Rails ã§ã Plack ã§ãä½ã§ãããã®ã ããã®ã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¼ãããã¯ã¨ã³ã
ã¨ããã¨ãã«
- ããã³ãã¨ã³ãã§è£ãããã®ã¯ããã³ãã¨ã³ãã§
- ããã¯ã¨ã³ãã§ãªããã°å¦çã§ããªããã®ã ãããã¯ã¨ã³ãã§å¦çãã
ããã«ããå ¨ä½ã®ãªã½ã¼ã¹å¹çãä¸ãã¾ããããã¨ããããã®ãã®ã§ããã
ãã«ãããã»ã¹ã¢ãã«ã¨ä¸¦è¡å¦çæ§è½
æè¿ã¯ãããããªå®è£ ãããã®ã§ä¸æ¦ã«ã¯è¨ããªãã®ã ããWebã¢ããªã±ã¼ã·ã§ã³ã«ããããã¯ã¨ã³ãã®ã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¼ã§ã¯ãç¹ã«ã¹ã¯ãªããè¨èªã«ããã¦ã¯ãããã¯ãã«ãããã»ã¹ã¢ãã«ãæ¡ç¨ãã¦ãããã¨ãã»ã¨ãã©ã ããã«ãããã»ã¹ã¢ãã«ã§ã¯ã1ãªã¯ã¨ã¹ãã1ããã»ã¹ãæ å½ãããã¨ã«ãªããå¾ã£ã¦ã1ã·ã¹ãã ã§20ããã»ã¹ãèµ·åãã¦ããã¨ããã¨åæã«å¦çã§ãã並è¡å¦çæ°ã¯æ大ã§20ãªã¯ã¨ã¹ãã¨ãããã¨ã«ãªãã
ãåç¥ã®ããã«ã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¼ã¯ã¡ã¢ãªã大éã«æ¶è²»ãããã¡ã¢ãªä½¿ç¨éã¨ããæèã§ãéããã¨è¨ããããã®ããã32GB ã¨ãã©ããªã«æ½¤æ²¢ã«ã¡ã¢ãªã確ä¿ã§ããç¶æ³ã§ããããããæ大ããã»ã¹æ°ã¯ãããªãã¡ä¸¦è¡å¦çæ§è½ã¯ 50 ã¨ã 100 ã¨ããã®ãããã§ããã
Web ã·ã¹ãã ãå¿çããªããã°ãããªãã®ã¯ãä¾ãã° /login ã«æ¥ã1ãªã¯ã¨ã¹ãã«å¯¾ãã¦ãã®ç»é¢ãå«ãç»åãJavaScriptãCSS ãã®ä»å¤§éã®ã¢ã»ãããå«ããReverse Proxy ãå°å ¥ãã¦ããªãã·ã¹ãã ã§ã¯ãããã®ãã¹ã¦ã«ã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¼ãå¿çããªããã°ãªããªãããããåé¡ãæ¬æ¥ã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¼ã¯ãåçãªãªã¯ã¨ã¹ããå¦çããããã«ç¨æããã¦ãããã®ã§ããã«ãé¢ããããåçã§ãªãã³ã³ãã³ãã«ã¾ã§å¿çããªããã°ãªããªã・・・ã¨ããã®ã§ãå ã«ã¿ãããã«è²´éãªã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¼ããã»ã¹ããã©ãã§ãããéçãã¡ã¤ã«ã®ãªã¯ã¨ã¹ããªã©ã«ãæ¶è²»ããã¦ãã¾ããã®ã§ããã
Reverse Proxy ã§å¾æãªãã®ã¯å¾æãªäººãå¿çããããã«ãã
ãã㧠Reverse Proxy ãå ¥ãã¦ã(ãããã¯ã¼ã¯çã«) ã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¼ã®æåã«é ç½®ãããããã¦éçãã¡ã¤ã«ãªã©ãã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¼ãä»ããã«å¿çã§ãããã®ã¯ Reverse Proxy ãç´æ¥ã¯ã©ã¤ã¢ã³ãã«å¿çããã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¼ã§ãªããã°å¿çã§ããªããªã¯ã¨ã¹ãã®ã¿ã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¼ã«è»¢éããã
Reverse Proxy ã¯ã(ã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¼ããã«ãããã»ã¹ã¢ãã«ã§ããã®ã¨å¯¾ç §çã«) æ¨ä»ã®å ´å Nginx ã®ããã«ã·ã³ã°ã«ããã»ã¹ã»ã¤ãã³ãããªãã³ãªã¢ã¼ããã¯ãã£ã®å®è£ ã®ãã®ãç¨ããããã®ã¢ã¼ããã¯ãã£ã¯ãã©ããªã«å¤§éã®ãªã¯ã¨ã¹ããæ¥ã¦ãããã»ã¹æ°ã¯å¢ãããã«ã³ã³ããã¹ãã¹ã¤ããã³ã°ã®ã¿ (CPU çãªæå³ã®ã³ã³ããã¹ãã¹ã¤ããã³ã°ã§ã¯ãªããã¤ãã³ãã®æå³ã§ã®ãå ·ä½çã«ã¯ select ã epoll ã«é¡ããã·ã¹ãã ã³ã¼ã«) ã§ä¸¦è¡å¦çæ§è½ã稼ãã¨ãããå©ç¹ããã®ããã¡ã¢ãªä½¿ç¨éã®æèã§ã¯ã軽ããã
ãã®ãéããã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¼ã¨ã軽ããReverse Proxy ãçµã¿åããã¦ããããèªåãå¾æãªãã®ã ãæ å½ãããã¨ã§ãã·ã¹ãã å ¨ä½ã®ç³»ã§ã¿ãã¨ãã«ãªã½ã¼ã¹å¹çãå ¨ä½æé©ããã¾ããã・・・ã¨ããã®ãã¤ã³ãã©è¦ç¹ã§ Reverse Proxy ãå°å ¥ãããä¸çªã®çç±ã§ããã
Reverse Proxy ã¯ãããã¯ã¼ã¯çã«é ãã¯ã©ã¤ã¢ã³ãããKeepAlive ãªã¯ã¨ã¹ãããããã¯å¤§ããªãã¡ã¤ã«ãã¢ãããã¼ããã¦ããã¯ã©ã¤ã¢ã³ããªã©ããã¾ãä»äºã¯ããªããã©ãããã¯ã¼ã¯æ¥ç¶ã®ç¶æã¯è¦æ±ããããããããããªå´é¢ã§ãå½¹ã«ç«ã¤ãããã㯠Reverse Proxy å´ã§å¦çãã¦ãããã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¼ã«ã¯æ¬å½ã«å¿ è¦ãªã¨ãã ãã«ãããªã¯ã¨ã¹ãã転éããªã・・・ ãã¨ãã°é ãã¯ã©ã¤ã¢ã³ãã®HTTPãªã¯ã¨ã¹ãããã¹ã¦çä¿¡ãã¦ãã転éãKeepAlive ã¯ããã³ãã ãæå¹ã«ãã¦ããããã¯ã¨ã³ãã¯å¿çãçµãã£ããããåæãã¢ãããã¼ããã¡ã¤ã«ã¯çµããã¾ã§ããã³ãå´ã§ãããã¡ãªã³ã°ï½¥ï½¥ï½¥ãªã©ã§ããã
ãã®å½¹å²åæ ããæ´ã«å¾æãªãã®ã¯å¾æãªã¨ããã§ãã¨ãããªã½ã¼ã¹å ¨ä½æé©åã«å¯ä¸ããã
åæ¹å±±
ãã®è¾ºã Reverse Proxy ã«æ±ããããå½¹å²ã®ä¸»è»¸ã§ãL7 çãªä¾¿å©æ©è½ï½¥ï½¥ï½¥ URL ã® Rewrite ã¨ãããã³ãã¨ã³ãå´ã§ã®ã¢ã¯ã»ã¹å¶å¾¡ã¨ããã®ã¯ããã¾ã§å¯æ¬¡çãªè¦ç´ ã«éããªã・・・ã¨ããã®ãã¤ã³ãã©è¦ç¹ã§ã®èªèã§ã¯ãããã¾ããããã³ãã¨ã³ãã¨ã³ã¸ãã¢ãªã³ã°ããããããã®è¦ç¹ãé転ãããã¨ããã®ã¯ããããªãããªãã
ã¡ãªã¿ã«æè¿ã¯ Amazon ã® CloudFront ãªã© CDN ãæ¯è¼çã«ã¸ã¥ã¢ã«ã«ä½¿ããããã«ãªã£ãã®ã§ãéçãã¡ã¤ã«ã®é ä¿¡ã«é¢ãã¦ã¯ CDN ã ãã§è³ã£ã¦ãã¾ãã¨ããåæ¥ã使ãä¾ãå¢ãã¦ãæ°ããããå人çã«ã¯ããã®å¯æ¬¡çãªç¨éã®é åã大ããã®ã§ãCDN ã«ä½¿ãã«ãã¦ã Rerverse Proxy ãã£ãã»ãããããã¨ã¯æããã©ã
・・・æ¸ãã¦ããã¡ã«ã ãã ããã¤é¡ã«ãªã£ã¦ãã¦ãã¾ã£ãããã¿ã¾ãããèªåã®ç¥èãããç¨åº¦æ£ããã¨ããæãè¾¼ã¿ãåæã«ããã¡ãã¨ãã®è¾ºã®èæ¯äºæ ãç¶ãä¸ä»£ã«ç¶æ¿ãã¦ããããã¨ãæãã¨ããã§ãã
ã¡ãªã¿ã«ããã®è¾ºã¯ããµã¼ãã¼/ã¤ã³ãã©ãæ¯ããæè¡ãã¨ãã(ã¨ã£ã¦ã)ç´ æ´ãããæ¬ã«è©³ããæ¸ããã¦ããã®ã§ãã¡ããèªãã¨è¯ãã¨æãã