NginxÖض¨ÏòÉèÖÃʵս£¬ÊµÏÖÍøÕ¾Á÷Á¿×ªÒÆ
nginxÖض¨ÏòÉèÖÃʵս£¬ÊµÏÖÍøÕ¾Á÷Á¿×ªÒÆ
½üÄêÀ´£¬Ëæ×Å»¥ÁªÍøµÄѸÃÍÉú³¤£¬ÍøÕ¾Á÷Á¿×ªÒƱäµÃÔ½À´Ô½³£¼û¡£µ±ÎÒÃÇÐèÒª½«Ò»¸öÍøÕ¾µÄÁ÷Á¿Öض¨Ïòµ½ÁíÒ»¸öÍøվʱ£¬NginxµÄÖض¨ÏòÉèÖÃÄܹ»ºÜºÃµØÖª×ã×ðÁú¿Ê±ÐèÇó¡£±¾ÎĽ«´Óʵս½Ç¶È³ö·¢£¬ÏÈÈÝÔõÑùʹÓÃNginxµÄÖض¨ÏòÉèÖÃʵÏÖÍøÕ¾Á÷Á¿×ªÒƵÄÒªÁ죬²¢Ìṩ´úÂëʾÀý¡£
µ±ÎÒÃÇÐèÒª½«Ò»¸ö¾ÉÍøÕ¾µÄÁ÷Á¿×ªÒƵ½Ò»¸öÐÂÍøվʱ£¬¿ÉÒÔͨ¹ýNginxµÄÖض¨ÏòÉèÖÃÀ´ÊµÏÖÕâһĿµÄ¡£ÏÂÃæÊÇÒ»¸ö»ù±¾µÄNginxÖض¨ÏòÉèÖÃʾÀý£¬ÔÚÉèÖÃÎļþÖÐÌí¼ÓÒÔÏÂÄÚÈÝ£º
server { listen 80; server_name oldsite.com; return 301 http://newsite.com$request_uri; }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÉèÖý«»á½«ËùÓÐÀ´×Ôoldsite.comµÄÁ÷Á¿Öض¨Ïòµ½newsite.com¡£ÆäÖУ¬return 301ÌåÏÖÖ´ÐÐ301Öض¨Ïò£¬http://newsite.com$request_uriÌåÏÖÖض¨Ïòµ½http://newsite.com²¢±£´æÔʼÇëÇóµÄURI¡£
³ýÁË»ù±¾µÄÖض¨ÏòÉèÖã¬Nginx»¹ÌṩÁ˸ü¶à¸ß¼¶µÄÖض¨ÏòÉèÖÃÑ¡Ïî¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓÃÕýÔò±í´ïʽÀ´ÊµÏÖ¸üÖØ´óµÄÖض¨Ïò¹æÔò¡£ÏÂÃæÊÇÒ»¸öʹÓÃÕýÔò±í´ïʽµÄÖض¨ÏòÉèÖÃʾÀý£º
server { listen 80; server_name oldsite.com; rewrite ^/(.*)$ http://newsite.com/$1 permanent; }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÉèÖý«»áδÀ´×Ôoldsite.comµÄËùÓÐÁ÷Á¿Öض¨Ïòµ½newsite.com£¬²¢±£´æÔʼÇëÇóµÄURI¡£
³ýÁËÕë¶ÔÕû¸öÍøÕ¾µÄÁ÷Á¿Öض¨Ïò£¬ÎÒÃÇ»¹¿ÉÒÔÕë¶ÔÌض¨µÄURL¾ÙÐÐÖض¨Ïò¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔ½«oldsite.com/aboutÒ³ÃæµÄÁ÷Á¿Öض¨Ïòµ½newsite.com/aboutÒ³Ãæ¡£ÏÂÃæÊÇÒ»¸öÕë¶ÔÌض¨URLµÄÖض¨ÏòÉèÖÃʾÀý£º
location /about { rewrite ^ http://newsite.com/about permanent; }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÉèÖý«»áδÀ´×Ôoldsite.com/aboutÒ³ÃæµÄÁ÷Á¿Öض¨Ïòµ½newsite.com/aboutÒ³Ãæ¡£
³ýÁËÉÏÊöµÄ»ù±¾Ó÷¨£¬Nginx»¹ÌṩÁ˸ü¶à¸ß¼¶µÄÖض¨ÏòÉèÖÃÑ¡ÏÀýÈçÖض¨Ïòµ½Ö¸¶¨URLµÄÖ¸¶¨Î»ÖᢴøÓÐÕýÔò±í´ïʽµÄ¸ß¼¶Æ¥ÅäµÈ¡£ÕâЩѡÏî¿ÉÒÔÈÃÎÒÃÇÔ½·¢ÎÞаµØÉèÖÃÁ÷Á¿Öض¨Ïò¹æÔò¡£
ÐèҪעÖصÄÊÇ£¬µ±ÎÒÃÇÉèÖÃÁËÖض¨Ïò¹æÔòʱ£¬Ó¦µ±ÉóÉ÷µØ²âÊÔºÍÑéÖ¤ÕâЩ¹æÔòÊÇ·ñƾ֤ԤÆÚÊÂÇé¡£ÔÚÉèÖÃÎļþÖÐÌí¼ÓÒ»ÌõÖض¨Ïò¹æÔòʱ£¬Ó¦µ±ÖØмÓÔØNginxÉèÖÃÎļþÒÔʹÉèÖÃÉúЧ£º
sudo nginx -s reload
µÇ¼ºó¸´ÖÆ
ÔÚʹÓÃNginxµÄÖض¨ÏòÉèÖþÙÐÐÍøÕ¾Á÷Á¿×ªÒÆʱ£¬ÎÒÃÇ¿ÉÒÔƾ֤ÏÖʵÐèÇóÑ¡ÔñÊʺϵÄÖض¨Ïò¹æÔò£¬²¢Í¨¹ý²âÊÔÈ·±£¹æÔòµÄ׼ȷÐÔ¡£±ðµÄ£¬Ó¦µ±¼á³ÖNginxµÄ°æ±¾Óë×îеÄÎȹ̰汾¼á³ÖÒ»Ö£¬ÒÔ×èÖ¹·ºÆðÒÑÖªµÄÎó²îºÍÇå¾²ÎÊÌâ¡£
×ܽáÆðÀ´£¬NginxµÄÖض¨ÏòÉèÖÃÄܹ»ºÜºÃµØʵÏÖÍøÕ¾Á÷Á¿×ªÒÆ¡£Í¨¹ýºÏÀíÉèÖÃÖض¨Ïò¹æÔò£¬ÎÒÃÇ¿ÉÒÔ½«Ò»¸öÍøÕ¾µÄÁ÷Á¿ÓÐÑ¡ÔñµØÖض¨Ïòµ½ÁíÒ»¸öÍøÕ¾£¬ÊµÏÖÁ÷Á¿µÄתÒƺÍÌøת¡£Í¨¹ý±¾ÎÄÌṩµÄʾÀý´úÂëºÍʵսÂÄÀú£¬¶ÁÕß¿ÉÒÔ¸üºÃµØÃ÷È·ºÍÔËÓÃNginxµÄÖض¨Ïò¹¦Ð§¡£
ÒÔÉϾÍÊÇNginxÖض¨ÏòÉèÖÃʵս£¬ÊµÏÖÍøÕ¾Á÷Á¿×ªÒƵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡