一、Nginx虚拟主机
每个虚拟web 站点拥有独立的 “server { }” 配置端,各自监听的IP地址、端口号可以单独制定,当然网站名称也是不同的。
二、基于域名
- 环境准备:
这两个域名指向同一个IP地址
域名 | IP地址 |
---|---|
www.test.com / www.demo.com | 192.168.220.134 |
nginx的安装已经提前配置好了,需要的可以看上一篇博客,链接为:Nginx 的配置和访问控制
1、DNS解析
vim /etc/named.conf
修改两处:
listen-on port 53 { any; };
allow-query { any; };
vim /etc/named.rfc1912.zones
//增添这两个域名的区域配置
zone "test.com" IN {
type master;
file "test.com.zone";
allow-update { none; };
};
zone "demo.com" IN {
type master;
file "demo.com.zone";
allow-update { none; };
};
cd /var/named/
cp -p test.com.zone demo.com.zone
systemctl restart named //开启服务
2、配置测试网页的内容
mkdir -p /var/www/html/test
mkdir -p /var/www/html/demo
cd /var/www/html/
echo "This is test web" > test/index.html
echo "This is demo web" > demo/index.html
3、修改 /usr/local/nginx/conf/nginx.conf 文件,用 server {} 段配置不同站点。
ln -s /usr/local/nginx/conf/nginx.conf /etc/
vim /etc/nginx.conf
//修改内容:
server {
listen 80;
server_name www.test.com;
charset utf-8;
access_log logs/www.test.com.access.log;
location / {
root /var/www/html/test;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.demo.com;
charset utf-8;
access_log logs/www.demo.com.access.log;
location / {
root /var/www/html/demo;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
4、检查配置文件是否有语法错误
nginx -t
5、用客户机 win10 访问两个域名测试
三、基于端口
选择系统中不同的端口,将多个端口映射到同一个 IP地址。
1、修改 nginx.conf 文件
vim /etc/nginx.conf
server {
listen 192.168.220.134:80;
server_name www.test.com;
charset utf-8;
access_log logs/www.test.com.access.log;
location / {
root /var/www/html/test;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.220.134:8080;
server_name www.test.com;
charset utf-8;
access_log logs/www.test8080.com.access.log;
location / {
root /var/www/html/test8080;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
2、重启nginx
service nginx stop
service nginx start
3、测试网页内容
mkdir -p /var/www/html/test8080
cd /var/www/html/
echo "This is test8080 web" > test8080/index.html
4、用客户机 win10 访问两个端口测试
四、基于IP地址
域名 | IP地址 |
---|---|
www.test.com | 192.168.220.134 |
www.demo.com | 192.168.220.135 |
1、修改 nginx.conf 文件
vim /etc/nginx.conf
server {
listen 192.168.220.134:80;
server_name www.test.com;
charset utf-8;
access_log logs/www.test.com.access.log;
location / {
root /var/www/html/test;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.220.135:80;
server_name www.demo.com;
charset utf-8;
access_log logs/www.demo.com.access.log;
location / {
root /var/www/html/demo;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
2、重启nginx
service nginx stop
service nginx start
3、dns中 demo 的IP地址
cd /var/named/
vim demo.com.zone
//修改
www IN A 192.168.220.135
//启动服务
systemctl restart named
4、用客户机 win10 访问两个ip地址测试