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


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/ && ls50x.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/hosts192.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.confinclude extra/org.conf[root@nginx-oldboy conf]# sed -i ’10 i include extra/wtf.conf;ninclude extra/org.conf;’ nginx.confnginx语法检测和重新加载[root@nginx-oldboy conf]# nginx -t[root@nginx-oldboy conf]# nginx -s reload测试[roo开发云主机域名t@nginx-oldboy conf]# curl www.afeilinux.com第一次测试[root@nginx-oldboy conf]# curl www.afeilinux.org第二次测试[root@nginx-oldboy conf]# tree extra/这样虚拟主机配置文件就与nginx主配置文件分离开了!

相关推荐: 实现JSP通过Tomcat连接MySQL

实现JSP通过Tomcat连接MySQL服务概述1、Tomcat是什么?Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。Tomcat和I…

免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。

(0)
打赏 微信扫一扫 微信扫一扫
上一篇 03/30 12:49
下一篇 03/30 12:49