虚拟主机:把一台运行在因特网上的服务主机分成多台虚拟的主机,每台主机都可以是一个独立的网站,可以具有独立的域名,具有完整的服务器功能。

虚拟主机分类:

基于域名

基于ip

基于端口(最常用的一种)

1.基于域名的虚拟主机

include /opt/nginx/conf/vhosts/*.conf;
server {
listen       80 default;
server_name  localhost;
index index.html index.htm index.php;
root     /opt/nginx/html ;
access_log  logs/host.access.log  ;
location ~ \.php$ {
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include        fastcgi_params;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires      30d;
}
location ~ .*\.(js|css)?$
{
expires      1h;
}
}
server {
listen 80;
root /var/www/html;
index index.html index.htm ;
server_name www.zhu.com;
access_log /var/log/zhu.log;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
}
}
}

第一个虚拟主机,直接在主配置文件中配置 www.zhu.com

include /opt/nginx/conf/vhosts/*.conf;

第二个,第三个虚拟主机在vhosts目录下配置

www.jiang.com

server {
listen 80;
root /var/www/jiang;
index index.html index.htm ;
server_name www.jiang.com;
access_log /var/log/zhu.log;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/jiang$fastcgi_script_name;
}
}
~

www.tao.com

server {
listen 80;
root /var/www/tao;
index index.html index.htm ;
server_name www.tao.com;
access_log /var/log/zhu.log;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/tao$fastcgi_script_name;
}
}
~
~

2.在网页根目录创建文件


上的C:\WINDOWS\system32\drivers\etc\hosts绑定域名

192.168.70.134   www.zhu.com
192.168.70.134   www.jiang.com
192.168.70.134   www.tao.com

3.访问

Nginx之虚拟主机_server

Nginx之虚拟主机_server_02

Nginx之虚拟主机_localhost_03

Nginx之虚拟主机_listen_04

二:基于ip的虚拟主机

访问时通过ip地址访问

[root@zhu1 vhosts]# ifconfig eth0:1 192.168.70.135
[root@zhu1 vhosts]# ifconfig eth0:1 192.168.70.136

 

server {
listen 192.168.70.135:80;
server_name 192.168.70.135;
root /var/www/jiang;
index index.html index.htm ;
server_name www.jiang.com;
access_log /var/log/zhu.log;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/jiang$fastcgi_script_name;
}
}
~

 

server {
listen 192.168.70.136:80;
root /var/www/tao;
index index.html index.htm ;
server_name 192.168.70.136;
access_log /var/log/tao.log;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/tao$fastcgi_script_name;
}
}
~
~

三:基于端口的虚拟主机

 

server {
listen 8080;
root /var/www/jiang;
index index.html index.htm ;
server_name www.jiang.com;
access_log /var/log/zhu.log;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/jiang$fastcgi_script_name;
}
}

 

server {
listen 9999;
root /var/www/tao;
index index.html index.htm ;
server_name 192.168.70.136;
access_log /var/log/tao.log;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/tao$fastcgi_script_name;
}
}

 

访问:

http://192.168.70.134:8080/

http://192.168.70.134:9999/

基于端口的虚拟主机,配置时使用的是同一ip,只是监听端口不同,在访问时需要在ip地址后跟上端口

 

 

server {
listen      80;
server_name example.org www.example.org;
...
}
server {
listen      80;
server_name example.net www.example.net;
...
}
server {
listen      80;
server_name example.com www.example.com;
...
}

在上面的配置中nginx仅仅检查请求的host头来决定请求有那个虚拟主机来处理,

如果Host头没有匹配任意一个虚拟主机,或者请求中根本没有包含Host头,那nginx会将请求分发到定义在此端口上的默认虚拟主机。在以上配置中,第一个被列出的虚拟主机即nginx的默认虚拟主机——这是nginx的默认行为。而且,可以显式地设置某个主机为默认虚拟主机,即在"listen"指令中设置"default_server"参数: 

 

server {
listen      80 default_server;
server_name example.net www.example.net;
...
}