Nginx´î½¨Ð§ÀÍÆ÷µÄ¾²Ì¬×ÊÔ´ÓÅ»¯ºÍ¶¯Ì¬ÄÚÈÝ»º´æ¼¼ÇÉÏê½â
nginx´î½¨Ð§ÀÍÆ÷µÄ¾²Ì¬×ÊÔ´ÓÅ»¯ºÍ¶¯Ì¬ÄÚÈÝ»º´æ¼¼ÇÉÏê½â
Ò»¡¢Ð¡Ðò
NginxÊÇÒ»¸ö¸ßÐÔÄܵÄHTTPºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬ÆÕ±éÓ¦ÓÃÓÚ¹¹½¨Îȹ̺͸ßЧµÄwebЧÀÍ¡£ÔÚʹÓÃNginx´î½¨Ð§ÀÍÆ÷ʱ£¬ÎÒÃÇ¿ÉÒÔͨ¹ýһЩÓÅ»¯»ººÍ´æ¼¼ÇÉÀ´ÌáÉýÆäÐÔÄܺÍЧÂÊ£¬Ìṩ¸üºÃµÄÓû§ÌåÑé¡£
±¾ÎĽ«ÏêϸÏÈÈÝÔõÑùʹÓÃNginx¾ÙÐо²Ì¬×ÊÔ´ÓÅ»¯ºÍ¶¯Ì¬ÄÚÈÝ»º´æ£¬°üÀ¨ÉèÖûº´æÓâÆÚʱ¼ä¡¢ÆôÓÃgzipѹËõ¡¢É趨»º´æ¹æÔòºÍÉèÖÃ×î¼Ñʵ¼ùµÈ¡£Í¬Ê±£¬ÎÒÃÇ»¹»áÌṩ´úÂëʾÀý£¬×ÊÖúÄú¸üºÃµØÃ÷È·ºÍʵ¼ùÕâЩ¼¼ÇÉ¡£
¶þ¡¢¾²Ì¬×ÊÔ´ÓÅ»¯
ÉèÖûº´æÓâÆÚʱ¼ä
¾²Ì¬×ÊÔ´ÈçͼƬ¡¢CSSºÍJavaScriptµÈÒ»Ñùƽ³£²»»áƵÈԱ任£¬Òò´Ë¿ÉÒÔͨ¹ýÉèÖûº´æÓâÆÚʱ¼äÀ´ïÔÌЧÀÍÆ÷ѹÁ¦¡£ÔÚNginxÉèÖÃÎļþÖУ¬¿ÉÒÔʹÓÃexpiresÖ¸ÁîÉèÖþ²Ì¬×ÊÔ´µÄ»º´æÓâÆÚʱ¼ä¡£ÒÔÏÂÊÇÒ»¸öʾÀý£º
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
expires 7d;
µÇ¼ºó¸´ÖÆ
}
ÉÏÊöÉèÖÃÌåÏÖ¹ØÓÚÆ¥ÅäÖ¸¶¨ºó׺µÄ¾²Ì¬×ÊÔ´£¬½«Æ仺´æ7Ìì¡£ÕâÑù£¬ÔÚ7ÌìÄÚ»á¼ûÏàͬ×ÊԴʱ£¬ä¯ÀÀÆ÷»áÖ±½Ó´ÓÍâµØ»º´æÖлñÈ¡£¬ïÔÌÁ˶ÔЧÀÍÆ÷µÄÇëÇó¡£
ÆôÓÃgzipѹËõ
ÆôÓÃgzipѹËõ¿ÉÒÔÓÐÓüõС´«ÊäÎļþµÄ¾Þϸ£¬ÌáÉýÒ³Ãæ¼ÓÔØËÙÂÊ¡£ÔÚNginxÉèÖÃÎļþÖУ¬¿ÉÒÔʹÓÃgzipÖ¸ÁîÀ´¿ªÆôgzipѹËõ¡£ÒÔÏÂÊÇÒ»¸öʾÀý£º
gzip on;
gzip_min_length 1k;
gzip_comp_level 2;
gzip_types text/plain text/css application/javascript;
gzip_vary on;
ÉÏÊöÉèÖÃÌåÏÖÆôÓÃgzipѹËõ£¬²¢ÉèÖÃÁËһЩÏà¹Ø²ÎÊý£¬ÈçѹËõ¼¶±ð¡¢ÎļþÀàÐ͵ȡ£
Èý¡¢¶¯Ì¬ÄÚÈÝ»º´æ
É趨»º´æ¹æÔò
¹ØÓÚ¶¯Ì¬ÌìÉúµÄÄÚÈÝ£¬ÎÒÃÇ¿ÉÒÔʹÓÃNginxµÄproxy_cacheÖ¸ÁîÀ´ÊµÏÖ»º´æ¡£ÏÂÃæÊÇÒ»¸öÉèÖÃÊðÀí»º´æµÄʾÀý£º
http {
proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m; server { location / { proxy_cache my_cache; proxy_cache_valid 200 10m; proxy_cache_valid 404 1m; proxy_pass http://backend; } }
µÇ¼ºó¸´ÖÆ
}
ÉÏÊöÉèÖÃÖУ¬ÎÒÃÇͨ¹ýproxy_cache_pathÀ´½ç˵»º´æ·¾¶ºÍÏà¹Ø²ÎÊý¡£ÔÚserver¿éÖУ¬Í¨¹ýproxy_cacheÖ¸ÁÆô»º´æ£¬²¢Ê¹ÓÃproxy_cache_validÀ´ÉèÖûº´æÓÐÓÃʱ¼ä¡£
ÉèÖÃ×î¼Ñʵ¼ù
³ýÁËÉÏÊö»ù±¾µÄÉèÖ㬻¹¿ÉÒÔƾ֤ÏÖʵÐèÇóÉèÖøü¶àµÄNginx»º´æÏà¹ØÖ¸ÁÒÔʵÏÖ¸üºÃµÄÐÔÄܺÍÓû§ÌåÑé¡£ÒÔÏÂÊÇһЩ×î¼Ñʵ¼ùµÄʾÀý£º
http {
... proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504; proxy_cache_lock on; proxy_cache_lock_age 5s; proxy_cache_lock_timeout 10s; proxy_cache_revalidate on; proxy_cache_background_update on; proxy_cache_bypass $cookie_nocache $arg_nocache$arg_comment; ...
µÇ¼ºó¸´ÖÆ
}
ÉÏÊöÉèÖÃÖУ¬ÎÒÃÇʹÓÃÁËproxy_cache_use_staleÀ´ÉèÖûº´æÓâÆÚºóµÄÐÐΪ£¬proxy_cache_lockÀ´ÆôÓûº´æËø¶¨¹¦Ð§£¬proxy_cache_revalidateÀ´ÖØÐÂÑéÖ¤»º´æ¹¤¾ßµÄÓÐÓÃÐԵȵȡ£
ËÄ¡¢×ܽá
±¾ÎÄÏêϸÏÈÈÝÁËÔõÑùʹÓÃNginx¾ÙÐо²Ì¬×ÊÔ´ÓÅ»¯ºÍ¶¯Ì¬ÄÚÈÝ»º´æµÄ¼¼ÇÉ¡£Í¨¹ýÉèÖûº´æÓâÆÚʱ¼ä¡¢ÆôÓÃgzipѹËõ¡¢É趨»º´æ¹æÔòºÍÉèÖÃ×î¼Ñʵ¼ù£¬ÎÒÃÇ¿ÉÒÔÌáÉýЧÀÍÆ÷ÐÔÄܺÍÓû§ÌåÑé¡£
ÔÚʵ¼ùÀú³ÌÖУ¬ÎÒÃÇ»¹Í¨¹ý´úÂëʾÀýÀ´ÑÝʾÁËÏêϸµÄÉèÖÃÒªÁìºÍʹÓü¼ÇÉ¡£Ï£Íûͨ¹ý±¾ÎĵÄÏÈÈÝ£¬ÄúÄܸüºÃµØÃ÷È·ºÍʹÓÃNginx¾ÙÐÐЧÀÍÆ÷ÓÅ»¯»ººÍ´æ¼¼ÇÉ£¬ÎªÓû§Ìṩ¸ü¿ì¡¢¸üÎȹ̵ÄwebЧÀÍ¡£
ÒÔÉϾÍÊÇNginx´î½¨Ð§ÀÍÆ÷µÄ¾²Ì¬×ÊÔ´ÓÅ»¯ºÍ¶¯Ì¬ÄÚÈÝ»º´æ¼¼ÇÉÏê½âµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡