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

ÔõÑùʹÓÃNginxʵÏÖ»ùÓÚÓòÃûºÍ·¾¶µÄÐéÄâÖ÷»úÉèÖÃ

ÔõÑùʹÓÃnginxʵÏÖ»ùÓÚÓòÃûºÍ·¾¶µÄÐéÄâÖ÷»úÉèÖÃ

СÐò£º

ÔÚÍøÂçÓ¦Óÿª·¢ÖУ¬¾­³£ÐèÒªÉèÖöà¸öÐéÄâÖ÷»úÀ´Ìṩ²î±ðµÄЧÀÍ¡£NginxÊÇÒ»¸ö¸ßÐÔÄܵÄHTTPºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬Í¨¹ýʹÓÃNginx£¬ÎÒÃÇ¿ÉÒÔºÜÀû±ãµØʵÏÖ»ùÓÚÓòÃûºÍ·¾¶µÄÐéÄâÖ÷»úÉèÖᣱ¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃNginxÀ´ÊµÏÖÕâÑùµÄÉèÖ㬲¢¸ø³öÏìÓ¦µÄ´úÂëʾÀý¡£

Ò»¡¢»ùÓÚÓòÃûµÄÐéÄâÖ÷»úÉèÖÃ

ÔÚNginxµÄÉèÖÃÎļþÖУ¬Ê¹ÓÃserver¿éÀ´ÉèÖÃÐéÄâÖ÷»ú¡£ÔÚ»ùÓÚÓòÃûµÄÐéÄâÖ÷»úÉèÖÃÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýserver_nameÖ¸ÁîÀ´Ö¸¶¨ÓòÃû£¬²¢ÉèÖÃÏìÓ¦µÄЧÀÍ¡£

ʾÀý´úÂ룺

server {

listen 80;
server_name example.com;
root /var/www/example;
index index.html;

location / {
    try_files $uri $uri/ =404;
}

µÇ¼ºó¸´ÖÆ

}

ÔÚÉÏÊöʾÀý´úÂëÖУ¬ÉèÖÃÁËÒ»¸öÃûΪexample.comµÄÐéÄâÖ÷»ú¡£ÆäÖУ¬listenÖ¸ÁîÓÃÓÚÖ¸¶¨Nginx¼àÌýµÄ¶Ë¿ÚºÅ£¬server_nameÖ¸ÁîÓÃÓÚÖ¸¶¨ÓòÃû¡£rootÖ¸ÁîÓÃÓÚÖ¸¶¨ÍøÕ¾µÄ¸ùĿ¼£¬indexÖ¸ÁîÓÃÓÚÉèÖÃĬÈϵÄÊ×Ò³Îļþ¡£

¶þ¡¢»ùÓÚ·¾¶µÄÐéÄâÖ÷»úÉèÖÃ

³ýÁË»ùÓÚÓòÃûµÄÐéÄâÖ÷»úÉèÖã¬Nginx»¹Ö§³Ö»ùÓÚ·¾¶µÄÐéÄâÖ÷»úÉèÖá£Í¨¹ýÉèÖÃlocation¿é£¬ÎÒÃÇ¿ÉÒÔƾ֤»á¼ûµÄ·¾¶À´Ìṩ²î±ðµÄЧÀÍ¡£

ʾÀý´úÂ룺

server {

listen 80;
server_name example.com;

location / {
    root /var/www/example1;
    index index.html;
}

location /blog {
    root /var/www/example2;
    index index.php;
}

µÇ¼ºó¸´ÖÆ

}

ÔÚÉÏÊöʾÀý´úÂëÖУ¬ÉèÖÃÁËÒ»¸öÃûΪexample.comµÄÐéÄâÖ÷»ú£¬°üÀ¨Á½¸ölocation¿é¡£µÚÒ»¸ölocation¿éÉèÖÃÁ˸ù·¾¶/µÄЧÀÍ£¬rootÖ¸ÁîÉèÖÃΪ/var/www/example1£¬indexÖ¸ÁîÉèÖÃΪindex.html¡£µÚ¶þ¸ölocation¿éÉèÖÃÁË·¾¶Îª/blogµÄЧÀÍ£¬rootÖ¸ÁîÉèÖÃΪ/var/www/example2£¬indexÖ¸ÁîÉèÖÃΪindex.php¡£

Èý¡¢Í¬Ê±Ê¹ÓÃÓòÃûºÍ·¾¶µÄÐéÄâÖ÷»úÉèÖÃ

ÔÚÏÖʵӦÓÃÖУ¬ÎÒÃÇͨ³£ÐèҪͬʱʹÓÃÓòÃûºÍ·¾¶À´ÉèÖöà¸öÐéÄâÖ÷»ú¡£¿ÉÒÔͨ¹ý×éºÏʹÓÃserver_nameºÍlocationÖ¸ÁîÀ´ÊµÏÖÕâÑùµÄÉèÖá£

ʾÀý´úÂ룺

server {

listen 80;
server_name example.com;

location / {
    root /var/www/example1;
    index index.html;
}

µÇ¼ºó¸´ÖÆ

}

server {

listen 80;
server_name blog.example.com;

location / {
    root /var/www/example2;
    index index.php;
}

µÇ¼ºó¸´ÖÆ

}

ÔÚÉÏÊöʾÀý´úÂëÖУ¬ÉèÖÃÁËÁ½¸öÐéÄâÖ÷»ú¡£µÚÒ»¸öÐéÄâÖ÷»úʹÓÃexample.comÓòÃû£¬²¢ÌṩÒÔ/Ϊ·¾¶µÄЧÀÍ £»µÚ¶þ¸öÐéÄâÖ÷»úʹÓÃblog.example.comÓòÃû£¬²¢ÌṩÒÔ/Ϊ·¾¶µÄЧÀÍ¡£Á½¸öÐéÄâÖ÷»ú»®·ÖÖ¸¶¨Á˲î±ðµÄ¸ùĿ¼ºÍÊ×Ò³Îļþ¡£

½áÂÛ£º

ͨ¹ýNginxµÄÐéÄâÖ÷»úÉèÖã¬ÎÒÃÇ¿ÉÒÔÀû±ãµØΪ²î±ðµÄÓòÃûºÍ·¾¶Ìṩ²î±ðµÄЧÀÍ¡£ÔÚÏÖʵӦÓÃÖУ¬¿ÉÒÔÎÞаµØ×éºÏʹÓÃÓòÃûºÍ·¾¶À´ÉèÖöà¸öÐéÄâÖ÷»ú¡£Ï£Íû±¾ÎÄÄÜ×ÊÖú¶ÁÕ߸üºÃµØÃ÷È·ÔõÑùʹÓÃnginxʵÏÖ»ùÓÚÓòÃûºÍ·¾¶µÄÐéÄâÖ÷»úÉèÖá£

²Î¿¼´úÂ룺

https://www.nginx.com/resources/wiki/start/topics/examples/full/

https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04

ÒÔÉϾÍÊÇÔõÑùʹÓÃNginxʵÏÖ»ùÓÚÓòÃûºÍ·¾¶µÄÐéÄâÖ÷»úÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

QR code
ÍøÕ¾µØͼ