NginxЧÀÍÆ÷µÄ¸ß²¢·¢´¦Öóͷ£ºÍÇëÇóÏìÓ¦ÓÅ»¯¼¼ÇÉ
nginxЧÀÍÆ÷µÄ¸ß²¢·¢´¦Öóͷ£ºÍÇëÇóÏìÓ¦ÓÅ»¯¼¼ÇÉ
ÔÚµ±½ñ»¥ÁªÍøʱ´ú£¬ÍøÕ¾µÄ¸ß²¢·¢´¦Öóͷ£ºÍÇëÇóÏìÓ¦ËÙÂÊÖ±½ÓÓ°Ïì×ÅÓû§µÄÌåÑé¡£¶øNginx×÷Ϊһ¿î¸ßÐÔÄÜ¡¢¶à¹¦Ð§µÄЧÀÍÆ÷Èí¼þ£¬Äܹ»×ÊÖúÎÒÃÇʵÏָ߲¢·¢´¦Öóͷ£ºÍÇëÇóÏìÓ¦ÓÅ»¯¡£±¾ÎĽ«ÏÈÈݼ¸ÖÖ³£Óõļ¼ÇɺʹúÂëʾÀý£¬×ÊÖú¶ÁÕ߸üºÃµØÏàʶÔõÑùʹÓÃNginxʵÏָ߲¢·¢´¦Öóͷ£ºÍÇëÇóÏìÓ¦ÓÅ»¯¡£
Ò»¡¢Ê¹Ó÷´ÏòÊðÀí
NginxµÄ·´ÏòÊðÀí¹¦Ð§¿ÉÒÔ×ÊÖúÎÒÃÇʵÏָ߲¢·¢´¦Öóͷ£¡£Ê¹Ó÷´ÏòÊðÀíµÄÀûÒæÊÇ¿ÉÒÔ½«ÇëÇó·Ö·¢¸ø¶à¸öºó¶ËЧÀÍÆ÷£¬´Ó¶ø·Öµ£Ð§ÀÍÆ÷µÄ¸ºÔØѹÁ¦¡£ÒÔÏÂÊÇÒ»¸öʹÓ÷´ÏòÊðÀíµÄNginxÉèÖôúÂëʾÀý£º
http { upstream backend { server backend1.example.com; server backend2.example.com; server backend3.example.com; } server { listen 80; location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöʾÀýÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪbackendµÄÉÏÓÎЧÀÍÆ÷×飬ÆäÖаüÀ¨Á˶à¸öºó¶ËЧÀÍÆ÷¡£½Ó×Å£¬ÔÚserver¿éÖеÄlocationÉèÖÃÖУ¬ÎÒÃÇʹÓÃÁËproxy_passÖ¸ÁÇëÇóÊðÀíµ½backendÉÏÓÎЧÀÍÆ÷×é¡£ÕâÑù¾ÍÄܹ»ÊµÏÖÇëÇóµÄ¸ºÔØƽºâ£¬Ìá¸ßЧÀÍÆ÷µÄ²¢·¢´¦Öóͷ£ÄÜÁ¦¡£
¶þ¡¢ÆôÓÃgzipѹËõ
ÆôÓÃgzipѹËõ¿ÉÒÔ¼õСÏìÓ¦Êý¾ÝµÄ´«Êä¾Þϸ£¬Ìá¸ßÇëÇóÏìÓ¦ËÙÂÊ¡£ÒÔÏÂÊÇÒ»¸öÆôÓÃgzipѹËõµÄNginxÉèÖôúÂëʾÀý£º
http { gzip on; gzip_comp_level 5; gzip_min_length 256; gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript; server { listen 80; location / { ... } } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöʾÀýÖУ¬ÎÒÃÇÔÚhttp¿éÖÐʹÓÃÁËgzip onÖ¸ÁîÆôÓÃgzipѹËõ¡£½Ó×Å£¬Í¨¹ýgzip_comp_levelÖ¸ÁîÉèÖÃÁËѹËõ¼¶±ð£¬¹æÄ£´Ó1µ½9£¬Ä¬ÒÔΪ1¡£È»ºó£¬Í¨¹ýgzip_min_lengthÖ¸ÁîÉèÖÃÁË×îСѹËõ³¤¶È£¬Ö»Óе±ÏìÓ¦Êý¾ÝµÄ³¤¶ÈÁè¼Ý¸Ãֵʱ²Å»áÆôÓÃѹËõ¡£×îºó£¬Í¨¹ýgzip_typesÖ¸ÁîÖ¸¶¨ÁËÐèÒª¾ÙÐÐgzipѹËõµÄMIMEÀàÐÍ¡£
Èý¡¢Ê¹Óûº´æ¼ÓËÙ
»º´æ¼ÓËÙÊÇÌá¸ßÏìÓ¦ËÙÂʵÄÒ»ÖÖÓÐÓ÷½·¨¡£NginxµÄ»º´æ¹¦Ð§¿ÉÒÔ½«¾²Ì¬×ÊÔ´»º´æµ½ÄÚ´æ»ò´ÅÅÌÖУ¬´Ó¶ø¼ÓËÙÓû§ºóÐø»á¼ûµÄÏìÓ¦ËÙÂÊ¡£ÒÔÏÂÊÇÒ»¸öʹÓûº´æ¼ÓËÙµÄNginxÉèÖôúÂëʾÀý£º
http { proxy_cache_path /tmp/nginx_cache levels=1:2 keys_zone=my_cache:10m max_size=10g; server { listen 80; location / { proxy_cache my_cache; proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m; proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504; ... } } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöʾÀýÖУ¬ÎÒÃÇͨ¹ýproxy_cache_pathÖ¸ÁîÖ¸¶¨ÁË»º´æ·¾¶ºÍÏà¹Ø²ÎÊý£¬È绺´æ¼¶±ð¡¢»º´æÇøÃû¡¢×î´ó»º´æ¾ÞϸµÈ¡£½Ó×Å£¬ÔÚserver¿éÖеÄlocationÉèÖÃÖУ¬ÎÒÃÇʹÓÃÁËproxy_cacheÖ¸ÁîÆôÓûº´æ£¬²¢Í¨¹ýproxy_cache_validÖ¸ÁîÉèÖÃÁË»º´æµÄÓÐÓÃʱ¼ä¡£×îºó£¬Í¨¹ýproxy_cache_use_staleÖ¸ÁîÖ¸¶¨ÁËÔÚ¸üлº´æʱÊÇ·ñʹÓþɵĻº´æ¡£
×ܽ᣺
ͨ¹ýʹÓÃNginxµÄ·´ÏòÊðÀí¡¢ÆôÓÃgzipѹËõºÍʹÓûº´æ¼ÓËٵȼ¼ÇÉ£¬ÎÒÃÇ¿ÉÒÔÓÐÓÃÌá¸ßNginxЧÀÍÆ÷µÄ¸ß²¢·¢´¦Öóͷ£ºÍÇëÇóÏìÓ¦ÓÅ»¯ÄÜÁ¦¡£ËäÈ»£¬³ýÁ˱¾ÎÄÏÈÈݵļ¼ÇÉÍ⣬ÉÐÓÐÐí¶àÆäËûµÄÓÅ»¯ÒªÁì¿ÉÒÔ̽Ë÷ºÍʵ¼ù¡£Ï£Íûͨ¹ý±¾ÎĵÄÏÈÈÝ£¬¶ÁÕß¿ÉÒÔ¸üºÃµØÃ÷È·ºÍÓ¦ÓÃNginxЧÀÍÆ÷£¬ÊµÏÖ¸ü¸ßЧµÄ¸ß²¢·¢´¦Öóͷ£ºÍÇëÇóÏìÓ¦ÓÅ»¯¡£
ÒÔÉϾÍÊÇNginxЧÀÍÆ÷µÄ¸ß²¢·¢´¦Öóͷ£ºÍÇëÇóÏìÓ¦ÓÅ»¯¼¼ÇɵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡