Nginx¶àЧÀÍÆ÷¼¯ÈºÉèÖã¬ÔöÌíÍøÕ¾¿ÉÓÃÐÔ
nginx¶àЧÀÍÆ÷¼¯ÈºÉèÖã¬ÔöÌíÍøÕ¾¿ÉÓÃÐÔ
ÔÚÏÖ´ú»¥ÁªÍøÓ¦ÓÃÖУ¬°ü¹ÜÍøÕ¾µÄ¸ß¿ÉÓÃÐÔÊÇÖÁ¹ØÖ÷ÒªµÄ¡£Ò»¸ö³£ÓõÄÒªÁìÊÇͨ¹ýÉèÖöą̀ЧÀÍÆ÷µÄ¼¯ÈºÀ´·Öµ£¸ºÔØ£¬ÒÔÌá¸ßÍøÕ¾µÄ¿ÉÓÃÐÔºÍÐÔÄÜ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃNginxÀ´ÉèÖöą̀ЧÀÍÆ÷µÄ¼¯Èº£¬ÊµÏÖ¸ºÔØƽºâºÍ¹ÊÕÏתÒÆ¡£
×°ÖúÍÉèÖÃNginx
Ê×ÏÈ£¬ÐèÒªÔÚËùÓÐЧÀÍÆ÷ÉÏ×°ÖÃNginx¡£¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´×°Öãº
sudo apt-get update sudo apt-get install nginx
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³Éºó£¬ÐèÒª¶ÔNginx¾ÙÐлù±¾ÉèÖ᣷¿ªNginxµÄÖ÷ÉèÖÃÎļþnginx.conf£¬Ò»Ñùƽ³£Î»ÓÚ/etc/nginx/nginx.conf£¬Ê¹ÓÃÎı¾±à¼Æ÷¾ÙÐб༡£Ö÷ÒªµÄÉèÖÃÏîÈçÏ£º
http { upstream backend { server server1.example.com; server server2.example.com; server server3.example.com; } server { listen 80; server_name example.com; location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöÉèÖÃÖУ¬upstream backend½ç˵Á˺ó¶ËЧÀÍÆ÷µÄÁÐ±í¡£Äã¿ÉÒÔƾ֤ÏÖÕæÏàÐν«server1.example.comºÍserver2.example.comµÈÌ滻ΪÕæʵµÄЧÀÍÆ÷ÓòÃû»òIPµØµã¡£ÔÚserver¿éÖУ¬½«ÇëÇóͨ¹ýproxy_passÖ¸Áîת·¢µ½backendºó¶ËЧÀÍÆ÷¼¯Èº¡£
ÉèÖøºÔØƽºâ
ΪÁËʵÏÖ¸ºÔØƽºâ£¬NginxÌṩÁ˶àÖÖ¸ºÔØƽºâËã·¨¡£³£¼ûµÄÓÐÂÖѯ£¨Ä¬ÈÏ£©¡¢¼ÓȨÂÖѯ¡¢IP¹þÏ£µÈ¡£ÎÒÃÇ¿ÉÒÔÔÚupstream backend¿éÖÐͨ¹ýÌí¼Ó²î±ðµÄweightÊôÐÔÀ´ÊµÏÖ²î±ðµÄ¸ºÔØƽºâÕ½ÂÔ¡£ÒÔÏÂÊǼ¸ÖÖ³£¼ûµÄ¸ºÔØƽºâÕ½ÂÔʾÀý£º
ÂÖѯ£¨Ä¬ÈÏ£©£º
upstream backend { server server1.example.com; server server2.example.com; server server3.example.com; }
µÇ¼ºó¸´ÖÆ
¼ÓȨÂÖѯ£º
upstream backend { server server1.example.com weight=3; server server2.example.com weight=2; server server3.example.com; }
µÇ¼ºó¸´ÖÆ
IP¹þÏ££º
upstream backend { ip_hash; server server1.example.com; server server2.example.com; server server3.example.com; }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏʾÀý»®·Ö½ç˵ÁËÂÖѯ¡¢¼ÓȨÂÖѯºÍIP¹þÏ£ÈýÖÖ¸ºÔØƽºâÕ½ÂÔ¡£Äã¿ÉÒÔƾ֤ÏÖʵÐèÇóÑ¡ÔñÊʺϵÄÕ½ÂÔ¡£
ʵÏÖ¹ÊÕÏתÒÆ
µ±Ä³¸öºó¶ËЧÀÍÆ÷å´»ú»òÎÞ·¨Õý³£·Öµ£¸ºÔØʱ£¬ÎÒÃÇÏ£ÍûNginxÄÜ×Ô¶¯½«Á÷Á¿×ª·¢µ½ÆäËû¿ÉÓõÄЧÀÍÆ÷ÉÏ£¬ÒÔʵÏÖ¹ÊÕÏתÒÆ¡£ÎªÁËʵÏÖÕâÒ»µã£¬ÎÒÃÇ¿ÉÒÔΪÿ¸öserver¿éÌí¼ÓbackupÊôÐÔ£¬²¢ÉèÖñ¸·ÝЧÀÍÆ÷¡£
ÒÔÏÂÊÇÒ»¸öʾÀýÉèÖã¬ÆäÖÐserver2.example.comΪ±¸·ÝЧÀÍÆ÷£º
upstream backend { server server1.example.com; server server2.example.com backup; server server3.example.com; }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ÈôÊÇserver1.example.comÎÞ·¨Õý³£ÊÂÇ飬Nginx»á×Ô¶¯½«Á÷Á¿×ª·¢µ½server2.example.com¡£
ÖØмÓÔØÉèÖÃ
µ±ÄãÐÞ¸ÄÍêNginxµÄÉèÖÃÎļþºó£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÖØмÓÔØÉèÖã¬Ê¹ÐµÄÉèÖÃÉúЧ£º
sudo nginx -s reload
µÇ¼ºó¸´ÖÆ
ÕâÑù¾ÍÍê³ÉÁËNginx¶àЧÀÍÆ÷¼¯ÈºÉèÖõÄÀú³Ì¡£Í¨¹ýÉèÖöą̀ЧÀÍÆ÷µÄ¼¯Èº£¬ÊµÏÖ¸ºÔØƽºâºÍ¹ÊÕÏתÒÆ£¬¿ÉÒÔÌá¸ßÍøÕ¾µÄ¿ÉÓÃÐÔºÍÐÔÄÜ¡£
×ܽ᣺
NginxÊÇÒ»¸öÇ¿Ê¢µÄ¿ªÔ´·´ÏòÊðÀíЧÀÍÆ÷£¬Í¨¹ý¶àЧÀÍÆ÷¼¯ÈºµÄÉèÖ㬿ÉÒÔÌá¸ßÍøÕ¾µÄ¿ÉÓÃÐÔºÍÐÔÄÜ¡£±¾ÎÄÏÈÈÝÁËͨ¹ýNginxÉèÖøºÔØƽºâºÍ¹ÊÕÏתÒƵĻù±¾ÒªÁ죬²¢¸ø³öÁË´úÂëʾÀý¡£Ï£Íû¶ÁÕßÄܹ»ÔÚÏÖʵӦÓÃÖÐÎÞаÔËÓÃÕâЩÉèÖã¬Ìá¸ß×Ô¼ºÍøÕ¾µÄ¿ÉÓÃÐÔ¡£
ÒÔÉϾÍÊÇNginx¶àЧÀÍÆ÷¼¯ÈºÉèÖã¬ÔöÌíÍøÕ¾¿ÉÓÃÐÔµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡