ÔõÑùʹÓÃNginx¾ÙÐÐÇëÇóÖض¨ÏòºÍURLÖØд
ÔõÑùʹÓÃnginx¾ÙÐÐÇëÇóÖض¨ÏòºÍurlÖØд
Nginx×÷Ϊһ¿î¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬³ýÁËÌṩ»ù±¾µÄÇëÇó´¦Öóͷ£Í⣬»¹¿ÉÒÔʹÓÃÆäÇ¿Ê¢µÄÖض¨ÏòºÍURLÖØд¹¦Ð§¶ÔÇëÇó¾ÙÐнøÒ»²½´¦Öóͷ£¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃnginx¾ÙÐÐÇëÇóÖض¨ÏòºÍurlÖØд£¬²¢¸½ÉÏ´úÂëʾÀý¡£
ÇëÇóÖض¨Ïò
ÇëÇóÖض¨ÏòÊÇÖ¸ÔÚÎüÊÕµ½¿Í»§¶ËÇëÇóºó£¬½«ÇëÇóÖض¨Ïòµ½ÁíÒ»¸öURL¡£NginxÌṩÁËÁ½ÖÖ·½·¨ÊµÏÖÇëÇóÖض¨Ïò£ºÊ¹ÓÃreturnÖ¸ÁîºÍʹÓÃrewriteÖ¸Áî¡£
ʹÓÃreturnÖ¸Áî¾ÙÐÐÇëÇóÖض¨Ïò£¬Ê¾ÀýÈçÏ£º
server { listen 80; server_name oldsite.com; return 301 http://newsite.com$request_uri; }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖý«ËùÓÐÇëÇóת·¢µ½http://newsite.com£¬²¢±£´æÔʼURL¡£
ʹÓÃrewriteÖ¸Áî¾ÙÐÐÇëÇóÖض¨Ïò£¬Ê¾ÀýÈçÏ£º
server { listen 80; server_name oldsite.com; rewrite ^(.*)$ http://newsite.com$1 permanent; }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖý«ËùÓÐÇëÇóת·¢µ½http://newsite.com£¬²¢±£´æÔʼURL¡£Ê¹ÓÃpermanentÒªº¦×ÖÌåÏÖÖض¨ÏòÊÇÓÀÊÀÐԵģ¬ÈôÊÇҪʹÓÃÔÝʱÖض¨Ïò£¬¿ÉÒÔʹÓÃredirectÒªº¦×Ö¡£
URLÖØд
URLÖØдÊÇָͨ¹ýÐÞ¸ÄURLµÄ·½·¨£¬¶ÔÇëÇó¾ÙÐд¦Öóͷ£¡£NginxʹÓÃrewriteÖ¸ÁîÀ´ÊµÏÖURLÖØд¡£
ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄURLÖØдʾÀý£¬½«ËùÓÐÒÔ/user¿ªÍ·µÄURLÖØдΪ/user/profile£º
location / { rewrite ^/user(.*)$ /user/profile$1 last; }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖý«/user¿ªÍ·µÄURLÖØдΪ/user/profile£¬²¢½«ÖØдºóµÄURLת´ï¸øºóÐø´¦Öóͷ£¡£
³ýÁ˼òÆÓµÄURLÖØдÍ⣬Nginx»¹Ö§³ÖʹÓÃÕýÔò±í´ïʽ¾ÙÐиüÎÞаµÄURLÖØд¡£ÒÔÏÂÊÇÒ»¸öʹÓÃÕýÔò±í´ïʽµÄURLÖØдʾÀý£¬½«/user/123ÖØдΪ/user/profile?id=123£º
location / { rewrite ^/user/(d+)$ /user/profile?id=$1 last; }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÖУ¬(d+)ÌåÏÖÆ¥ÅäÒ»¸ö»ò¶à¸öÊý×Ö£¬²¢½«Æä×÷Ϊ²ÎÊýת´ï¸ø/user/profile¡£
ÐèҪעÖصÄÊÇ£¬URLÖØд¿ÉÄܻᵼÖÂÖض¨ÏòÑ»·£¬Òò´ËÐèÒªÉóÉ÷ÉèÖ᣿ÉÒÔʹÓÃif»òÕßreturnÖ¸ÁîÍŽáÌõ¼þÓï¾äÀ´×èÖ¹Öض¨ÏòÑ»·µÄ±¬·¢¡£
×ÛÉÏËùÊö£¬Ê¹ÓÃNginx¾ÙÐÐÇëÇóÖض¨ÏòºÍURLÖØд¿ÉÒÔÓÃÓÚʵÏÖһЩ³£¼ûµÄÐèÇó£¬Èç¾ÉÍøÖ·Ìøת¡¢Ò³ÃæÌøת¡¢URL²ÎÊý´¦Öóͷ£µÈ¡£Í¨¹ýºÏÀíµÄÉèÖ㬿ÉÒÔÌáÉýÍøÕ¾µÄ¿ÉÓÃÐÔºÍÓû§ÌåÑé¡£
Ï£Íû±¾ÎĶÔÄãÃ÷È·ºÍÓ¦ÓÃNginx¾ÙÐÐÇëÇóÖض¨ÏòºÍURLÖØдÓÐËù×ÊÖú¡£ÈôÊÇÄãÐèÒª¸üÏêϸµÄÉèÖúÍʾÀý£¬Çë²Î¿¼NginxµÄ¹Ù·½Îĵµ»òÕßÏà¹ØµÄÊÖÒÕÎÄÕ¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃNginx¾ÙÐÐÇëÇóÖض¨ÏòºÍURLÖØдµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡