您的位置:首页 > 运维架构 > Nginx

Nginx虚拟主机配置实践(一)

2017-06-20 14:36 344 查看
Nginx虚拟主机配置实践(一)
一、虚拟主机的概念
在Web服务里虚拟主机就是一个独立的网站站点,这个站点对应独立的域名(也可能是IP或端口),具有独立的程序及资源目录,可以独立的对外提供服务供用户访问。
二、虚拟主机的类型
基于域名的虚拟主机

基于端口的虚拟主机

基于IP的虚拟主机
说明:实际生产中用的最多的就是基于域名的虚拟主机,其他两种了解即可。

三、基于一个域名虚拟主机的配置
Nginx主配置文件结构




创建一个最简化的Nginx主配置文件
[root@nginx-oldboy conf]# egrep -v "#|^$" nginx.conf.default > nginx.conf
说明:Nginx的主配置文件是nginx.conf,nginx.conf.default与nginx.conf内容是一样的。
执行上述命令之后,得到如下内容:



修改如下内容:
12 server_name www.afeilinux.com;
14 root html/wtf;




创建域名对应的站点目录及文件
[root@nginx-oldboy nginx1.10]# mkdir -p html/wtf

[root@nginx-oldboy nginx1.10]# cd html/ && ls
50x.html index.html wtf
[root@nginx-oldboy html]# echo "第一次测试" > ./wtf/index.html
[root@nginx-oldboy html]# cat ./wtf/index.html
第一次测试
说明:上述命令的作用是创建一个html/wtf站点目录,对应于主机配置文件里root根目录的html/wtf设置(root html/wtf;)。然后生成一个默认的首页文件index.html,文件内容是“第一次测试”。

nginx语法检查并重新加载
[root@nginx-oldboy html]# nginx -t



[root@nginx-oldboy html]# nginx -s reload
说明:如果没有启动nginx,则无法reload。报错内容如下:




查看监听端口
[root@nginx-oldboy html]# netstat -lnp |grep nginx




修改hosts配置文件
[root@nginx-oldboy html]# echo "192.168.100.116 www.afeilinux.com" >> /etc/hosts
查看一下:
[root@nginx-oldboy html]# tail -n1 /etc/hosts
192.168.100.116 www.afeilinux.com
在windows端也要修改hosts配置文件。

测试域名站点




四、基于多个域名虚拟主机的配置
增加新域名对应的配置
上面已经有了一个www.afeilinux.com虚拟主机的配置,下面再增加一个www.afeilinux.org虚拟主机的配置。增加的主机一定要在nginx.conf的http{}区块内,最好放在www.afeilinux.com虚拟主机配置的下面。
server {

listen 80;
server_name www.afeilinux.org;
location / {
root html/org;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}




创建新虚拟机主机站点对应的目录和文件
[root@nginx-oldboy nginx1.10]# mkdir ./html/org
[root@nginx-oldboy nginx1.10]# echo "第二次测试" > ./html/org/index.html
[root@nginx-oldboy nginx1.10]# cat !$
cat ./html/org/index.html
第二次测试

检查下站点目录结构
[root@nginx-oldboy nginx1.10]# tree
-bash: tree: command not found
解决方法:
[root@nginx-oldboy nginx1.10]# yum install -y tree
[root@nginx-oldboy nginx1.10]# tree html/




检查语法并重新加载nginx配置
[root@nginx-oldboy ~]# nginx -t
[root@nginx-oldboy ~]# nginx -s reload

测试域名站点




五、规范和优化nginx配置文件
将虚拟主机配置成单独的配置文件与nginx主配置文件nginx.conf分开
说明:
(1)适用于虚拟主机数量不多的情况;
(2)主配置文件包含的所有虚拟主机的子配置文件会统一放在extra目录中;
(3)虚拟主机配置单独的配置文件,使用参数include,它可以放在nginx主配置文件中任何位置。
[root@nginx-oldboy conf]# mkdir extra
[root@nginx-oldboy conf]# sed -n '10,21p' nginx.conf



[root@nginx-oldboy conf]# sed -n '10,21p' nginx.conf > extra/wtf.conf

[root@nginx-oldboy conf]# cat extra/wtf.conf
[root@nginx-oldboy conf]# sed -n '22,33p' nginx.conf > extra/org.conf

[root@nginx-oldboy conf]# cat extra/org.conf

更改主配置文件nginx.conf
删除主配置文件nginx.conf中所有虚拟主机的配置,包含server{}标签。
[root@nginx-oldboy conf]# sed -i '10,33d' nginx.conf
[root@nginx-oldboy conf]# cat nginx.conf



把虚拟主机独立配置文件wtf.conf和org.conf的信息包含到nginx.conf里,这样就把主配置文件和各个虚拟主机配置分离了。
include extra/wtf.conf
include extra/org.conf
[root@nginx-oldboy conf]# sed -i '10 i include extra/wtf.conf;\ninclude extra/org.conf;' nginx.conf




nginx语法检测和重新加载
[root@nginx-oldboy conf]# nginx -t
[root@nginx-oldboy conf]# nginx -s reload

测试
[root@nginx-oldboy conf]# curl www.afeilinux.com
第一次测试
[root@nginx-oldboy conf]# curl www.afeilinux.org
第二次测试
[root@nginx-oldboy conf]# tree extra/



这样虚拟主机配置文件就与nginx主配置文件分离开了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息