ÏêϸÆÊÎöNginxЧÀÍÆ÷Ôڸ߲¢·¢ÇéÐÎÖеÄÐÔÄÜÓÅ»¯Õ½ÂÔ
ÏêϸÆÊÎöNginxЧÀÍÆ÷Ôڸ߲¢·¢ÇéÐÎÖеÄÐÔÄÜÓÅ»¯Õ½ÂÔ
Ëæ×Å»¥ÁªÍøµÄ¿ìËÙÉú³¤£¬¸ß²¢·¢»á¼û³ÉΪһ¸öÈÕÒæÍ»³öµÄÎÊÌâ¡£×÷Ϊһ¸ö¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬NginxÔÚ´¦Àí¸ß²¢·¢ÇëÇóʱÌåÏÖ¾«²Ê¡£±¾ÎĽ«ÏêϸÆÊÎöNginxÔڸ߲¢·¢ÇéÐÎÖеÄÐÔÄÜÓÅ»¯Õ½ÂÔ£¬²¢Ìṩ´úÂëʾÀý£¬×ÊÖú¶ÁÕßÏàʶ²¢Êµ¼ùÕâЩսÂÔ¡£
Ò»¡¢³ä·ÖʹÓÃNginxµÄÊÂÎñÇý¶¯¼Ü¹¹
Nginx½ÓÄÉÁËÊÂÎñÇý¶¯µÄ¼Ü¹¹£¬Í¨¹ýʹÓ÷ÇÛÕ±ÕµÄI/OÄ£×ÓÀ´¸ßЧ´¦Àí²¢·¢ÇëÇó¡£Ôڸ߲¢·¢ÇéÐÎÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýµ÷½âNginxµÄworker_processesºÍworker_connections²ÎÊýÀ´³ä·ÖʹÓÃÆäÊÂÎñÇý¶¯µÄÌØÕ÷¡£
worker_processes²ÎÊý£ºÖ¸¶¨NginxµÄÊÂÇéÀú³ÌÊý¡£ÔÚ¶àºËCPUµÄЧÀÍÆ÷ÉÏ£¬¿ÉÒÔ½«¸Ã²ÎÊýÉèÖÃΪCPUºËÊýµÄ2±¶¡£ÀýÈ磬¹ØÓÚ4ºËCPUµÄЧÀÍÆ÷£¬¿ÉÒÔÉèÖÃworker_processesΪ8£º
worker_processes 8;
worker_connections²ÎÊý£ºÖ¸¶¨Ã¿¸öÊÂÇéÀú³Ì¿ÉÒÔͬʱ´¦ÀíµÄÅþÁ¬Êý¡£¿ÉÒÔƾ֤ЧÀÍÆ÷µÄÉèÖúÍÐèÇó¾ÙÐе÷½â¡£ÀýÈ磬¿ÉÒÔ½«worker_connectionsÉèÖÃΪ1024£º
events {
worker_connections 1024;
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
}
¶þ¡¢ºÏÀíÉèÖÃNginxµÄ»º³åÇø
ºÏÀíÉèÖÃNginxµÄ»º³åÇø¿ÉÒÔÌáÉýÆäÔڸ߲¢·¢ÇéÐÎÖеÄÐÔÄÜ¡£
client_body_buffer_size²ÎÊý£ºÖ¸¶¨NginxÎüÊÕ¿Í»§¶ËÇëÇóÌåµÄ»º³åÇø¾Þϸ¡£¿ÉÒÔƾ֤ÇëÇóÌåµÄ¾Þϸ¾ÙÐе÷½â¡£ÀýÈ磬¿ÉÒÔ½«client_body_buffer_sizeÉèÖÃΪ1m£º
client_body_buffer_size 1m;
client_header_buffer_size²ÎÊý£ºÖ¸¶¨NginxÎüÊÕ¿Í»§¶ËÇëÇóÍ·²¿µÄ»º³åÇø¾Þϸ¡£¿ÉÒÔƾ֤ÇëÇóÍ·²¿µÄ¾Þϸ¾ÙÐе÷½â¡£ÀýÈ磬¿ÉÒÔ½«client_header_buffer_sizeÉèÖÃΪ2k£º
client_header_buffer_size 2k;
Èý¡¢Ê¹ÓÃNginxµÄ·´ÏòÊðÀí»º´æ¹¦Ð§
NginxµÄ·´ÏòÊðÀí»º´æ¹¦Ð§¿ÉÒÔ´ó´óÌáÉýÔڸ߲¢·¢ÇéÐÎÖеÄÐÔÄÜ¡£Í¨¹ý½«ÇëÇóµÄЧ¹û»º´æÆðÀ´£¬¿ÉÒÔ¼õÇáºó¶ËЧÀÍÆ÷µÄѹÁ¦£¬´Ó¶øÌáÉýÕûÌåµÄÏìÓ¦ËÙÂÊ¡£
proxy_cache_path²ÎÊý£ºÖ¸¶¨NginxµÄ·´ÏòÊðÀí»º´æ·¾¶¡£¿ÉÒÔƾ֤ЧÀÍÆ÷µÄÉèÖúÍÐèÇó¾ÙÐе÷½â¡£ÀýÈ磬¿ÉÒÔ½«proxy_cache_pathÉèÖÃΪ/var/cache/nginx/proxy_cache£º
proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m;
proxy_cache²ÎÊý£ºÓÃÓÚ¿ªÆô»ò¹Ø±ÕNginxµÄ·´ÏòÊðÀí»º´æ¹¦Ð§¡£ÀýÈ磬¿ÉÒÔ½«proxy_cacheÉèÖÃΪon£º
proxy_cache on;
ËÄ¡¢Ê¹ÓÃNginxµÄ¸ºÔØƽºâ¹¦Ð§
NginxµÄ¸ºÔØƽºâ¹¦Ð§¿ÉÒÔ½«ÇëÇó·Ö·¢µ½¶à¸öºó¶ËЧÀÍÆ÷ÉÏ£¬ÌáÉý²¢·¢»á¼ûµÄ´¦ÀíÄÜÁ¦¡£
upstream²ÎÊý£ºÓÃÓÚÉèÖúó¶ËЧÀÍÆ÷µÄµØµãºÍȨÖØ¡£¿ÉÒÔƾ֤ЧÀÍÆ÷µÄÉèÖúÍÐèÇó¾ÙÐе÷½â¡£ÀýÈ磬¿ÉÒÔ½«upstreamÉèÖÃΪ£º
upstream backend {
server backend1.example.com weight=5; server backend2.example.com; server backend3.example.com;
µÇ¼ºó¸´ÖÆ
}
proxy_pass²ÎÊý£ºÓÃÓÚÖ¸¶¨Nginx½«ÇëÇóת·¢µ½µÄºó¶ËЧÀÍÆ÷¡£ÀýÈ磬¿ÉÒÔ½«proxy_passÉèÖÃΪ£º
proxy_pass http://backend;
ͨ¹ýÉÏÊöÓÅ»¯Õ½ÂÔ£¬ÎÒÃÇ¿ÉÒÔ³ä·ÖʹÓÃNginxµÄÐÔÄÜÓÅÊÆ£¬ÌáÉýÆäÔڸ߲¢·¢ÇéÐÎÖеĴ¦ÀíÄÜÁ¦¡£ÒÔÏÂÊÇÒ»¸öÍêÕûµÄNginxÉèÖÃʾÀý£º
user nginx;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 1024;
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
}
http {
... client_body_buffer_size 1m; client_header_buffer_size 2k; proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m; proxy_cache my_cache; upstream backend { server backend1.example.com weight=5; server backend2.example.com; server backend3.example.com; } server { listen 80; location / { proxy_pass http://backend; proxy_cache my_cache; } } ...
µÇ¼ºó¸´ÖÆ
}
Ï£Íûͨ¹ý±¾ÎĵÄÏÈÈݺÍʾÀý£¬¶ÁÕßÄܹ»ÉîÈëÃ÷È·²¢Êµ¼ùNginxÔڸ߲¢·¢ÇéÐÎÖеÄÐÔÄÜÓÅ»¯Õ½ÂÔ£¬´Ó¶øÌáÉýЧÀÍÆ÷µÄ´¦ÀíÄÜÁ¦ºÍÏìÓ¦ËÙÂÊ¡£Í¨¹ýÎÞаÉèÖÃNginx£¬²¢ÍŽáÏÖÕæÏàÐξÙÐе÷½â£¬ÎÒÃÇ¿ÉÒÔ¸üºÃµØÖª×ãÓû§µÄÐèÇó£¬Ìṩ¸üºÃµÄÓû§ÌåÑé¡£
ÒÔÉϾÍÊÇÏêϸÆÊÎöNginxЧÀÍÆ÷Ôڸ߲¢·¢ÇéÐÎÖеÄÐÔÄÜÓÅ»¯Õ½ÂÔµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡