Nginx 配置虚拟主机

VPS 上安装了 nginx。用多个子域名,每个子域名到不同的目录。

如:

  1. http {  
  2.     server {  
  3.         listen 80;  
  4.         server_name a.com;  
  5.         access_log logs/a.access.log main;  
  6.   
  7.         server_name_in_redirect off;  
  8.   
  9.         location / {  
  10.                 index index.html;  
  11.                 root /home/www/host_a/;  
  12.         }  
  13.     }  
  14.   
  15.     server {  
  16.         listen 80;  
  17.         server_name b.com;  
  18.         access_log logs/b.access.log main;  
  19.   
  20.         server_name_in_redirect off;  
  21.   
  22.         location / {  
  23.                 index index.html;  
  24.                 root /home/www/host_b/;  
  25.         }  
  26.     }  
  27. }  

结果发现用 b.com 还是指到 host_a 目录。后来看了官方示例:http://wiki.nginx.org/NginxVirtualHostExample,提到有个 default 的匹配,如:

  1. http {  
  2.   server {  
  3.     listen          80 default;  
  4.     server_name     _;  
  5.     access_log      logs/default.access.log main;  
  6.   
  7.     server_name_in_redirect  off;  
  8.   
  9.     location / {  
  10.       index index.html;  
  11.       root  /var/www/default/htdocs;  
  12.     }  
  13.   }  
  14. }  

加上这个 default 就可使 a.com 和 b.com 正常工作了。

原文地址:https://www.cnblogs.com/rmbteam/p/2208937.html