nginx基于域名&&端口的多虚拟主机配置
基于多域名的多虚拟主机
[环境准备]
1.在本地windows系统中的hosts文件中,添加对应的解析记录,方便测试
10.0.0.200 www.nginx.com
10.0.0.200 www.nginx_cc.com
10.0.0.200 www.nginx_yy.com
在linux中/etc/hosts文件中也写入对应解析记录
10.0.0.200 www.nginx.com
10.0.0.200 www.nginx_cc.com
10.0.0.200 www.nginx_yy.com
【开始实践】
1.在nginx.conf主配置文件中修改第一台虚拟主机配置
server {
listen 80;
server_name www.nginx_qq.com;
charset utf-8;
location / {
root /www/nginx_qq;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
2.在/opt/tngx232/conf/extra/nginx_qq.conf中修改第二台虚拟主机配置
server {
listen 80;
server_name www.nginx_cc.com;
charset utf-8;
location / {
root /www/nginx_cc;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
3.在/opt/tngx232/conf/extra/nginx_yy.conf中修改第三台虚拟主机配置
server {
listen 80;
server_name www.nginx_yy.com;
charset utf-8;
location / {
root /www/nginx_yy;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
4.配置完成后检查nginx配置文件语法
[root@backup extra]# nginx -t
nginx: the configuration file /opt/tngx232/conf/nginx.conf syntax is ok
nginx: configuration file /opt/tngx232/conf/nginx.conf test is successful
5.重启nginx
[root@backup extra]# nginx -s stop
[root@backup extra]# nginx
6.准备基于域名的三个站点目录资料
[root@backup extra]# mkdir -p /www/{nginx_qq,nginx_cc,nginx_yy}
[root@backup extra]# echo "我是nginx_qq,祝贺你成功完成了基于域名的多虚拟主机配置" > /www/nginx_qq/index.html
[root@backup extra]# echo "我是nginx_cc,祝贺你成功完成了基于域名的多虚拟主机配置" > /www/nginx_cc/index.html
[root@backup extra]# echo "我是nginx_yy,祝贺你成功完成了基于域名的多虚拟主机配置" > /www/nginx_yy/index.html
7.测试访问
[root@backup extra]# curl www.nginx_qq.com
我是nginx_qq,祝贺你成功完成了基于域名的多虚拟主机配置
[root@backup extra]# curl www.nginx_cc.com
我是nginx_cc,祝贺你成功完成了基于域名的多虚拟主机配置
[root@backup extra]# curl www.nginx_yy.com
我是nginx_yy,祝贺你成功完成了基于域名的多虚拟主机配置
【基于端口的多虚拟主机】
1.第一个虚拟主机
server {
#监听的端口和ip
listen 80;
#主机域名
server_name www.chaogelinux.com;
access_log logs/www.chaogelinux.log;
charset utf-8;
#url匹配
location / {
#HTML文件存放的目录
root /website/s1/;
#默认首页文件,从左往右寻找,index.html或是index.htm文件
index index.html index.htm;
#autoindex off;
}
}
2.#第二个虚拟主机
server {
listen 81;
server_name www.chaogenginx.com;
access_log logs/www.chaogenginx.com;
charset utf-8;
location / {
index index.html index.htm;
root /website/s2;
#autoindex on;
}
}
3.nginx -s reload
4.访问80、81端口
[root@bogon nginx]# curl 127.0.0.1:81
我是来自于域名www.chaogenginx.com的多域名虚拟主机
[root@bogon nginx]# curl 127.0.0.1:80
我是多域名虚拟主机,来自于域名www.chaogelinux.com
<img src="./1.jpg">
<img src="./2.jpg">
<img src="./3.jpg">
<head>
<link rel="shortcut icon" href="#"/>
</head>