×ðÁú¿­Ê±

ÔõÑùʹÓÃNginx¾ÙÐÐHTTPÇëÇóµÄ»º´æ¿ØÖÆ

ÔõÑùʹÓÃnginx¾ÙÐÐhttpÇëÇóµÄ»º´æ¿ØÖÆ

HTTPÇëÇóµÄ»º´æ¿ØÖÆÊÇÒ»ÖÖÓÅ»¯ÍøÕ¾ÐÔÄܵÄÖ÷ÒªÊֶΣ¬Ëü¿ÉÒÔïÔ̭ЧÀÍÆ÷´¦Öóͷ£ÇëÇóµÄ´ÎÊý£¬Ìá¸ßÍøÕ¾µÄÏìÓ¦ËÙÂÊ¡£Nginx×÷Ϊһ¿î¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬ÌṩÁËÎÞаµÄ»º´æ¿ØÖƹ¦Ð§£¬±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃnginx¾ÙÐÐhttpÇëÇóµÄ»º´æ¿ØÖÆ¡£

Ò»¡¢Ê¹ÓÃÊðÀí»º´æ

NginxÌṩÁËÊðÀí»º´æµÄ¹¦Ð§£¬Äܹ»»º´æÀ´×ÔÉÏÓÎЧÀÍÆ÷µÄÏìӦЧ¹û£¬ïÔÌ­¶ÔÉÏÓÎЧÀÍÆ÷µÄÇëÇó´ÎÊý¡£ÒªÊ¹ÓÃÊðÀí»º´æ£¬¿ÉÒÔÔÚNginxµÄÉèÖÃÎļþÖÐÌí¼ÓÒÔÏÂÉèÖãº

http {
  proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;

  server {
    ...
    location / {
      proxy_cache my_cache;
      proxy_cache_key $host$uri$is_args$args;
      proxy_cache_valid 200 302 10m;
      proxy_cache_valid 404 1m;
      proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
      proxy_ignore_headers Cache-Control;
      proxy_ignore_headers Set-Cookie;
      proxy_hide_header Set-Cookie;
      proxy_pass http://upstream_server;
    }
    ...
  }
}

µÇ¼ºó¸´ÖÆ

ÉÏÊöÉèÖÃÖУ¬proxy_cache_pathÓÃÓÚÉèÖûº´æµÄ·¾¶ºÍÏà¹Ø²ÎÊý¡£levels=1:2ÌåÏÖÔÚ»º´æ·¾¶Öн¨Éè1¼¶Ä¿Â¼ºÍ2¼¶Ä¿Â¼£¬ÒÔÌá¸ßЧÂÊ¡£keys_zoneÓÃÓÚÉèÖûº´æÇøµÄÃû³ÆºÍÄÚ´æ¾Þϸ£¬¿ÉÒÔƾ֤ÏÖʵÐèÒª¾ÙÐе÷½â¡£max_sizeÌåÏÖ»º´æÇøµÄ×î´óÈÝÁ¿£¬inactiveÌåÏÖ»º´æµÄÓâÆÚʱ¼ä£¬¼´60·ÖÖÓÄÚûÓб»»á¼ûµÄ»º´æ½«±»É¾³ý¡£use_temp_path=offÌåÏÖ½ûÓÃÔÝʱ·¾¶£¬¿ÉÒÔÌá¸ßÐÔÄÜ¡£

ÔÚÏêϸµÄЧÀÍÆ÷ÉèÖÃÖУ¬Í¨¹ýlocationÖ¸ÁîÖ¸¶¨ÐèÒª¾ÙÐлº´æµÄURL¡£proxy_cacheÖ¸ÁîÌåÏÖÆôÓûº´æ£¬proxy_cache_keyÖ¸ÁîÖ¸¶¨»º´æµÄ¼üÖµ£¬¿ÉÒÔʹÓöà¸ö±äÁ¿Æ´½Ó³É»º´æ¼üÖµ¡£proxy_cache_validÖ¸¶¨Á˲î±ðHTTP״̬ÂëµÄ»º´æÓÐÓÃÆÚ£¬Èç200ºÍ302״̬ÂëµÄÏìӦЧ¹ûÔÚ10·ÖÖÓÄÚÓÐÓã¬404״̬ÂëµÄÏìӦЧ¹ûÔÚ1·ÖÖÓÄÚÓÐÓá£proxy_cache_use_staleÓÃÓÚÖ¸¶¨µ±ÉÏÓÎЧÀÍÆ÷·ºÆð¹ýʧ¡¢³¬Ê±»ò¸üÐÂʱ£¬ÊÇ·ñʹÓÃÓâÆڵĻº´æ¡£proxy_ignore_headersºÍproxy_hide_headerÖ¸Áî¿ÉÓÃÓÚºöÂÔ»òÒþ²ØÏìӦͷÖеÄijЩÊôÐÔ¡£

ÔÚÉèÖÃÍê³Éºó£¬ÖØÆôNginxЧÀÍʹÉèÖÃÉúЧ¡£´Ëʱ£¬Nginx½«»á¶ÔÆ¥ÅäµÄURL¾ÙÐлº´æ£¬ÏàͬµÄURLÔٴα»ÇëÇóʱ£¬½«»áÖ±½Ó´Ó»º´æÖлñÈ¡ÏìӦЧ¹û£¬¶ø²»ÐèÒªÔÙ´ÎÇëÇóÉÏÓÎЧÀÍÆ÷¡£

¶þ¡¢Ê¹ÓÃä¯ÀÀÆ÷»º´æ

³ýÁËÊðÀí»º´æ£¬»¹¿ÉÒÔʹÓÃä¯ÀÀÆ÷»º´æÀ´ïÔÌ­ÍøÂçÇëÇó¡£Nginx¿ÉÒÔͨ¹ýÉèÖÃÏìӦͷÖеÄCache-ControlºÍExpiresÀ´¿ØÖÆä¯ÀÀÆ÷»º´æµÄÐÐΪ¡£

ʾÀýÈçÏ£º

http {
  ...
  server {
    ...
    location /static/ {
      expires max;
      add_header Cache-Control public;
    }
    ...
  }
}

µÇ¼ºó¸´ÖÆ

ÉÏÊöÉèÖÃÖУ¬expiresÖ¸ÁîÉèÖÃÁËmax£¬ÌåÏÖ½«ÏìӦЧ¹ûµÄÓâÆÚʱ¼äÉèÖÃΪ×î´óÖµ£¬¼´ÓÀ²»¹ýÆÚ¡£add_headerÖ¸ÁîΪÏìӦЧ¹ûÌí¼ÓÁËCache-ControlÍ·£¬²¢ÉèÖÃΪpublic£¬ÌåÏÖÔÊÐí¹«¹²»º´æ¡£

ÔÚÏêϸµÄURLÆ¥Åä¹æÔòÖУ¬¿ÉÒÔƾ֤²î±ðµÄÐèÇóÉèÖòî±ðµÄ»º´æÕ½ÂÔ¡£ºÃ±È£¬¾²Ì¬×ÊԴͨ³£²»»á¾­³£±¬·¢¸Ä±ä£¬¿ÉÒÔÉèÖÃexpiresΪ½Ï³¤µÄʱ¼ä£¬ÈÃä¯ÀÀÆ÷»º´æ×ÊÔ´ £»¶ø¶¯Ì¬ÌìÉúµÄÒ³Ãæ¿ÉÒÔÉèÖÃΪ²»»º´æ»ò»º´æʱ¼ä½Ï¶Ì¡£

Èý¡¢Ê¹ÓÃÌõ¼þ»º´æ

Ìõ¼þ»º´æÊÇÒ»ÖÖÔÚ¿Í»§¶ËºÍЧÀÍÆ÷Ö®¼ä¾ÙÐÐͨѶµÄ»úÖÆ£¬¿ÉÒÔƾ֤ÇëÇóµÄÌõ¼þ¾öÒéÊÇ·ñʹÓûº´æ¡£Nginxͨ¹ýÉèÖÃÏìӦͷÖеÄLast-ModifiedºÍETag£¬ÒÔ¼°ÇëÇóÍ·ÖеÄIf-Modified-SinceºÍIf-None-MatchÀ´ÊµÏÖÌõ¼þ»º´æ¡£

ʾÀýÈçÏ£º

http {
  ...
  server {
    ...
    location / {
      if_modified_since before;
      add_header ETag "123456";
      if_none_match $http_if_none_match;
      if_modified_since off;
      ...
    }
    ...
  }
}

µÇ¼ºó¸´ÖÆ

ÉÏÊöÉèÖÃÖУ¬if_modified_sinceÖ¸ÁîÓÃÓÚÅжÏÇëÇóÍ·ÖеÄIf-Modified-SinceÊÇ·ñÔçÓÚЧÀÍÆ÷ÉèÖõÄLast-Modified £»add_headerÖ¸ÁîÌí¼ÓÁËETagÍ·£¬ÓÃÓÚ±êʶ×ÊÔ´µÄΨһÐÔ £»if_none_matchÖ¸ÁîÓÃÓÚÅжÏÇëÇóÍ·ÖеÄIf-None-MatchÊÇ·ñÓëЧÀÍÆ÷ÉèÖõÄETagÏàÆ¥Åä £»if_modified_sinceºÍif_none_matchÖ¸Áî»®·Ö¶ÔÓ¦ÁËIf-Modified-SinceºÍIf-None-MatchÇëÇóÍ·µÄÖµ¡£

ͨ¹ýÉèÖÃÌõ¼þ»º´æ£¬¿ÉÒÔÔÚ¿Í»§¶Ë·¢ËÍÇëÇóʱ£¬Æ¾Ö¤Ð§ÀÍÆ÷·µ»ØµÄLast-ModifiedºÍETagÅжÏÊÇ·ñʹÓûº´æ¡£ÈôÊÇ×ÊԴûÓб¬·¢×ª±ä£¬Ð§ÀÍÆ÷¿ÉÒÔ·µ»Ø304 Not Modified£¬¿Í»§¶Ë´Ó»º´æÖлñÈ¡×ÊÔ´ £»ÈôÊÇ×ÊÔ´ÒѾ­±¬·¢×ª±ä£¬Ð§ÀÍÆ÷·µ»ØеÄ×ÊÔ´¡£

ËÄ¡¢»º´æÕ½ÂÔ

ΪÁ˸üºÃµØ¿ØÖÆ»º´æµÄÐÐΪ£¬¿ÉÒÔƾ֤²î±ðµÄURLÉèÖòî±ðµÄ»º´æÕ½ÂÔ¡£Í¨³££¬¾²Ì¬×ÊÔ´µÄURL¾ßÓÐÎȹ̵ÄÌص㣬¿ÉÒÔÉèÖýϳ¤Ê±¼äµÄ»º´æʧЧÆÚ £»¶ø¶¯Ì¬Ò³ÃæµÄURL¿ÉÄÜ»áƵÈԱ任£¬¿ÉÒÔÉèÖý϶̵Ļº´æʧЧÆÚ¡£

ʾÀýÈçÏ£º

http {
  ...
  server {
    ...
    location /static/ {
      expires 7d;
      add_header Cache-Control public;
    }

    location /dynamic/ {
      expires 1h;
      add_header Cache-Control no-cache;
    }
    ...
  }
}

µÇ¼ºó¸´ÖÆ

ÉÏÊöÉèÖÃÖУ¬ÒÔ/static/¿ªÍ·µÄURLÆ¥Å侲̬×ÊÔ´£¬ÉèÖÃÁËÓâÆÚʱ¼äΪ7Ì죬ÔÊÐí¹«¹²»º´æ £»ÒÔ/dynamic/¿ªÍ·µÄURLÆ¥Å䶯̬×ÊÔ´£¬ÉèÖÃÁËÓâÆÚʱ¼äΪ1Сʱ£¬²¢Õ¥È¡»º´æ¡£

ͨ¹ýºÏÀíµÄ»º´æÕ½ÂÔ£¬¿ÉÒÔÔÚÌá¸ßÍøÕ¾ÐÔÄܵÄͬʱ£¬È·±£Óû§»ñÈ¡µ½×îеÄ×ÊÔ´¡£

×ܽá

ʹÓÃNginx¾ÙÐÐHTTPÇëÇóµÄ»º´æ¿ØÖÆÊÇÒ»ÖÖÓÅ»¯ÍøÕ¾ÐÔÄܵÄÓÐÓÃÊֶΡ£Í¨¹ýÊðÀí»º´æ¡¢ä¯ÀÀÆ÷»º´æºÍÌõ¼þ»º´æ£¬¿ÉÒÔïÔÌ­¶ÔЧÀÍÆ÷µÄÇëÇó´ÎÊý£¬Ìá¸ßÍøÕ¾µÄÏìÓ¦ËÙÂÊ¡£ÔÚÏêϸµÄ»º´æÕ½ÂÔÖУ¬ÐèҪƾ֤²î±ðµÄURLÉèÖòî±ðµÄ»º´æʧЧÆÚ£¬ÒÔÌṩ¸üºÃµÄÓû§ÌåÑé¡£

²Î¿¼×ÊÁÏ£ºhttps://nginx.org/

ÒÔÉϾÍÊÇÔõÑùʹÓÃNginx¾ÙÐÐHTTPÇëÇóµÄ»º´æ¿ØÖƵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±ÊµÊ±ÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

13452372176

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
¡¾ÍøÕ¾µØͼ¡¿¡¾sitemap¡¿