所谓虚拟主机,在 Web 服务里就是一个独立的网站站点,这个站点对应独立的域名(也可能是IP 或端口),具有独立的程序及资源,可以独立地对外提供服务供用户访问。
在 Nginx 中,使用一个 server{} 标签来标识一个虚拟主机,一个 Web 服务里可以有多个虚拟主机标签对,即可以同时支持多个虚拟主机站点。
虚拟主机有三种类型:基于域名的虚拟主机、基于端口的虚拟主机、基于 IP 的虚拟主机。
[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)
[root@localhost conf]# mkdir -p /usr/local/nginx/conf/vhosts
[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;
}
}
[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
echo "127.0.0.1 www.abc.com bbs.abc.com blog.abc.com" >> /etc/hosts
或者使用命令打开/etc/hosts文件,然后编辑信息
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
#Nginx 基于域名的虚拟主机 本机hosts文件添加配置,使得本地可以ping通
127.0.0.1 www.abc.com bbs.abc.com blog.abc.com
在cmd窗口输入以下命令,刷新DNS内容:
ipconfig /flushdns
在浏览器中分别输入www.abc.com、bbs.abc.com 和blog.abc.com,如果浏览器可以返回对应的首页内容,说明新增域名虚拟主机成功。
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配置虚拟主机步骤如下(适合各类虚拟主机类型)