尊龙凯时人生就是搏

ÔõÑùʹÓÃNginxʵÏָ߿ÉÓõÄWeb¼¯Èº

ÔõÑùʹÓÃnginxʵÏָ߿ÉÓõÄweb¼¯Èº

СÐò:

ÔÚÏÖ´ú»¥ÁªÍøÓ¦ÓÃÖУ¬¸ß¿ÉÓÃÐÔÊÇÒ»¸öºÜÊÇÖ÷ÒªµÄ¿¼Á¿ÒòËØ¡£ÎªÁËÈ·±£Ôڸ߻á¼ûÁ¿ÏÂÓ¦ÓõÄÎȹÌÐԺͿɿ¿ÐÔ£¬ÍùÍùÐèÒª¹¹½¨Ò»¸ö¸ß¿ÉÓÃµÄ Web ¼¯Èº¡£ÆäÖУ¬Nginx ÊÇÒ»¸öºÜÊÇÊ¢Ðеķ´ÏòÊðÀíЧÀÍÆ÷£¬Ëü¿ÉÒÔͨ¹ýһЩ¼òÆÓµÄÉèÖÃʵÏÖ¸ºÔØƽºâºÍ¸ß¿ÉÓÃÐÔ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓà Nginx ´î½¨Ò»¸ö¸ß¿ÉÓÃµÄ Web ¼¯Èº£¬²¢¸ø³ö´úÂëʾÀý¡£

Ò»¡¢×°ÖúÍÉèÖà Nginx

Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚÿ¸ö Web ЧÀÍÆ÷ÉÏ×°ÖúÍÉèÖà Nginx¡£

×°Öà Nginx£º

ÔÚ Ubuntu ϵͳÖУ¬¿Éͨ¹ýÒÔÏÂÏÂÁî×°Öà Nginx£º

$ sudo apt-get update
$ sudo apt-get install nginx

µÇ¼ºó¸´ÖÆ

ÉèÖà Nginx£º

·­¿ª Nginx ÉèÖÃÎļþ nginx.conf£¬Ä¬ÈÏ·¾¶ÊÇ /etc/nginx/nginx.conf£¬Ê¹ÓÃÎı¾±à¼­Æ÷¾ÙÐб༭¡£

ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄʾÀýÉèÖãº

http {
 upstream backend {
     server backend1.example.com;
     server backend2.example.com;
     server backend3.example.com;
 }
  
 server {
     listen 80;
     
     location / {
         proxy_pass http://backend;
         proxy_redirect off;
     
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     }
 }
}

µÇ¼ºó¸´ÖÆ

ÆäÖУ¬

upstream ¿é½ç˵ÁËÒ»×鸺ÔØƽºâºó¶ËЧÀÍÆ÷£»

server ¿é½ç˵Á˼àÌýµÄ¶Ë¿ÚºÍ»á¼ûÉèÖá£

ÉúÑIJ¢Í˳öÉèÖÃÎļþ£¬È»ºóÖØÆô Nginx ЧÀÍÒÔʹÉèÖÃÉúЧ£º

$ sudo systemctl restart nginx

µÇ¼ºó¸´ÖÆ

¶þ¡¢ÉèÖøºÔØƽºâÕ½ÂÔ

ΪÁËʵÏÖ¸ºÔØƽºâ£¬¿ÉÒÔÔÚ Nginx ÉèÖÃÎļþÖнç˵²î±ðµÄ¸ºÔØƽºâÕ½ÂÔ¡£ÒÔÏÂÊÇһЩ³£ÓõĸºÔØƽºâÕ½ÂÔ¡£

ÂÖѯ£¨Round-Robin£©Õ½ÂÔ£º

¸ÃÕ½ÂÔƾ֤ºó¶ËЧÀÍÆ÷µÄ˳ÐòÒÀ´Î·ÖÅÉÇëÇó¡£

upstream backend {
 server backend1.example.com;
 server backend2.example.com;
 server backend3.example.com;
}

µÇ¼ºó¸´ÖÆ

IP ¹þÏ££¨IP Hash£©Õ½ÂÔ£º

¸ÃÕ½ÂÔͨ¹ý¶Ô¿Í»§¶Ë IP µØµã¾ÙÐйþÏ£ÅÌË㣬½«Í³Ò»¸ö¿Í»§¶ËµÄÇëÇó·¢Ë͵½Í³Ò»Ì¨ºó¶ËЧÀÍÆ÷ÉÏ¡£

upstream backend {
 ip_hash;
 server backend1.example.com;
 server backend2.example.com;
 server backend3.example.com;
}

µÇ¼ºó¸´ÖÆ

¼ÓȨÂÖѯ£¨Weighted Round-Robin£©Õ½ÂÔ£º

¸ÃÕ½ÂÔƾ֤ºó¶ËЧÀÍÆ÷µÄȨÖؾÞϸ¾ÙÐÐÇëÇó·ÖÅÉ¡£

upstream backend {
 server backend1.example.com weight=3;
 server backend2.example.com weight=2;
 server backend3.example.com weight=1;
}

µÇ¼ºó¸´ÖÆ

Èý¡¢ÉèÖÿµ½¡¼ì²éºÍÈÝ´í

ΪÁËʵÏָ߿ÉÓÃÐÔ£¬ÎÒÃÇÐèÒª¶Ôºó¶ËЧÀÍÆ÷¾ÙÐпµ½¡¼ì²é£¬²¢ÔÚЧÀÍÆ÷²»¿ÉÓÃʱ¾ÙÐÐÈÝ´í´¦Àí¡£

¿µ½¡¼ì²é£º

¿ÉÒÔͨ¹ý nginx_http_healthcheck_module »ò nginx-stream-healthcheck-module Ä£¿éʵÏÖ Nginx ¶Ôºó¶ËЧÀÍÆ÷µÄ¿µ½¡¼ì²é¡£

ÈÝ´í´¦Àí£º

µ±Ä³Ì¨ºó¶ËЧÀÍÆ÷²»¿ÉÓÃʱ£¬Nginx ¿ÉÒÔ½«ÇëÇó·¢ËÍÖÁÆäËû¿ÉÓÃЧÀÍÆ÷£¬ÒÔʵÏÖÈÝ´í¡£ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄÈÝ´íÉèÖÃʾÀý£º

http {
 upstream backend {
     server backend1.example.com;
     server backend2.example.com backup;
     server backend3.example.com;
 }
  
 server {
     listen 80;
     ...
 }
}

µÇ¼ºó¸´ÖÆ

ÒÔÉÏÉèÖÃÖУ¬backup ²ÎÊýÖ¸¶¨Á˱¸ÓÃЧÀÍÆ÷£¬µ±Ö÷ЧÀÍÆ÷²»¿ÉÓÃʱ£¬ÇëÇó»áת·¢µ½±¸ÓÃЧÀÍÆ÷¡£

ËÄ¡¢´úÂëʾÀý

ÒÔÏÂÊÇÒ»¸ö¼òÆӵĻùÓÚ Node.js µÄ Web ЧÀÍÆ÷ʾÀý£¬ÓÃÓÚÑÝʾ Nginx µÄ¸ºÔØƽºâ¹¦Ð§¡£

server.js£º

const http = require('http');

const server = http.createServer((req, res) => {
 res.statusCode = 200;
 res.setHeader('Content-Type', 'text/plain');
 res.end('Hello, World!');
});

server.listen(3000, 'localhost', () => {
 console.log('Server running at http://localhost:3000/');
});

µÇ¼ºó¸´ÖÆ

Æô¶¯ 3 ¸öЧÀÍÆ÷£º

$ node server.js
$ node server.js
$ node server.js

µÇ¼ºó¸´ÖÆ

Nginx ÉèÖÃÎļþ£¨²Î¿¼Ö®Ç°µÄʾÀý£©¡£

ͨ¹ýÒÔÉÏÉèÖã¬ÎÒÃÇ¿ÉÒÔÔÚä¯ÀÀÆ÷Öлá¼û http://localhost£¬²¢ÔÚÿ´ÎË¢ÐÂÒ³ÃæʱÊӲ쵽ÇëÇó±»·ÖÅɵ½²î±ðµÄºó¶ËЧÀÍÆ÷¡£

½áÂÛ:

±¾ÎÄÏÈÈÝÁËÔõÑùʹÓà Nginx ´î½¨Ò»¸ö¸ß¿ÉÓÃµÄ Web ¼¯Èº£¬²¢ÌṩÁË×°Öá¢ÉèÖúʹúÂëʾÀý¡£Í¨¹ýºÏÀíÉèÖøºÔØƽºâÕ½ÂÔ¡¢¿µ½¡¼ì²éºÍÈÝ´í´¦Àí£¬¿ÉÒÔÈ·±£ Web ¼¯ÈºÔڸ߻á¼ûÁ¿ÏµÄÎȹÌÐԺͿɿ¿ÐÔ¡£Ï£Íû±¾ÎĶÔÄúÓÐËù×ÊÖú£¬Ð»Ð»ÔĶÁ£¡

ÒÔÉϾÍÊÇÔõÑùʹÓÃNginxʵÏָ߿ÉÓõÄWeb¼¯ÈºµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是搏

18523999891

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

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

QR code
sitemap¡¢ÍøÕ¾µØͼ