Nginx-多站点配置示例
说明
介绍在nginx中如何配置多站点。
分配域名
1.如果有自己域名的,可以直接添加两个子域名指到自己的服务器上。
2.没有的,可以添加两个虚拟域名到hosts文件下,windows在C:WindowsSystem32driversetc 目录下,mac的自己百度一下吧。T.T
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
47.105.156.25 zxliu.com zsan.com
多站点配置
<h5>配置地址说明</h5>
博主建议每一个站点都配置一个server文件进行单独管理,根据Linux的发行商和nginx的安装方式不同,放置server文件的文件夹都有所不同,博主是在ubuntu下通过apt-get install直接安装的nginx,所以server文件可以放置在conf.d/文件夹和sites-enabled/文件夹下。
配置文件中有配置:
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
<h5>多站点server配置</h5>
在sites-enabled/文件夹创建 zxliu.com zsan.com两个文件,注意记录error_log的文件夹要存在并且可执行
zxliu.com:
server {
listen 80;
server_name zxliu.com;
error_log /home/zhangwei/data/log/nginx/zxliu_error.log error;
root /home/zhangwei/data/www/a/;
location / {
index index.html;
}
location /NginxStatus{
stub_status on;
}
}
zsan.com:
server {
listen 80;
server_name zsan.com;
error_log /home/zhangsan/data/log/nginx/zsan_error.log error;
root /home/zhangsan/data/www/b/;
location / {
index index.html;
}
}
<h5>项目文件添加</h5>
在/home/zhangsan/data/www/文件夹下创建a,b目录并分别添加index.html文件
cd /home/zhangsan/data/www/
mkdir a b
cd a
echo 'A web' >index.html
cd ../b
echo 'B web' >index.html
<h5>测试</h5>
打开浏览器,分别访问zxliu.com和zsan.com
如下所示:


结尾
若无缘,若有缘。