×ðÁú¿­Ê±¹ÙÍøµÇ¼

Nginx¸ºÔØƽºâËã·¨Ïê½â £¬ÓÅ»¯ÍøվЧÀÍ

nginx¸ºÔØƽºâËã·¨Ïê½â £¬ÓÅ»¯ÍøվЧÀÍ

СÐò£º

ÔÚ´óÐÍÍøÕ¾ÖÐ £¬ÎªÁËÌá¸ßÍøÕ¾µÄÐÔÄܺͿɿ¿ÐÔ £¬¾­³£ÐèҪʹÓøºÔØƽºâÊÖÒÕ ¡£NginxÊÇÒ»¿î¸ßÐÔÄܵĿªÔ´·´ÏòÊðÀíЧÀÍÆ÷ £¬Ëü¿ÉÒÔ×÷Ϊ¸ºÔØƽºâЧÀÍÆ÷ £¬½«¿Í»§¶ËÇëÇó·Ö·¢µ½¶ą̀ºó¶ËЧÀÍÆ÷¾ÙÐд¦Àí £¬´Ó¶øʵÏÖÍøÕ¾µÄ¸ß²¢·¢»á¼ûºÍ¹ÊÕÏתÒÆ ¡£±¾ÎĽ«ÏêϸÏÈÈÝNginx¸ºÔØƽºâËã·¨ £¬²¢ÇÒÌṩһЩ´úÂëʾÀý ¡£

Ò»¡¢³£¼ûµÄ¸ºÔØƽºâËã·¨£º

ÂÖѯ£¨Round Robin£©£º½«¿Í»§¶ËÇëÇóÔȳƵطַ¢¸øºó¶ËЧÀÍÆ÷ ¡£µ±Ð§ÀÍÆ÷²î±ðµÄÐÔÄÜÆßÁã°ËÂäʱ £¬ÂÖѯËã·¨¿ÉÄÜÎÞ·¨µÖ´ïÀíÏëµÄ¸ºÔØƽºâЧ¹û£»

¼ÓȨÂÖѯ£¨Weighted Round Robin£©£ºÔÚÂÖѯËã·¨µÄ»ù´¡ÉϼÓÈëÁËȨÖصĿ´·¨ £¬Í¨¹ýÉèÖÃȨÖØÖµ¿ÉÒÔµ÷½âºó¶ËЧÀÍÆ÷µÄ¸ºÔØƽºâ±ÈÀý£»

IP¹þÏ££¨IP Hash£©£ºÆ¾Ö¤¿Í»§¶ËµÄIPµØµã¾ÙÐйþÏ£ÅÌËã £¬½«Í³Ò»¿Í»§¶ËµÄÇëÇó·Ö·¢µ½Í³Ò»ºó¶ËЧÀÍÆ÷ £¬°ü¹ÜÁËͳһ¿Í»§¶ËÔÚÒ»¶Îʱ¼äÄÚ»á¼ûµÄÊÇͳһ̨ЧÀÍÆ÷ £¬ÊÊÓÃÓÚ״̬¼á³ÖµÄÓ¦Óó¡¾°£»

×îÉÙÅþÁ¬£¨Least Connections£©£º½«ÇëÇó·Ö·¢µ½Ä¿½ñÅþÁ¬Êý×îÉٵĺó¶ËЧÀÍÆ÷ £¬ÔÆÔÆ·Ö·¢¿ÉÒÔ×îºéÁ÷ƽÉÏ°ü¹Üºó¶ËЧÀÍÆ÷µÄ¸ºÔØƽºâ ¡£

¶þ¡¢Nginx¸ºÔØƽºâÉèÖãº

ÒÔÏÂÊÇÒ»¸öʹÓÃ×îÉÙÅþÁ¬Ëã·¨µÄNginx¸ºÔØƽºâÉèÖÃʾÀý£º

ÐÞ¸Änginx.confÉèÖÃÎļþ £¬ÔÚhttp¿éÖÐÌí¼ÓÈçÏÂÄÚÈÝ£º

upstream backend {
 least_conn;   #ʹÓÃ×îÉÙÅþÁ¬Ëã·¨
 server backend1.example.com;
 server backend2.example.com;
}

server {
 listen 80;
 location / {
     proxy_pass http://backend;  #ת·¢µ½backendºó¶ËЧÀÍÆ÷×é
 }
}

µÇ¼ºó¸´ÖÆ

Ð޸ĺó¶ËЧÀÍÆ÷µÄÉèÖÃÎļþ£º

ÔÚÿ̨ºó¶ËЧÀÍÆ÷µÄnginx.confÉèÖÃÎļþÖÐ £¬Ö¸¶¨×ÔÉíµÄIPµØµãºÍ¶Ë¿ÚºÅ £¬ÈçÏÂËùʾ£º

server {
 listen 80;
 server_name backend1.example.com;
 ...
}

server {
 listen 80;
 server_name backend2.example.com;
 ...
}

µÇ¼ºó¸´ÖÆ

Èý¡¢Nginx¸ºÔØƽºâÊÃ÷ÈÕ½£º

ÏÂÃæÒÔÒ»¸ö¼òÆÓµÄWebÓ¦ÓÃΪÀý £¬ÑÝʾNginx¸ºÔØƽºâµÄÏÖʵӦÓà ¡£

±àдһ¸ö¼òÆÓµÄWebÓ¦Óãº

ÎÒÃÇʹÓÃPythonµÄFlask¿ò¼Ü±àдһ¸ö¼òÆÓµÄWebÓ¦Óà £¬´úÂëÈçÏ£º

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
 return "Hello, World!"

if __name__ == '__main__':
 app.run(host='0.0.0.0', port=5000)

µÇ¼ºó¸´ÖÆ

Æô¶¯Á½¸öºó¶ËЧÀÍÆ÷£º

ÔÚÁ½Ì¨Ð§ÀÍÆ÷ÉÏ»®·ÖÔËÐÐÕâ¸öWebÓ¦Óà £¬»®·Ö¼àÌý²î±ðµÄ¶Ë¿ÚºÅ ¡£

ÐÞ¸ÄNginxÉèÖÃÎļþ£º

ÔÚNginxµÄÉèÖÃÎļþÖоÙÐÐÈçÏÂÉèÖãº

http {
 upstream backend {
     least_conn;
     server 192.168.0.1:5000;
     server 192.168.0.2:5000;
 }

 server {
     listen 80;
     location / {
         proxy_pass http://backend;
     }
 }
}

µÇ¼ºó¸´ÖÆ

Æô¶¯NginxЧÀÍÆ÷£º

Æô¶¯NginxЧÀÍÆ÷ £¬½«¿Í»§¶ËÇëÇóת·¢¸øÁ½Ì¨ºó¶ËЧÀÍÆ÷¾ÙÐд¦Àí ¡£

½áÂÛ£º

ͨ¹ýNginx¸ºÔØƽºâËã·¨µÄÑ¡ÔñºÍÉèÖà £¬ÎÒÃÇ¿ÉÒÔʵÏÖÍøÕ¾µÄ¸ß²¢·¢»á¼ûºÍ¹ÊÕÏתÒÆ £¬Ìá¸ßÁËÍøÕ¾µÄÐÔÄܺͿɿ¿ÐÔ ¡£ÔÚÏÖʵӦÓÃÖÐ £¬Æ¾Ö¤ÏêϸµÄÐèÇóÑ¡ÔñÊʺϵĸºÔØƽºâËã·¨ £¬²¢¾ÙÐÐÏìÓ¦µÄÉèÖà £¬¿ÉÒÔ×îºéÁ÷ƽµØÓÅ»¯ÍøÕ¾µÄЧÀÍ ¡£

×ܽ᣺

±¾ÎÄÏêϸÏÈÈÝÁËNginx¸ºÔØƽºâËã·¨ £¬²¢ÌṩÁËÏà¹ØµÄ´úÂëʾÀý ¡£Ï£Íû¶ÁÕßͨ¹ýÔĶÁ±¾ÎÄ £¬¶ÔNginx¸ºÔØƽºâÓиüÉîÈëµÄÏàʶ £¬²¢ÄÜÔÚÏÖʵӦÓÃÖÐÎÞаÔËÓà ¡£Í¬Ê± £¬Ò²Ï£Íû±¾ÎÄÄܹ»ÎªÍøÕ¾ÐÔÄÜÓÅ»¯ÌṩһЩ²Î¿¼ºÍÖ¸µ¼ ¡£

ÒÔÉϾÍÊÇNginx¸ºÔØƽºâËã·¨Ïê½â £¬ÓÅ»¯ÍøվЧÀ͵ÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼ ¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý ¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ