NginxÖض¨ÏòÉèÖý̳̣¬ÊµÏÖURLת·¢ºÍץȡ
nginxÖض¨ÏòÉèÖý̳̣¬ÊµÏÖurlת·¢ºÍץȡ
NginxÊÇÒ»¿î¸ßÐÔÄܵĿªÔ´WebЧÀÍÆ÷£¬Ò²¿ÉÒÔÓÃÀ´ÊµÏÖ·´ÏòÊðÀí¡¢¸ºÔØƽºâÒÔ¼°URLÖض¨ÏòµÈ¹¦Ð§¡£ÔÚ±¾ÆªÎÄÕÂÖУ¬ÎÒÃǽ«ÏÈÈÝÔõÑùͨ¹ýNginxÉèÖÃʵÏÖURLÖض¨ÏòºÍץȡµÄ¹¦Ð§£¬²¢ÇÒÌṩÏà¹ØµÄ´úÂëʾÀý¡£
Ò»¡¢URLת·¢
URLת·¢ÊÇÖ¸½«Ò»¸öURLÇëÇóת·¢µ½ÁíÒ»¸öURLµØµãÉÏ¡£ÔÚNginxÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÉèÖÃÀ´ÊµÏÖURLµÄת·¢¹¦Ð§¡£ÒÔÏÂÊÇһЩ³£¼ûµÄURLת·¢Ê¾Àý£º
ÇëÇóת·¢
µ±Óû§ÇëÇó»á¼ûÒ»¸öÌض¨µÄURLʱ£¬ÎÒÃÇ¿ÉÒÔ½«ÇëÇóת·¢µ½Ö¸¶¨µÄURLÉÏ¡£ÏÂÃæÊÇÒ»¸öʾÀýÉèÖãº
server { listen 80; server_name www.example.com; location / { proxy_pass http://www.new-example.com; } }
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
Õâ¸öÉèÖý«»á°ÑÓû§ÇëÇóת·¢µ½http://www.new-example.comÉÏ¡£
»ùÓÚ·¾¶µÄת·¢
µ±Óû§µÄÇëÇ󷾶ƥÅäµ½Ö¸¶¨µÄ¹æÔòʱ£¬ÎÒÃÇ¿ÉÒÔ½«ÇëÇóת·¢µ½ÁíÒ»¸öURLµØµã¡£ÒÔÏÂÊÇÒ»¸öʾÀýÉèÖãº
server { listen 80; server_name www.example.com; location /old { rewrite ^/old/(.*)$ /new/$1 break; proxy_pass http://www.new-example.com; } }
µÇ¼ºó¸´ÖÆ
Õâ¸öÉèÖý«»á°ÑÓû§ÇëÇ󷾶ÖÐ/oldµÄ²¿·ÖÌ滻Ϊ/new£¬È»ºóÔÙ½«ÇëÇóת·¢µ½http://www.new-example.comÉÏ¡£
»ùÓÚÓòÃûµÄת·¢
µ±Óû§ÇëÇó»á¼ûÒ»¸öÌض¨µÄÓòÃûʱ£¬ÎÒÃÇ¿ÉÒÔ½«ÇëÇóת·¢µ½ÁíÒ»¸öURLµØµã¡£ÒÔÏÂÊÇÒ»¸öʾÀýÉèÖãº
server { listen 80; server_name www.example.com; location / { proxy_pass http://www.new-example.com; } }
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
Õâ¸öÉèÖý«»á°ÑÀ´×Ôwww.example.comÓòÃûµÄÇëÇóת·¢µ½http://www.new-example.comÉÏ¡£
ÒÔÉÏÊÇһЩ³£¼ûµÄURLת·¢Ê¾Àý£¬Äã¿ÉÒÔƾ֤ÏêϸÐèÇó¾ÙÐÐÉèÖá£
¶þ¡¢URLץȡ
URLץȡÊÇָͨ¹ýNginx½«Óû§ÇëÇóµÄURLÄÚÈÝץȡÏÂÀ´²¢»º´æÆðÀ´£¬ÒÔÌá¸ß»á¼ûËÙÂÊ¡£ÒÔÏÂÊÇÒ»¸öʾÀýÉèÖãº
http { # ½ç˵»º´æĿ¼ proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m; server { listen 80; server_name www.example.com; location / { # »º´æÉèÖà proxy_cache my_cache; proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m; # ץȡÉèÖà proxy_cache_use_stale updating; proxy_cache_lock on; # ץȡÄÚÈÝ proxy_pass http://www.news-example.com; } } }
µÇ¼ºó¸´ÖÆ
Õâ¸öÉèÖý«»áδÀ´×Ôwww.example.comµÄÇëÇóץȡÏÂÀ´£¬²¢´æ´¢ÔÚ·¾¶Îª/path/to/cacheµÄĿ¼ÖС£µ±Óû§Ôٴλá¼ûÏàͬµÄURLʱ£¬Nginx½«Ö±½Ó´Ó»º´æÖжÁÈ¡ÄÚÈÝ£¬Ìá¸ßÁË»á¼ûËÙÂÊ¡£
×¢ÖØ£ºÒÔÉÏÖ»ÊÇÒ»¸öʾÀýÉèÖã¬Äã¿ÉÒÔƾ֤ÏÖʵÐèÇó¾ÙÐе÷½â¡£
×ܽá
±¾ÎÄÏÈÈÝÁËÔõÑùͨ¹ýNginxÉèÖÃʵÏÖURLת·¢ºÍץȡµÄ¹¦Ð§£¬²¢ÌṩÁËÏà¹ØµÄ´úÂëʾÀý¡£Í¨¹ýÎÞаµÄNginxÉèÖã¬ÎÒÃÇ¿ÉÒÔʵÏÖÖÖÖÖURLת·¢ºÍץȡµÄÐèÇó£¬Ìá¸ßWebÓ¦ÓõÄÐÔÄܺÍÎÞаÐÔ¡£Ï£Íû±¾ÎĶÔÄãÓÐËù×ÊÖú¡£ÈôÊÇÓÐÈκÎÎÊÌ⣬ÇëÔÚ̸ÂÛÇøÁôÑÔ¡£
ÒÔÉϾÍÊÇNginxÖض¨ÏòÉèÖý̳̣¬ÊµÏÖURLת·¢ºÍץȡµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡