|
nginx学习笔记之基于端口的虚拟主机基于主机名的虚拟主机root、alias、index配置
实验环境:
centos 测试节点IP:172.16.3.101
基于端口的虚拟主机:
vim /etc/nginx/nginx.conf
# 向里面的http {}里面加入如下内容
server { # server定义一个虚拟主机
listen 8080; # 监听本机所有IP端口8080
server_name www.test.com; # 虚拟主机名为:www.test.com
location / { # 根据用户请求的URI决定是否匹配该location,如若匹配到,则将被该location中的配置所处理
root "/web/htdocs"; # web资源路径映射
}
}
# 保存退出
# 创建目录/web/htdocs
mkdir -pv /web/htdocs
vim /web/htdocs/index.html
# 向里面加入如下内容
hello,my serser_name is www.test.com,port is 8080
# 保存退出
# 测试,在远端浏览器分别输入:http://172.16.3.101 和 http://172.16.3.101:8080
# 如果显示对应的结果,则表明基于端口的虚拟主机配置成功
配置基于主机名的虚拟主机
vim /etc/nginx/nginx.conf
# 向里面的http{}里面加入如下内容
server { # server定义一个虚拟主机
listen 80; # 监听本机所有IP端口:80
server_name www.test.com; # 虚拟主机名为:www.test.com
location / { # 根据用户请求的RUL决定是否匹配该location,如果匹配到,则将被该location中的配置所处理
root "/web/htdocs"; # web资源路径映射
}
}
server { # server定义一个虚拟主机
listen 80; # 监听本机所有IP端口:80
server_name mail.test.com; # 虚拟主机名为:mail.test.com
location / { # 上面已经说了,此处不再重复
root "/web/mail"; # web资源路径映射
}
}
mkdir -pv /web/mail
vim /web/mail/index.html
# 向里面加入如下内容
hello,my server_name is mail.test.com
# 保存退出
vim /web/htdocs/index.html
# 向里面加入如下内容
hello,my server_name is www.test.com
# 保存退出
# 检查其语法
[iyunv@localhost conf]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# 重启nginx【由于此处修改了端口号,所以需要重启nginx】
[iyunv@localhost conf]# service nginx restart
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
# 配置解析文件/etc/hosts【这里访问测试还是在本机进行的,所以我就修改本机的hosts文件】
vim /etc/hosts
# 向里面加入如下内容
172.16.3.101 www.test.com # 把主机名www.test.com解析为172.16.3.101
172.16.3.101 mail.test.com # 把主机名mail.test.com解析为172.16.3.101
# 保存退出
# 测试【在本机直接测试就行,前面已说,hosts文件已经修改好】
# 测试www.test.com虚拟主机
[iyunv@localhost conf]# elinks -dump http://www.test.com
hello,my server_name is www.test.com
# 可知,www.test.com虚拟主机正常
# 测试mail.test.com虚拟主机
[iyunv@localhost conf]# elinks -dump http://mail.test.com
hello,my server_name is mail.test.com
# 可知,mail.test.com虚拟主机正常
root和alias的区别:
我这里先不说,且看下面的例子
vim /etc/nginx/nginx.conf
# 把如下语句加入http{}里面
server {
listen 80;
server_name www.test.com;
location /root {
root "/web/htdocs";
}
}
server {
listen 80;
server_name mail.test.com;
location /alias {
alias "/web/mail";
}
}
# 对于虚拟主机www.test.com
mkdir -pv /web/htdocs/root/
vim /web/htdocs/root/index.html
# 向里面加入如下语句
hello,this is root type.
# 保存,退出
# 对于虚拟主机mail.test.com
mkdir -pv /web/mail
vim /web/mail/index.html
# 向里面加入如下语句
hello,this is alias type.
# 保存退出
# 语法检查
nginx -t
# nginx服务重启
service nginx restart
# 配置解析文件/etc/hosts,和上面那个实验做得一样,这里就不重复啦
# 测试【直接在本机测试】
# 测试www.test.com虚拟主机
[iyunv@localhost root]# elinks -dump http://www.test.com/root/
hello,this is root type.
# 可知root类型是这样访问的,是这样起作用的,其对应访问路径为:/web/htdocs/root/index.html,不用我多说了吧。
# 测试mail.test.com虚拟主机
[iyunv@localhost root]# elinks -dump http://mail.test.com/alias
hello,this is alias type.
# 可知alias类型是这样访问的,是这样起作用的,其对应访问路径为:/web/mail/index.html,不用我解释了吧。
index配置:设置默认主页面
看下面的操作吧
vim /etc/nginx/nginx.conf
# 把如下内容放入里面
server {
listen 80;
server_name www.test.com;
location /root {
root "/web/htdocs";
}
}
# nginx 语法检查
nginx -t
# nginx 服务重启
service nginx restart
# 创建其web资源路径和文件
mkdir -pv /web/htdocs/root/
vim /web/htdocs/root/index.html
# 向里面加入如下语句
hello,this is root type.
# 保存,退出
# 配置解析文件/etc/hosts
vim /etc/hosts
# 把如下语句加入其中
172.16.3.101 www.test.com
# 保存退出
# 访问【在本机测试访问就行】
# 输入如下语句,这里没有输入主页文件,但是也能正常访问,可知nginx默认主页文件为index.html
[iyunv@localhost root]# elinks -dump http://www.test.com/root/
hello,this is root type.
vim /etc/nginx/nginx.conf
# 把刚才加入的语句换成如下语句
server {
listen 80;
server_name www.test.com;
location /root {
root "/web/htdocs";
index test.html
}
}
# nginx语法检查
nginx -t
# nginx 服务重启
service nginx restart
# 访问【在本机测试访问就行】
# 输入如下语句,这里没有输入主页文件,但是不能正常访问了
[iyunv@localhost root]# elinks -dump http://www.test.com/root
403 Forbidden
--------------------------------------------------------------------------
nginx/1.6.2
# 现在我把主页文件名更改一下
mv /web/htdocs/root/index.html /web/htdocs/root/test.html
# 再访问一下
# 输入如下语句,这里没有输入主页文件,可以正常访问了
[iyunv@localhost root]# elinks -dump http://www.test.com/root
hello,this is root type.
现在应该明白index的作用了吧
|
|