nginx基于域名的虚拟主机配置实战
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx基于域名的虚拟主机配置实战相关的知识,希望对你有一定的参考价值。
背景:
在www虚拟主机站点基础上新增一个bbs虚拟主机站点。
1 备份配置文件
[[email protected] conf]# pwd /application/nginx/conf [[email protected] conf]# cp nginx.conf{,.oldboy.2017.0819}
2 编辑nginx.conf配置文件,新增bbs虚拟主机站点
[[email protected] conf]# vim nginx.conf.oldboy.20170819 worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.etiantian.org; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
新增bbs站点后的nginx.conf配置文件为:
[[email protected] conf]# cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.etiantian.org; location / { root html/www; index index.html index.htm; } } server { listen 80; server_name bbs.etiantian.org; location / { root html/bbs; index index.html index.htm; } } }
修改配置文件后,需要建立站点相关文件
[[email protected] conf]# mkdir ../html/{www,bbs} -p #相当于在/application/nginx/html/目录下建立www和bbs两个目录 [[email protected] conf]# echo "www" >../html/www/index.html #在www首页index.html中写入www字母 [[email protected] conf]# echo "bbs" >../html/bbs/index.html #在bbs首页index.html中写入bbs字母
3 nginx配置文件检查语法查看是否有错误:/application/nginx/sbin/nginx -t
[[email protected] conf]# /application/nginx/sbin/nginx -t #自检查看是否有错误 nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
根据提示nginx配置文件检查语法结果成功
4 nginx服务平滑重启
[[email protected] conf]# /application/nginx/sbin/nginx -s reload
apche,nginx等如果平滑重启reload失败那么就用stop和start,禁止用restart
[[email protected] conf]# /application/nginx/sbin/nginx -s stop #stop是带-s stop [[email protected] conf]# /application/nginx/sbin/nginx #start是直接nginx就是start
5 检查nginx服务80端口是否启动
[[email protected] conf]# lsof -i :80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 5784 root 6u IPv4 23562 0t0 TCP *:http (LISTEN) nginx 5785 www 6u IPv4 23562 0t0 TCP *:http (LISTEN)
6 在linux中验证nginx服务是否可用(当然也可以再windows中验证)
6.1 在管理机m01机器上面本机hosts文件中添加172.16.1.8的dns解析172.16.1.8 web01 www.etiantian.org bbs.etiantian.org
[[email protected] ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.16.1.5 lb01 172.16.1.6 lb01 172.16.1.7 web02 172.16.1.8 web01 www.etiantian.org bbs.etiantian.org 172.16.1.51 de01 db01.etiantian.org 172.16.1.31 nfs01 172.16.1.41 backup 172.16.1.61 m01
6.2 然后ping www.etiantian.org和bbs.etiantian.org看是否可用解析到172.16.1.8这个ip地址。
[[email protected] ~]# ping -c4 www.etiantian.org PING web01 (172.16.1.8) 56(84) bytes of data. 64 bytes from web01 (172.16.1.8): icmp_seq=1 ttl=64 time=0.629 ms 64 bytes from web01 (172.16.1.8): icmp_seq=2 ttl=64 time=0.432 ms 64 bytes from web01 (172.16.1.8): icmp_seq=3 ttl=64 time=0.439 ms 64 bytes from web01 (172.16.1.8): icmp_seq=4 ttl=64 time=0.654 ms --- web01 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3003ms rtt min/avg/max/mdev = 0.432/0.538/0.654/0.105 ms [[email protected] ~]# ping -c4 bbs.etiantian.org PING web01 (172.16.1.8) 56(84) bytes of data. 64 bytes from web01 (172.16.1.8): icmp_seq=1 ttl=64 time=0.896 ms 64 bytes from web01 (172.16.1.8): icmp_seq=2 ttl=64 time=0.459 ms 64 bytes from web01 (172.16.1.8): icmp_seq=3 ttl=64 time=0.782 ms 64 bytes from web01 (172.16.1.8): icmp_seq=4 ttl=64 time=0.445 ms --- web01 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3005ms rtt min/avg/max/mdev = 0.445/0.645/0.896/0.199 ms
6.3 curl 两个虚拟主机站点名字,看是否可用返回对应的www和bbs内容(首页index.html里面的内容)
[[email protected] ~]# curl www.etiantian.org www [[email protected] ~]# curl bbs.etiantian.org bbs
7 windows浏览器中验证
7.1 在windows主机hosts文件中添加dns解析
在C:\Windows\System32\drivers\etc\hosts中添加一条主机解析:
10.0.0.8 www.etiantian.org bbs.etiantian.org
7.2 在浏览器中分别输入www.etiantian.org 和bbs.etiantian.org可以返回对应的首页内容www和bbs,说明新增域名虚拟主机成功。
本文出自 “sandshell” 博客,请务必保留此出处http://sandshell.blog.51cto.com/9055959/1957720
以上是关于nginx基于域名的虚拟主机配置实战的主要内容,如果未能解决你的问题,请参考以下文章
Nginx服务虚拟主机的配置----------基于域名端口IP(实战!)