×ðÁú¿­Ê±¹ÙÍøµÇ¼

NginxÔõÑùʵÏÖ»ùÓÚÇëÇóURLµÄÇëÇóÖØдÉèÖÃ

NginxÊÇÒ»¿îÇáÁ¿¡¢¸ßÐÔÄܵÄWebЧÀÍÆ÷ £¬Ëü²»µ«Ö§³Ö·´ÏòÊðÀí¡¢¸ºÔØƽºâµÈ¸ß¼¶¹¦Ð§ £¬»¹¾ß±¸Ç¿Ê¢µÄÇëÇóÖØдÄÜÁ¦¡£ÔÚÏÖʵµÄWebÓ¦ÓÃÖÐ £¬Ðí¶àÇéÐÎÏÂÐèÒª¶ÔÇëÇóURL¾ÙÐÐÖØд £¬ÒÔµÖ´ï¸üºÃµÄÓû§ÌåÑéºÍËÑË÷ÒýÇæÓÅ»¯Ð§¹û¡£±¾ÎĽ«ÏÈÈÝNginxÔõÑùʵÏÖ»ùÓÚÇëÇóURLµÄÇëÇóÖØдÉèÖà £¬°üÀ¨ÏêϸµÄ´úÂëʾÀý¡£

ÖØдÓï·¨

ÔÚNginxÖÐ £¬¿ÉÒÔʹÓÃrewriteÖ¸ÁîÀ´¾ÙÐÐÇëÇóÖØд¡£Æä»ù±¾Óï·¨ÈçÏ£º

rewrite regex replacement [flag];

µÇ¼ºó¸´ÖÆ

ÆäÖÐ £¬regexÌåÏÖÕýÔò±í´ïʽ £¬ÓÃÓÚÆ¥ÅäÄ¿½ñÇëÇóURL £»replacementÌåÏÖÄ¿µÄURL £¬Ìæ»»Ô­À´µÄURL £»flagÊÇ¿ÉÑ¡µÄ±ê¼Çλ £¬ÓÃÓÚ¿ØÖÆÖØдµÄÐÐΪ¡£

ÀýÈç £¬ÏÂÃæµÄÖØд¹æÔò¿ÉÒÔ½«ÒÔ¡°/page/¡±¿ªÍ·µÄURLÖØдΪÏìÓ¦µÄ¡°/index.php?page=¡±ÐÎʽ£º

rewrite ^/page/(d+)$ /index.php?page=$1 last;

µÇ¼ºó¸´ÖÆ

Ú¹ÊÍÒ»ÏÂÉÏÊö¹æÔòµÄ¼ÄÒ壺

^/page/(d+)$£ºÌåÏÖÒÔ¡°/page/¡±¿ªÍ· £¬ºóÃæ¸úÉÏÒ»¸ö»ò¶à¸öÊý×ÖµÄURL £»

/index.php?page=$1£ºÌåÏÖ½«Æ¥Åäµ½µÄURL £¬ÖØдΪ¡°/index.php?page=¡±¼ÓÉÏÆ¥Åäµ½µÄÊý×Ö £»

last£ºÌåÏÖÖÕֹĿ½ñrewriteÖ¸Áî £¬·µ»ØÖØдºóµÄURL¡£

ÇëÇóÖØдʾÀý

½ÓÏÂÀ´ £¬ÎÒÃǽ«Í¨¹ýʾÀýÀ´ÑÝʾÔõÑùʹÓÃNginxµÄÇëÇóÖØд¹¦Ð§ £¬ÒÔ¼°ÔõÑùʵÏÖ»ùÓÚÇëÇóURLµÄÇëÇóÖØдÉèÖ᣼ÙÉèÎÒÃÇÓÐÒ»¸ö¼òÆÓµÄPHPÓ¦Óà £¬ËüÓÐÁ½¸öÒ³Ã棺

/index.php£ºÊ×Ò³ £¬ÓÃÓÚÏÔʾ×îеÄʮƪÎÄÕ £»

/article.php?id=XX£ºÎÄÕÂÏêÇéÒ³ £¬ÓÃÓÚÏÔʾidΪXXµÄÎÄÕÂÏêϸÄÚÈÝ¡£

ÏÖÔÚ £¬ÎÒÃÇÏ£Íûͨ¹ýÇëÇóÖØдµÄ·½·¨ £¬À´ÓÅ»¯Õâ¸öÓ¦ÓõÄURL½á¹¹ £¬Ê¹ÆäÔ½·¢ÓѺúÍÓÅ»¯¡£ÏêϸÀ´Ëµ £¬ÎÒÃÇҪʵÏÖÒÔÏÂÁ½¸ö¹¦Ð§£º

½«Ê×Ò³µÄURL´Ó¡°/index.php¡±ÖØдΪ¡°/¡± £»

½«ÎÄÕÂÏêÇéÒ³µÄURL´Ó¡°/article.php?id=XX¡±ÖØдΪ¡°/article/XX¡±¡£

ÏÂÃæÊÇÍêÕûµÄNginxÉèÖÃÎļþºÍ×¢ÊÍÚ¹ÊÍ£º

# ½ç˵HTTP Server
server {
    # ¼àÌý80¶Ë¿Ú£¬´¦ÀíËùÓÐÀ´×Ô¿Í»§¶ËµÄÇëÇó
    listen 80;
    # ¼àÌýµÄÓòÃû
    server_name example.com;

    # ÉèÖÃÊ×Ò³µÄÇëÇóÖØд¹æÔò
    location = / {
        rewrite ^/$ /index.php last;
    }

    # ÉèÖÃÎÄÕÂÏêÇéÒ³µÄÇëÇóÖØд¹æÔò
    location ~ /article/(d+) {
        rewrite ^/article/(d+)$ /article.php?id=$1 last;
    }

    # ÉèÖÃPHP FastCGIЧÀÍÆ÷
    location ~ .php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # ½ç˵ÍøÕ¾¸ùĿ¼
    root /var/www/example.com;
    index index.php;
}

µÇ¼ºó¸´ÖÆ

×¢ÊÍ˵Ã÷£º

µÚ1-10ÐУº½ç˵һ¸öHTTP Server £¬¼àÌý80¶Ë¿Ú £¬´¦ÀíÀ´×Ôexample.comµÄÇëÇó £»

µÚ12-17ÐУºÉèÖÃÇëÇóÖØд¹æÔò £¬½«¡°/¡±ÖØдΪ¡°/index.php¡± £»

µÚ19-26ÐУºÉèÖÃÇëÇóÖØд¹æÔò £¬½«¡°/article/XX¡±ÖØдΪ¡°/article.php?id=XX¡± £»

µÚ28-35ÐУºÉèÖÃPHP FastCGIЧÀÍÆ÷ £¬¶ÔËùÓÐÒÔ¡°.php¡±×îºóµÄÇëÇó¾ÙÐд¦Àí £»

µÚ37-39ÐУº½ç˵ÍøÕ¾¸ùĿ¼ºÍĬÈÏÊ×Ò³¡£

ÔÚÉÏÊöµÄ´úÂëÖÐ £¬ÎÒÃÇʹÓÃÁËÁ½¸ölocationÖ¸ÁîÀ´»®·Ö½ç˵ÖØд¹æÔò £¬ËüÃÇ»®·ÖÆ¥Åä¶ÔÓ¦µÄURL¡£ÆäÖÐ £¬µÚÒ»¸ölocationÖ¸ÁîÆ¥ÅäµÄÊǸù·¾¶¡°/¡± £»µÚ¶þ¸ölocationÖ¸ÁîʹÓÃÁËÕýÔò±í´ïʽ £¬Æ¥ÅäµÄÊÇÒÔ¡°/article/¡±¿ªÍ·µÄURL¡£ÔÚÕâÁ½¸ölocationÖ¸ÁîÖÐ £¬ÎÒÃÇʹÓÃÁËrewriteÖ¸ÁîÀ´ÊµÏÖÇëÇóÖØд¡£

×ܽá

ͨ¹ý±¾ÎĵÄÏÈÈÝ £¬ÎÒÃÇÏàʶÁËNginxÔõÑùʵÏÖ»ùÓÚÇëÇóURLµÄÇëÇóÖØдÉèÖà £¬²¢Í¨¹ýÏêϸµÄ´úÂëʾÀý¾ÙÐÐÁËÑÝʾ¡£ÔÚÏÖʵӦÓÃÖÐ £¬ÎÒÃÇ¿ÉÒÔƾ֤ÐèÇó £¬×Ô½ç˵²î±ðµÄÖØд¹æÔò £¬ÒÔÓÅ»¯ÍøÕ¾µÄURL½á¹¹ £¬ÌáÉýÓû§ÌåÑéºÍËÑË÷ÒýÇæÓÅ»¯Ð§¹û¡£Ï£Íû±¾ÎÄÄܶԶÁÕßÓÐËù×ÊÖú¡£

ÒÔÉϾÍÊÇNginxÔõÑùʵÏÖ»ùÓÚÇëÇóURLµÄÇëÇóÖØдÉèÖõÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

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

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

QR code
ÍøÕ¾µØͼ