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
2.4 重新加載 Nginx 配置
[root@localhost conf]# /usr/local/nginx/sbin/nginx -t
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload
2.5 訪問測試
[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
3.基於端口的虛擬主機
基於端口的虛擬主機生產環境不多見,只需要修改主機監聽端口就可以了,域名相同也可以,因為基於端口的虛擬主機就是他通過端口來唯一分區不通的虛擬主機的,只要端口不同就是不同的虛擬主機。
3.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 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;
}
}
}
3.2 創建虛擬主機站點對應的目錄和文件
[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
3.3 編輯 /etc/hosts 文件,域名解析
echo "127.0.0.1 www.abc.com" >> /etc/hosts
3.4 重新加載 Nginx 配置
[root@localhost conf]# /usr/local/nginx/sbin/nginx -t
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload
3.5 訪問測試
[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
4.基於 IP 的虛擬主機
基於 IP 的虛擬主機在生產環境中不常使用,只需要將基於域名的虛擬主機中的域名修改為 IP 就可以了,前提是服務器有多個IP地址。如果需要不同的 IP 對應不同的服務,可在網站前端的負載均衡器上配置。
5.虛擬主機別名配置
虛擬主機別名,就是為虛擬主機設置除了主域名以外的一個或多個域名名字,這樣就能實現用戶訪問的多個域名對應同一個虛擬主機網站的功能。
[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;
}
}
