所謂虛擬主機,在 Web 服務裏就是一個獨立的網站站點,這個站點對應獨立的域名(也多是IP 或端口),具備獨立的程序及資源,能夠獨立地對外提供服務供用戶訪問。html
在 Nginx 中,使用一個 server{} 標籤來標識一個虛擬主機,一個 Web 服務裏能夠有多個虛擬主機標籤對,便可以同時支持多個虛擬主機站點。前端
虛擬主機有三種類型:基於域名的虛擬主機、基於端口的虛擬主機、基於 IP 的虛擬主機。nginx
域名的虛擬主機是生產環境中最經常使用的。vim
[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目錄app
[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;
}
建立每一個虛擬主機配置文件:curl
[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
[root@localhost conf]# /usr/local/nginx/sbin/nginx -t
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost html]# curl http://www.abc.com
http://www.abc.com
[root@localhost html]# curl http://blog.abc.com
http://blog.abc.com
[root@localhost html]# curl http://bbs.abc.com
http://bbs.abc.com
基於端口的虛擬主機生產環境很少見,只須要修改主機監聽端口就能夠了,域名相同也能夠,由於基於端口的虛擬主機就是他經過端口來惟一分區不通的虛擬主機的,只要端口不一樣就是不一樣的虛擬主機。測試
[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 81;
server_name www.abc.com;
location / {
root html/bbs;
index index.html index.htm;
}
}
server {
listen 82;
server_name www.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 80 81 82
> do
> mkdir ${n}
> echo "http://www.abc.com:${n}" > ${n}/index.html
> done
echo "127.0.0.1 www.abc.com" >> /etc/hosts
[root@localhost conf]# /usr/local/nginx/sbin/nginx -t
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost html]# curl http://www.abc.com:80
http://www.abc.com:80
[root@localhost html]# curl http://www.abc.com:81
http://www.abc.com:81
[root@localhost html]# curl http://www.abc.com:82
http://www.abc.com:82
基於 IP 的虛擬主機在生產環境中不常使用,只須要將基於域名的虛擬主機中的域名修改成 IP 就能夠了,前提是服務器有多個IP地址。若是須要不一樣的 IP 對應不一樣的服務,可在網站前端的負載均衡器上配置。網站
虛擬主機別名,就是爲虛擬主機設置除了主域名之外的一個或多個域名名字,這樣就能實現用戶訪問的多個域名對應同一個虛擬主機網站的功能。
[root@localhost conf]# cat vhosts/www.abc.com.conf
server {
listen 80;
server_name www.abc.com abc.com; # 這裏設置abc.com做爲別名
location / {
root html/www;
index index.html index.htm;
}
}