ÔõÑùʹÓÃNginx¾ÙÐÐHTTPÇëÇóµÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâ
ÔõÑùʹÓÃnginx¾ÙÐÐhttpÇëÇóµÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâ
СÐò£º
Ëæ×Å»¥ÁªÍøµÄ¿ìËÙÉú³¤£¬Ô½À´Ô½¶àµÄÍøÕ¾ÐèÒª´¦Öóͷ£´ó×ÚµÄHTTPÇëÇó¡£ÔÚÕâÖÖÇéÐÎÏ£¬ÎªÁË°ü¹ÜϵͳµÄÎȹÌÐԺͿÉÀ©Õ¹ÐÔ£¬Ê¹Ó÷´ÏòÊðÀíºÍ¸ºÔØƽºâÊǺÜÊÇÖ÷ÒªµÄ¡£Nginx×÷Ϊһ¿î¸ßÐÔÄܵÄWebЧÀÍÆ÷£¬ÌṩÁËÇ¿Ê¢µÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâ¹¦Ð§£¬±¾ÎĽ«ÏêϸÏÈÈÝÔõÑùʹÓÃNginxÀ´ÊµÏÖHTTPÇëÇóµÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâ¡£
Ò»¡¢Ê²Ã´ÊÇ·´ÏòÊðÀíºÍ¸ºÔØƽºâ
·´ÏòÊðÀí
·´ÏòÊðÀíÊÇ¿Í»§¶Ë²»Ö±½ÓÓëЧÀÍÆ÷ͨѶ£¬¶øÊÇÓë·´ÏòÊðÀíЧÀÍÆ÷ͨѶ£¬È»ºó·´ÏòÊðÀíЧÀÍÆ÷½«ÇëÇóת·¢µ½ºó¶ËµÄÕæʵЧÀÍÆ÷¾ÙÐд¦Öóͷ££¬²¢½«Ð§¹û·µ»Ø¸ø¿Í»§¶Ë¡£·´ÏòÊðÀíÒþ²ØÁ˺ó¶ËЧÀÍÆ÷µÄϸ½Ú£¬¿ÉÒÔÌá¸ßϵͳµÄÇå¾²ÐԺͿÉÀ©Õ¹ÐÔ¡£
¸ºÔØƽºâ
¸ºÔØƽºâÊÇδÀ´×Ô¿Í»§¶ËµÄÇëÇóÔȳƵطַ¢µ½¶ą̀ЧÀÍÆ÷ÉÏ£¬ÒÔµÖ´ï¸üºÃµÄ×ÊԴʹÓúÍЧÀÍÏìÓ¦ËÙÂʵÄÄ¿µÄ¡£¸ºÔØƽºâ¿ÉÒÔÌá¸ßϵͳµÄ¿É¿¿ÐÔºÍÐÔÄÜ¡£
¶þ¡¢Ê¹ÓÃNginx¾ÙÐз´ÏòÊðÀí
×°ÖÃNginx
Ê×ÏÈ£¬ÐèҪװÖÃNginx¡£ÒÔUbuntuΪÀý£¬Ö´ÐÐÒÔÏÂÏÂÁî¾ÙÐÐ×°Öãº
sudo apt-get update sudo apt-get install nginx
µÇ¼ºó¸´ÖÆ
ÉèÖ÷´ÏòÊðÀí
ÔÚNginxµÄÉèÖÃÎļþ£¨Ò»Ñùƽ³£Îª /etc/nginx/nginx.conf »ò /etc/nginx/conf.d/default.conf£©ÖÐÌí¼ÓÒÔÏÂÉèÖãº
http { server { listen 80; server_name yourdomain.com; location / { proxy_pass http://backend-server; } } upstream backend-server { server backend1 ip:port; server backend2 ip:port; server backend3 ip:port; # ¿ÉÒÔƾ֤ÐèÇóÉèÖøü¶àµÄºó¶ËЧÀÍÆ÷ } }
µÇ¼ºó¸´ÖÆ
ÕâÀïͨ¹ý location / Ö¸¶¨Á˵±ÓÐÇëÇó»á¼û / ·¾¶Ê±£¬½«ÇëÇóת·¢µ½ backend-server ÖÐÉèÖõĺó¶ËЧÀÍÆ÷ÉÏ¡£
ÖØÆôNginx
ÉèÖÃÍê±Ïºó£¬Ö´ÐÐÒÔÏÂÏÂÁîÖØмÓÔØNginxµÄÉèÖÃÎļþ£º
sudo systemctl restart nginx
µÇ¼ºó¸´ÖÆ
ÖÁ´Ë£¬NginxµÄ·´ÏòÊðÀíÉèÖÃÍê³É¡£
Èý¡¢Ê¹ÓÃNginx¾ÙÐиºÔØƽºâ
ÉèÖøºÔØƽºâ
ÔÚNginxµÄÉèÖÃÎļþÖÐµÄ upstream backend-server ²¿·Ö£¬¿ÉÒÔÉèÖöà¸öºó¶ËЧÀÍÆ÷£¬Nginx»á×Ô¶¯½«ÇëÇóƽºâ·Ö·¢µ½ÕâЩЧÀÍÆ÷ÉÏ¡£¿ÉÒÔ½ÓÄɲî±ðµÄ¸ºÔØƽºâÕ½ÂÔ£¬ÀýÈçÂÖѯ¡¢IP¹þÏ£µÈ¡£ÒÔÏÂÊÇÒ»¸öÂÖѯµÄʾÀýÉèÖãº
http { upstream backend-server { server backend1 ip:port; server backend2 ip:port; server backend3 ip:port; # ¿ÉÒÔƾ֤ÐèÇóÉèÖøü¶àµÄºó¶ËЧÀÍÆ÷ # ĬÈϽÓÄÉÂÖѯսÂÔ } }
µÇ¼ºó¸´ÖÆ
²âÊÔ¸ºÔØƽºâ
ÉèÖÃÍê³Éºó£¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî²âÊÔ¸ºÔØƽºâµÄЧ¹û£º
for i in {1..10}; do curl yourdomain.com; done
µÇ¼ºó¸´ÖÆ
ÕâÀïʹÓÃÁË curl ÏÂÁîÀ´Ä£Äâ·¢ËÍ10´ÎÇëÇóµ½ yourdomain.com£¬¿ÉÒÔÊӲ쵽ÇëÇó»á±»ÔȳƷַ¢µ½ºó¶ËµÄ¶ą̀ЧÀÍÆ÷ÉÏ¡£
¿¢ÊÂÓ
ͨ¹ýÉÏÊö°ì·¨£¬ÎÒÃÇѧ»áÁËÔõÑùʹÓÃnginx¾ÙÐÐhttpÇëÇóµÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâ¡£·´ÏòÊðÀíºÍ¸ºÔØƽºâÊǹ¹½¨¸ßÐÔÄÜ¡¢¸ß¿ÉÓÃÐÔϵͳµÄÖ÷Òª×é³É²¿·Ö£¬ÔÚÏÖʵӦÓÃÖÐÓÐ×ÅÆÕ±éµÄÓ¦Óá£Ï£Íû±¾ÎÄÄܶԶÁÕßÃ÷È·ºÍʹÓÃNginxÌṩµÄÕâÁ½ÏЧÌṩ×ÊÖú¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃNginx¾ÙÐÐHTTPÇëÇóµÄ·´ÏòÊðÀíºÍ¸ºÔØƽºâµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡