ÉîÈë̽ÌÖNginxµÄ»º´æ»úÖƺÍÐÔÄܵ÷Óż¼ÇÉ
ÉîÈë̽ÌÖNginxµÄ»º´æ»úÖƺÍÐÔÄܵ÷Óż¼ÇÉ
СÐò£º
ÔÚÏÖÔÚµÄWeb¿ª·¢ÖУ¬¸ßÐÔÄܺ͸߲¢·¢ÊÇÎÒÃÇËù×·ÇóµÄÄ¿µÄ¡£¶øNginx×÷Ϊһ¸ö¸ßÐÔÄܵÄWebЧÀÍÆ÷£¬Æ仺´æ»úÖƺÍÐÔÄܵ÷Óż¼ÇɹØÓÚÌáÉýÍøÕ¾µÄ¸ºÔØÄÜÁ¦ÖÁ¹ØÖ÷Òª¡£±¾ÎĽ«ÉîÈë̽ÌÖNginxµÄ»º´æ»úÖƺÍÐÔÄܵ÷Óż¼ÇÉ£¬²¢¸ø³öÏà¹ØµÄ´úÂëʾÀý¡£
Ò»¡¢NginxµÄ»º´æ»úÖÆ
NginxµÄ»º´æ»úÖÆÊÇͨ¹ýÊðÀí»º´æÄ£¿éʵÏֵġ£Ëü¿ÉÒÔ½«¾ÓÉÊðÀíµÄÊý¾Ý»º´æµ½ÍâµØ£¬µ±Ï´ÎÓÐÏàͬµÄÇëÇóµ½À´Ê±£¬Ö±½Ó´Ó»º´æÖжÁÈ¡Êý¾Ý£¬¶ø²»ÐèÒªÔÙÈ¥Ïòºó¶ËЧÀÍÆ÷·¢ËÍÇëÇó¡£ÕâÑù¿ÉÒÔ´ó´ó¼õÇáºó¶ËЧÀÍÆ÷µÄѹÁ¦£¬ÌáÉýÍøÕ¾µÄÏìÓ¦ËÙÂÊ¡£
ÆôÓûº´æ
ÒªÆôÓÃNginxµÄ»º´æ¹¦Ð§£¬Ê×ÏÈÐèÒªÔÚNginxµÄÉèÖÃÎļþÖмÓÈëÒÔÏ´úÂ룺
http { ... proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off; proxy_temp_path /data/nginx/tmp; ... }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¶Î´úÂëÖУ¬proxy_cache_path½ç˵ÁË»º´æµÄ·¾¶ºÍһЩÏà¹ØµÄ²ÎÊý¡£ÆäÖУ¬levels²ÎÊýÌåÏÖÔÚ»º´æ·¾¶Öн¨ÉèµÄĿ¼²ã¼¶£¬keys_zone²ÎÊý½ç˵ÁËÒ»¸ö¹²ÏíÄÚ´æÇøÓò£¬ÓÃÓÚ´æ´¢»º´æµÄË÷ÒýºÍÏà¹ØÔªÊý¾Ý£¬max_size²ÎÊý½ç˵ÁË»º´æµÄ×î´ó¾Þϸ£¬inactive²ÎÊýÌåÏÖÁè¼Ýһ׼ʱ¼äûÓб»»á¼ûµÄ»º´æ½«±»ÒÔΪÊÇÓâÆڵģ¬use_temp_path²ÎÊýÖ¸¶¨ÊÇ·ñ½«»º´æÎļþ·ÅÔÚÔÝʱ·¾¶ÖС£
ÉèÖûº´æ¹æÔò
ÒªÖ¸¶¨ÄÄЩÇëÇóÐèÒª±»»º´æ£¬¿ÉÒÔÔÚNginxµÄÉèÖÃÎļþÖÐÌí¼ÓÒÔÏ´úÂ룺
http { ... location / { proxy_pass http://backend; proxy_cache my_cache; proxy_cache_valid 200 304 1h; } ... }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¶Î´úÂëÖУ¬proxy_passÖ¸Áî½ç˵ÁËÒªÊðÀíµ½µÄºó¶ËЧÀÍÆ÷µØµã£¬proxy_cacheÖ¸¶¨ÁËʹÓõĻº´æÇøÓò£¬proxy_cache_valid½ç˵ÁËHTTP״̬Âë200ºÍ304±»»º´æµÄʱ¼ä¡£
ˢкÍÕûÀí»º´æ
ΪÁË°ü¹ÜÊý¾ÝµÄʵʱÐÔºÍ׼ȷÐÔ£¬ÎÒÃÇÓÐʱÐèÒªÊÖ¶¯Ë¢Ð»òÕûÀí»º´æ¡£¿ÉÒÔÔÚNginxµÄÉèÖÃÎļþÖÐÌí¼ÓÒÔÏ´úÂ룺
http { ... location /flush_cache { internal; proxy_cache_purge my_cache "$scheme$request_method$host$request_uri"; return 200 "Cache flushed successfully"; } ... }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¶Î´úÂëÖУ¬locationÖ¸¶¨ÁËˢлº´æµÄURL£¬internalÖ¸ÁîÏÞÖÆÁ˸ÃÇëÇóÖ»Äܱ»NginxÄÚ²¿Å²Óã¬proxy_cache_purgeÖ¸ÁîÓÃÓÚÕûÀí»º´æ¡£
¶þ¡¢NginxµÄÐÔÄܵ÷Óż¼ÇÉ
³ýÁË»º´æ»úÖÆÖ®Í⣬ÎÒÃÇ»¹¿ÉÒÔͨ¹ýһЩÐÔÄܵ÷Óŵļ¼ÇÉÀ´½øÒ»²½ÌáÉýNginxµÄÐÔÄÜ¡£
Ìá¸ßWorkerÀú³ÌµÄ²¢·¢ÅþÁ¬Êý
NginxĬÈÏÉèÖõÄWorkerÀú³ÌÊý½ÏÉÙ£¬¿ÉÒÔͨ¹ýÐÞ¸ÄÉèÖÃÎļþÀ´Ìá¸ßWorkerÀú³ÌµÄÊýÄ¿£¬´Ó¶øÌá¸ß²¢·¢ÅþÁ¬Êý£º
worker_processes auto; events { worker_connections 4096; }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¶Î´úÂëÖУ¬worker_processesÖ¸¶¨ÁËWorkerÀú³ÌµÄÊýÄ¿£¬events²¿·ÖÔòÖ¸¶¨ÁËÿ¸öWorkerÀú³ÌµÄ×î´ó²¢·¢ÅþÁ¬Êý¡£
ÉèÖÃTCPÅþÁ¬ºÍ³¬Ê±²ÎÊý
ºÏÀíÉèÖÃTCPÅþÁ¬ºÍ³¬Ê±²ÎÊý¿ÉÒÔÌá¸ßNginxµÄÐÔÄÜ£º
http { ... keepalive_timeout 65; keepalive_requests 100; send_timeout 2m; client_header_timeout 1m; ... }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¶Î´úÂëÖУ¬keepalive_timeout½ç˵ÁËÒ»¸ö¿Í»§¶ËÅþÁ¬ÔÚûÓÐÈκÎÇëÇóµÄÇéÐÎϼá³Ö»îÔ¾µÄ×î´óʱ¼ä£¬keepalive_requests½ç˵ÁËÒ»¸ö¿Í»§¶ËÅþÁ¬ÇëÇóµÄ×î´ó´ÎÊý£¬send_timeout½ç˵ÁËÏò¿Í»§¶Ë·¢ËÍÏìÓ¦µÄ×ʱ¼ä£¬client_header_timeout½ç˵ÁËÎüÊÕ¿Í»§¶ËÇëÇóÍ·µÄ×ʱ¼ä¡£
ÆôÓÃGzipѹËõ
ÆôÓÃGzipѹËõ¿ÉÒÔ¼õС´«ÊäµÄÊý¾ÝÁ¿£¬Ìá¸ßÒ³Ãæ¼ÓÔØËÙÂÊ£º
http { ... gzip on; gzip_disable "msie6"; gzip_types text/plain text/css application/json; ... }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¶Î´úÂëÖУ¬gzipÖ¸ÁîÆôÓÃÁËGzipѹËõ£¬gzip_disableÖ¸¶¨Á˲»Ñ¹Ëõmsie6ä¯ÀÀÆ÷µÄÇëÇó£¬gzip_typesÖ¸¶¨ÁËÐèÒª¾ÙÐÐGzipѹËõµÄMIMEÀàÐÍ¡£
½áÓ
ͨ¹ýÉîÈë̽ÌÖNginxµÄ»º´æ»úÖƺÍÐÔÄܵ÷Óż¼ÇÉ£¬ÎÒÃÇ¿ÉÒÔ¸üºÃµØÃ÷È·ºÍÓ¦ÓÃNginx£¬²¢ÇÒÓÐÓÃÌáÉýÍøÕ¾µÄ¸ºÔØÄÜÁ¦ºÍÓû§ÌåÑ顣ͨ¹ýºÏÀíÉèÖûº´æ»úÖƺÍÐÔÄܲÎÊý£¬ÍŽáÏÖÕæÏàÐξÙÐе÷ÓÅ£¬ÎÒÃÇÄܹ»ÔÚ¸ßÐÔÄܺ͸߲¢·¢µÄWeb¿ª·¢ÖÐÈ¡µÃ¸üºÃµÄЧ¹û¡£Ï£Íû±¾ÎÄÄܹ»¶Ô¶ÁÕßÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇÉîÈë̽ÌÖNginxµÄ»º´æ»úÖƺÍÐÔÄܵ÷Óż¼ÇɵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡