½¨Õ¾Ñ§ - ÇáËɽ¨Õ¾´Ó´Ë¿ªÊ¼£¡

½¨Õ¾Ñ§-¸öÈ˽¨Õ¾Ö¸ÄÏ,ÍøÒ³ÖÆ×÷,ÍøÕ¾Éè¼Æ,ÍøÕ¾ÖÆ×÷½Ì³Ì

nginx»º´æcacheµÄ5ÖÖ·½°¸

ʱ¼ä:2009-07-04 22:57À´Ô´: ×÷Õß: µã»÷: ´Î
nginx»º´æcacheµÄ5ÖÖ·½°¸,òËÆ´ÓÀ´Ã»ÓÐд¹ýnginxµÄ»º´æ¹¦ÄÜ£¬¶¼ÊÇÖ»¿´²»·ÖÏí£¬ÕâÊDz»ºÃ֮ϰ¹ß°¡¡£Õâ¸ö°ì·¨ÊÇ°ÑnginxµÄ404´íÎó¶¨Ïòµ½ºó¶Ë£¬È»ºóÓÃproxy_store°Ñºó¶Ë·µ»ØµÄÒ³Ãæ±£´æ¡£

òËÆ´ÓÀ´Ã»ÓÐд¹ýnginxµÄ»º´æ¹¦ÄÜ£¬¶¼ÊÇÖ»¿´²»·ÖÏí£¬ÕâÊDz»ºÃ֮ϰ¹ß°¡¡£

1¡¢´«Í³»º´æÖ®Ò»£¨404£©

Õâ¸ö°ì·¨ÊÇ°ÑnginxµÄ404´íÎó¶¨Ïòµ½ºó¶Ë£¬È»ºóÓÃproxy_store°Ñºó¶Ë·µ»ØµÄÒ³Ãæ±£´æ¡£

ÅäÖãº

location / {
root /home/html/;#Ö÷Ŀ¼
expires 1d;#ÍøÒ³µÄ¹ýÆÚʱ¼ä
error_page 404 =200 /fetch$request_uri;#404¶¨Ïòµ½/fetchĿ¼ÏÂ
}

location /fetch/ {#404¶¨Ïòµ½ÕâÀï
internal;#Ö¸Ã÷Õâ¸öĿ¼²»ÄÜÔÚÍⲿֱ½Ó·ÃÎʵ½
expires 1d;#ÍøÒ³µÄ¹ýÆÚʱ¼ä
alias /home/html/;#ÐéÄâĿ¼ÎļþϵͳµØÖ·ÒªºÍlocaion /Ò»Ö£¬proxy_store»á½«Îļþ±£´æµ½ÕâĿ¼ÏÂ
proxy_pass http://www.sudone.com/;#ºó¶ËupstreamµØÖ·£¬/fetchͬʱÊÇÒ»¸ö´úÀí
proxy_set_header Accept-Encoding '';#Èúó¶Ë²»Òª·µ»ØѹËõ£¨gzip»òdeflate£©µÄÄÚÈÝ£¬±£´æѹËõºóµÄÄÚÈÝ»áÒý·¢ÂÒ×Ó¡£
proxy_store on;#Ö¸¶¨nginx½«´úÀí·µ»ØµÄÎļþ±£´æ
proxy_temp_path /home/tmp;#ÁÙʱĿ¼£¬Õâ¸öĿ¼ҪºÍ/home/htmlÔÚͬһ¸öÓ²ÅÌ·ÖÇøÄÚ
}

ʹÓõÄʱºò»¹ÓÐҪעÒâÊÇnginxÒªÓÐȨÏÞÍù/home/tmpºÍ/home/htmlÏÂÓÐдÈëÎļþµÄȨÏÞ£¬ÔÚlinuxÏÂnginxÒ»°ã»áÅäÖóÉnobodyÓû§ÔËÐУ¬ÕâÑùÕâÁ½¸öĿ¼¾ÍÒªchown nobody£¬Éè³ÉnobodyÓû§×¨Ó㬵±È»Ò²¿ÉÒÔchmod 777£¬²»¹ýËùÓÐÓо­ÑéµÄϵͳ¹ÜÀíÔ±¶¼»á½¨Òé²»ÒªËæ±ãʹÓÃ777¡£

2¡¢´«Í³»º´æÖ®¶þ£¨!-e£©

Ô­ÀíºÍ404Ìøת»ù±¾Ò»Ö£¬µ«¸ü¼ò½àһЩ£º

location / {
root /home/html/;
proxy_store on;
proxy_set_header Accept-Encoding '';
proxy_temp_path /home/tmp;
if ( !-f $request_filename )
{
        proxy_pass http://www.sudone.com/;
}
}

¿ÉÒÔ¿´µ½Õâ¸öÅäÖñÈ404½ÚÔ¼Á˲»ÉÙ´úÂ룬ËüÊÇÓÃ!-fÀ´ÅжÏÇëÇóµÄÎļþÔÚÎļþϵͳÉÏ´æ²»´æÔÚ£¬²»´æÔÚ¾Íproxy_passµ½ºó¶Ë£¬·µ»ØͬÑùÊÇÓÃproxy_store±£´æ¡£

Á½ÖÖ´«Í³»º´æ¶¼ÓÐ×Å»ù±¾Ò»ÑùµÄÓŵãºÍȱµã£º
ȱµã1£º²»Ö§³Ö´ø²ÎÊýµÄ¶¯Ì¬Á´½Ó£¬±ÈÈçread.php?id=1£¬ÒòΪnginxÖ»±£´æÎļþÃû£¬ËùÒÔÕâ¸öÁ´½ÓÖ»ÔÚÎļþϵͳϱ£´æΪread.php£¬ÕâÑùÓû§·ÃÎÊread.php?id=2ʱ»á·µ»Ø²»ÕýÈ·µÄ½á¹û¡£Í¬Ê±²»Ö§³Öhttp://www.sudone.com/ÕâÖÖÐÎʽµÄÊ×Ò³ºÍ¶þ¼¶Ä¿Â¼http://www.sudone.com/download/£¬ÒòΪnginx·Ç³£ÀÏʵ£¬»á½«ÕâÑùµÄÇëÇóÕÕÁ´½ÓдÈëÎļþϵͳ£¬¶øÕâ¸öÁ´½ÓÏÔÈ»ÊÇÒ»¸öĿ¼£¬ËùÒÔ±£´æʧ°Ü¡£ÕâЩÇé¿ö¶¼ÐèҪдrewrite²ÅÄÜÕýÈ·±£´æ¡£
ȱµã2£ºnginxÄÚ²¿Ã»Óлº´æ¹ýÆÚºÍÇåÀíµÄÈκλúÖÆ£¬ÕâЩ»º´æµÄÎļþ»áÓÀ¾ÃÐԵر£´æÔÚ»úÆ÷ÉÏ£¬Èç¹ûÒª»º´æµÄ¶«Î÷·Ç³£¶à£¬ÄǾͻá³Å±©Õû¸öÓ²Å̿ռ䡣Ϊ´Ë¿ÉÒÔʹÓÃÒ»¸öshell½Å±¾¶¨ÆÚÇåÀí£¬Í¬Ê±¿ÉÒÔ׫дphpµÈ¶¯Ì¬³ÌÐòÀ´×öʵʱ¸üС£
ȱµã3£ºÖ»ÄÜ»º´æ200״̬Â룬Òò´Ëºó¶Ë·µ»Ø301/302/404µÈ״̬Â붼²»»á»º´æ£¬¼ÙÈçÇ¡ºÃÓÐÒ»¸ö·ÃÎÊÁ¿ºÜ´óµÄα¾²Ì¬Á´½Ó±»É¾³ý£¬ÄǾͻ᲻ͣ´©Í¸µ¼Öºó¶Ë³ÐÔز»Ð¡Ñ¹Á¦¡£
ȱµã4£ºnginx²»»á×Ô¶¯Ñ¡ÔñÄÚ´æ»òÓ²ÅÌ×÷Ϊ´æ´¢½éÖÊ£¬Ò»ÇÐÓÉÅäÖþö¶¨£¬µ±È»ÔÚµ±Ç°µÄ²Ù×÷ϵͳÀﶼ»áÓвÙ×÷ϵͳ¼¶µÄÎļþ»º´æ»úÖÆ£¬ËùÒÔ´æÔÚÓ²ÅÌÉÏÒ²²»ÐèÒª¹ý·Öµ£ÐÄ´ó²¢·¢¶ÁÈ¡Ôì³ÉµÄioÐÔÄÜÎÊÌâ¡£

nginx´«Í³»º´æµÄȱµãÒ²ÊÇËüºÍsquidµÈ»º´æÈí¼þµÄ²»Í¬Ö®ÌØÉ«£¬ËùÒÔÒ²¿É¿´×÷ÆäÓŵ㡣ÔÚÉú²úÓ¦ÓÃÖÐËü³£³£ÓÃ×÷ºÍsquidµÄ´îµµ£¬squid¶ÔÓÚ´ø?µÄÁ´½ÓÍùÍùÎÞ·¨×èµ²£¬¶ønginxÄܽ«Æä·ÃÎÊÀ¹×¡£¬ÀýÈ磺http://sudone.com/?ºÍhttp://sudone.com/ÔÚsquidÉϻᱻµ±×öÁ½¸öÁ´½Ó£¬ËùÒÔ»áÔì³ÉÁ½´Î´©Í¸£»¶ønginxÖ»»á±£´æÒ»´Î£¬ÎÞÂÛÁ´½Ó±ä³Éhttp://sudone.com/?1»¹ÊÇhttp://sudone.com/?123£¬¾ù²»ÄÜ͸¹ýnginx»º´æ£¬´Ó¶øÓÐЧµØ±£»¤Á˺ó¶ËÖ÷»ú¡£

nginx»á·Ç³£ÀÏʵµØ½«Á´½ÓÐÎʽ±£´æµ½ÎļþϵͳÖУ¬ÕâÑù¶ÔÓÚÒ»¸öÁ´½Ó£¬¿ÉÒԺܷ½±ãµØ²éÔÄËüÔÚ»º´æ»úÆ÷ÉϵĻº´æ״̬ºÍÄÚÈÝ£¬Ò²¿ÉÒԺܷ½±ãµØºÍ±ðµÄÎļþ¹ÜÀíÆ÷ÈçrsyncµÈÅäºÏʹÓã¬ËüÍêÍêÈ«È«¾ÍÊÇÒ»¸öÎļþϵͳ½á¹¹¡£

ÕâÁ½ÖÖ´«Í³»º´æ¶¼¿ÉÒÔÔÚlinuxϽ«Îļþ±£´æµ½/dev/shmÀһ°ãÎÒÒ²ÊÇÕâô×öµÄ£¬ÕâÑù¿ÉÒÔÀûÓÃϵͳÄÚ´æÀ´×ö»º´æ£¬ÀûÓÃÄÚ´æµÄ»°£¬ÇåÀí¹ýÆÚÄÚÈÝËٶȾͻá¿ìµÃ¶à¡£Ê¹ÓÃ/dev/shm/ʱ³ýÁËÒª°ÑtmpĿ¼ҲָÏòµ½/dev/shmÕâ¸ö·ÖÇøÍ⣬Èç¹ûÓдóÁ¿Ð¡ÎļþºÍĿ¼£¬»¹ÒªÐÞ¸ÄÒ»ÏÂÕâ¸öÄÚ´æ·ÖÇøµÄinodeÊýÁ¿ºÍ×î´óÈÝÁ¿£º

mount -o size=2500M -o nr_inodes=480000 -o noatime,nodiratime -o remount /dev/shm

ÉÏÃæµÄÃüÁîÔÚһ̨ÓÐ3GÄÚ´æµÄ»úÆ÷ÉÏʹÓã¬ÒòΪ/dev/shmĬÈÏ×î´óÄÚ´æÊÇϵͳÄÚ´æµÄÒ»°ë¾ÍÊÇ1500M£¬ÕâÌõÃüÁÆäµ÷´ó³É2500M£¬Í¬Ê±shmϵͳinodeÊýÁ¿Ä¬ÈÏÇé¿öÏ¿ÉÄÜÊDz»¹»Óõģ¬µ«ÓÐȤµÄÊÇËü¿ÉÒÔËæÒâµ÷½Ú£¬ÕâÀïµ÷½ÚΪ480000±£ÊØÁ˵㣬µ«Ò²»ù±¾¹»ÓÃÁË¡£

3¡¢»ùÓÚmemcachedµÄ»º´æ

nginx¶ÔmemcachedÓÐËùÖ§³Ö£¬µ«Êǹ¦Äܲ¢²»ÊÇÌرð֮ǿ£¬ÐÔÄÜÉÏ»¹ÊǷdz£Ö®ÓÅÐã¡£

location /mem/ {
    if ( $uri ~ "^/mem/([0-9A-Za-z_]*)$" )
    {
     set $memcached_key "$1";
     memcached_pass     192.168.1.2:11211;
    }
    expires 70;
}

Õâ¸öÅäÖûὫhttp://sudone.com/mem/abcÖ¸Ã÷µ½memcachedµÄabcÕâ¸ökeyȥȡÊý¾Ý¡£

nginxĿǰûÓÐдÈëmemcachedµÄÈκλúÖÆ£¬ËùÒÔÒªÍùmemcachedÀïдÈëÊý¾ÝµÃÓúǫ́µÄ¶¯Ì¬ÓïÑÔÍê³É£¬¿ÉÒÔÀûÓÃ404¶¨Ïòµ½ºó¶ËȥдÈëÊý¾Ý¡£

4¡¢»ùÓÚµÚÈý·½²å¼þncache

ncacheÊÇÐÂÀËÐֵܿª·¢µÄÒ»¸ö²»´íµÄÏîÄ¿£¬ËüÀûÓÃnginxºÍmemcachedʵÏÖÁËÒ»²¿·ÖÀàËÆsquid»º´æµÄ¹¦ÄÜ£¬ÎÒ²¢Ã»ÓÐʹÓÃÕâ¸ö²å¼þµÄ¾­Ñ飬¿ÉÒԲο¼£º

http://code.google.com/p/ncache/

5¡¢nginxпª·¢µÄproxy_cache¹¦ÄÜ

´Ónginx-0.7.44°æ¿ªÊ¼£¬nginxÖ§³ÖÁËÀàËÆsquid½ÏΪÕý¹æµÄcache¹¦ÄÜ£¬Ä¿Ç°»¹´¦ÓÚ¿ª·¢½×¶Î£¬Ö§³ÖÏ൱ÓÐÏÞ£¬Õâ¸ö»º´æÊÇ°ÑÁ´½ÓÓÃmd5±àÂëhashºó±£´æ£¬ËùÒÔËü¿ÉÒÔÖ§³ÖÈÎÒâÁ´½Ó£¬Í¬Ê±Ò²Ö§³Ö404/301/302ÕâÑùµÄ·Ç200״̬¡£

ÅäÖãº

Ê×ÏÈÅäÖÃÒ»¸öcache¿Õ¼ä£º

proxy_cache_path /path/to/cache levels=1:2 keys_zone=NAME:10m inactive=5m max_size=2m clean_time=1m;

×¢ÒâÕâ¸öÅäÖÃÊÇÔÚserver±êÇ©Í⣬levelsÖ¸¶¨¸Ã»º´æ¿Õ¼äÓÐÁ½²ãhashĿ¼£¬µÚÒ»²ãĿ¼ÊÇ1¸ö×Öĸ£¬µÚ¶þ²ãΪ2¸ö×Öĸ£¬±£´æµÄÎļþÃû¾Í»áÀàËÆ/path/to/cache/c/29/b7f54b2df7773722d382f4809d65029c£»keys_zoneΪÕâ¸ö¿Õ¼äÆð¸öÃû×Ö£¬10mÖ¸¿Õ¼ä´óСΪ10MB£»inactiveµÄ5mÖ¸»º´æĬÈÏʱ³¤5·ÖÖÓ£»max_sizeµÄ2mÊÇÖ¸µ¥¸öÎļþ³¬¹ý2mµÄ¾Í²»»º´æ£»clean_timeÖ¸¶¨Ò»·ÖÖÓÇåÀíÒ»´Î»º´æ¡£

location / {
    proxy_pass http://www.sudone.com/;

    proxy_cache NAME;#ʹÓÃNAMEÕâ¸ökeys_zone

    proxy_cache_valid 200 302 1h;#200ºÍ302״̬Âë±£´æ1Сʱ
    proxy_cache_valid 301 1d;#301״̬Âë±£´æÒ»Ìì
    proxy_cache_valid any 1m;#ÆäËüµÄ±£´æÒ»·ÖÖÓ
}

ps£ºÖ§³ÖcacheµÄ0.7.44µ½0.7.51Õ⼸¸ö°æ±¾µÄÎȶ¨ÐÔ¾ùÓÐÎÊÌ⣬·ÃÎÊÓÐЩÁ´½Ó»á³öÏÖ´íÎó£¬ËùÒÔÕ⼸¸ö°æ±¾×îºÃ²»ÒªÔÚÉú²ú»·¾³ÖÐʹÓá£nginx-0.7ÏÂÄ¿Ç°ËùÖª½ÏΪÎȶ¨µÄ°æ±¾ÊÇ0.7.39¡£Îȶ¨°æ0.6.36°æÒ²ÊǽüÆÚ¸üУ¬Èç¹ûÔÚÅäÖÃÀïûÓÐʹÓõ½0.7µÄһЩбêǩй¦ÄÜ£¬Ò²¿ÉÒÔʹÓÃ0.6.36°æ¡£

(ÔðÈα༭£ºadmin)
Ö¯ÃζþάÂëÉú³ÉÆ÷
¶¥Ò»ÏÂ
(0)
0%
²ÈÒ»ÏÂ
(0)
0%
------·Ö¸ôÏß----------------------------
·¢±íÆÀÂÛ
Çë×Ô¾õ×ñÊØ»¥ÁªÍøÏà¹ØµÄÕþ²ß·¨¹æ£¬ÑϽû·¢²¼É«Çé¡¢±©Á¦¡¢·´¶¯µÄÑÔÂÛ¡£
ÆÀ¼Û:
񡀂:
Óû§Ãû: ÑéÖ¤Âë:µã»÷ÎÒ¸ü»»Í¼Æ¬