Nginx 配置虚拟主机1

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 正常工作了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值