1、虚拟主机概念

  • 所谓虚拟主机,在 Web 服务里就是一个独立的网站站点,这个站点对应独立的域名(也可能是IP 或端口),具有独立的程序及资源,可以独立地对外提供服务供用户访问。
  • 在 Nginx 中,使用一个 server{} 标签来标识一个虚拟主机,一个 Web 服务里可以有多个虚拟主机标签对,即可以同时支持多个虚拟主机站点。
  • 虚拟主机有三种类型:基于域名的虚拟主机、基于端口的虚拟主机、基于 IP 的虚拟主机。

2、基于域名的虚拟主机配置

2.1 编辑配置文件

[root@localhost conf]# vim 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.abc.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
   
    server {
        listen       80;
        server_name  bbs.abc.com;
        location / { 
            root   html/bbs;
            index  index.html index.htm;
        }   
    }   

    server {
        listen       80;
        server_name  blog.abc.com;
        location / { 
            root   html/blog;
            index  index.html index.htm;
        }   
    }   
}

规范化 Nginx 配置文件,将每个虚拟主机配置成单独的文件,放在统一目录中(如:vhosts)

  • 创建vhosts目录
[root@localhost conf]# mkdir -p /usr/local/nginx/conf/vhosts
  • 编辑 nginx.conf 主配置文件
[root@localhost conf]# vim nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    include vhosts/*.conf;
}
  • 创建每个虚拟主机配置文件:
[root@localhost conf]# vim vhosts/www.abc.com.conf
server {
        listen       80;
        server_name  www.abc.com;
        location / {
                root   html/www;
                index  index.html index.htm;
        }
}
[root@localhost conf]# vim vhosts/bbs.abc.com.conf
server {
        listen       80;
        server_name  bbs.abc.com;
        location / {
                root   html/bbs;
                index  index.html index.htm;
        }
}
[root@localhost conf]# vim vhosts/blog.abc.com.conf
server {
        listen       80;
        server_name  blog.abc.com;
        location / {
                root   html/blog;
                index  index.html index.htm;
        }
}

2.2 创建虚拟主机站点对应的目录和文件

[root@localhost html]# cd /usr/local/nginx/html/
[root@localhost html]# for n in www bbs blog
> do
> mkdir ${n}
> echo "http://${n}.abc.com" > ${n}/index.html
> done

2.3 编辑 /etc/hosts 文件,域名解析

echo "127.0.0.1 www.abc.com bbs.abc.com blog.abc.com" >> /etc/hosts

或者使用命令打开/etc/hosts文件,然后编辑信息

2.4 重新加载 Nginx 配置

nginx配置文件检查语法查看是否有错误:

[root@localhost conf]# /usr/local/nginx/sbin/nginx -t
根据提示nginx配置文件检查语法结果成功

nginx服务平滑重启

[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload

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

[root@localhost conf]# /usr/local/ginx/sbin/nginx -s stop    #stop是带-s stop
[root@localhost conf]# /usr/local/nginx/sbin/nginx    #直接nginx就是start

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

[root@localhost conf]# lsof -i :80
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx    6290   root    6u  IPv4  56693      0t0  TCP *:http (LISTEN)
nginx   10273 nobody    6u  IPv4  56693      0t0  TCP *:http (LISTEN)

在linux中验证nginx服务是否可用(当然也可以再windows中验证)!!!

[root@localhost conf]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 www.abc.com bbs.abc.com blog.abc.com

也可以输入命令:vim /etc/hosts 修改hosts文件

[root@localhost gmz]# vim /etc/hosts

然后输入命令: ping www.abc.com、blog.abc.com和bbs.abc.com看是否可用解析到127.0.0.1这个IP地址。如果输入以上命令,控制台未显示以下信息,则请求超时(ping 不通),如果想要终端ping操作,按Ctr+C可结束ping命令。当出现ping不通的情况,我们可以去检查主机hosts文件+虚拟机hosts文件是否完成上一步的操作,即在hosts文件中添加DNS解析。

[root@localhost gmz]# ping www.abc.com
PING www.abc.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.108 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.076 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.060 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.062 ms
64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.060 ms
64 bytes from localhost (127.0.0.1): icmp_seq=6 ttl=64 time=0.059 ms
64 bytes from localhost (127.0.0.1): icmp_seq=7 ttl=64 time=0.030 ms
64 bytes from localhost (127.0.0.1): icmp_seq=8 ttl=64 time=0.040 ms
64 bytes from localhost (127.0.0.1): icmp_seq=9 ttl=64 time=0.061 ms
^C
--- www.abc.com ping statistics ---
9 packets transmitted, 9 received, 0% packet loss, time 8002ms
rtt min/avg/max/mdev = 0.030/0.061/0.108/0.022 ms
[root@localhost gmz]# ping bbs.abc.com
PING www.abc.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.016 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.047 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.056 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.154 ms
64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.059 ms
64 bytes from localhost (127.0.0.1): icmp_seq=6 ttl=64 time=0.059 ms
^C
--- www.abc.com ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5000ms
rtt min/avg/max/mdev = 0.016/0.065/0.154/0.042 ms
[root@localhost gmz]# ping blog.abc.com
PING www.abc.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.017 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.067 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.056 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.060 ms
^C64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.059 ms

64 bytes from localhost (127.0.0.1): icmp_seq=6 ttl=64 time=0.062 ms
^C
--- www.abc.com ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5002ms
rtt min/avg/max/mdev = 0.017/0.053/0.067/0.018 ms
  • windows浏览器中验证
    在windows主机hosts文件中添加dns解析,在C:\Windows\System32\drivers\etc\hosts中添加一条主机解析:
#Nginx 基于域名的虚拟主机  本机hosts文件添加配置,使得本地可以ping通
127.0.0.1 www.abc.com bbs.abc.com blog.abc.com

在cmd窗口输入以下命令,刷新DNS内容:

ipconfig /flushdns

在浏览器中分别输入www.abc.combbs.abc.com 和blog.abc.com,如果浏览器可以返回对应的首页内容,说明新增域名虚拟主机成功。

2.5 访问测试

cul + 虚拟主机站点名字,查看控制台是否显示对应站点的内容(首页index.html 里面的内容)

[root@localhost gmz]# curl http://www.abc.com
http://www.abc.com
[root@localhost gmz]# curl http://blog.abc.com
http://blog.abc.com
[root@localhost gmz]# curl http://bbs.abc.com 
http://bbs.abc.com

小结,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接地址访问。