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

如:

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

http { server { listen 80; server_name a.chenlb.com; access_log logs/a.access.log main; server_name_in_redirect off; location / { index index.html; root /home/www/host_a/; } } server { listen 80; server_name b.chenlb.com; access_log logs/b.access.log main; server_name_in_redirect off; location / { index index.html; root /home/www/host_b/; } } }


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

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

    server_name_in_redirect  off;

    location / {
      index index.html;
      root  /var/www/default/htdocs;
    }
  }
}


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