nginx基于域名的虚拟主机配置实战

背景:

        在www虚拟主机站点基础上新增一个bbs虚拟主机站点。

1    备份配置文件

1
2
3
[root@web01 conf] # pwd
/application/nginx/conf
[root@web01 conf] # cp nginx.conf{,.oldboy.2017.0819}

2    编辑nginx.conf配置文件,新增bbs虚拟主机站点


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@web01 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配置文件为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@web01 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;
         }
     }
}

        修改配置文件后,需要建立站点相关文件

1
2
3
[root@web01 conf] # mkdir ../html/{www,bbs} -p    #相当于在/application/nginx/html/目录下建立www和bbs两个目录
[root@web01 conf] # echo "www" >../html/www/index.html    #在www首页index.html中写入www字母
[root@web01 conf] # echo "bbs" >../html/bbs/index.html    #在bbs首页index.html中写入bbs字母


3    nginx配置文件检查语法查看是否有错误:/application/nginx/sbin/nginx -t    

1
2
3
[root@web01 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服务平滑重启

1
[root@web01 conf] # /application/nginx/sbin/nginx -s reload

    apche,nginx等如果平滑重启reload失败那么就用stop和start,禁止用restart

1
2
[root@web01 conf] # /application/nginx/sbin/nginx -s stop    #stop是带-s stop
[root@web01 conf] # /application/nginx/sbin/nginx    #start是直接nginx就是start

5    检查nginx服务80端口是否启动

1
2
3
4
[root@web01 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

1
2
3
4
5
6
7
8
9
10
11
[root@m01 ~] # 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地址。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@m01 ~] # 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
[root@m01 ~] # 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里面的内容)

1
2
3
4
[root@m01 ~] # curl www.etiantian.org
www
[root@m01 ~] # 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,说明新增域名虚拟主机成功。

wKiom1mYVCXiWZlHAACdmeBfXcc352.jpg

wKioL1mYVCLwD_-1AAC4h4cx1UY462.jpg


nginx配置虚拟主机步骤如下(适合各类虚拟主机类型)
1)增加一个完整的server标签段到结尾处。注意,要放在http的结束大括号前,也就是将server标签段到如http标签。
2)更改server_name及对应网页的root根目录,如果需要其他参数,可以增加或修改。
3)创建server_name域名对应网页的根目录,并且建立测试文件,如果没有index首页,访问会出现    403错误。
4)检查nginx配置文件语法,平滑重启nginx服务,快速检查启动结果。
5)在客户端对server_name处配置的域名做host解析或dns配置,并检查(ping域名看返回的ip对不对)
6)在win32浏览器中输入地址访问,或者在linux客户端做hosts解析,用wget或curl接地址访问。
 nginx虚拟主机的官网帮助网址为:http://nginx.org/en/docs/http/request_processing.html

本文转自sandshell博客51CTO博客,原文链接http://blog.51cto.com/sandshell/1957720如需转载请自行联系原作者


sandshell

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值