Nginx虛擬主機配置詳解
一、虛擬主機介紹
虛擬主機使用的是特殊的軟硬件技術,它把一臺運行在因特網上的服務器主機分成一臺臺“虛擬”的主機,每臺虛擬主機都可以是一個獨立的網站,可以具有獨立的域名,具有完整的Intemet服務器功能(WWW、FTP、Email等),同一臺主機上的虛擬主機之間是完全獨立的。從網站訪問者來看,每一臺虛擬主機和一臺獨立的主機完全一樣。
利用虛擬主機,不用爲每個要運行的網站提供一臺單獨的Nginx服務器或單獨運行一組Nginx進程。虛擬主機提供了在同一臺服務器、同一組Nginx進程上運行多個網站的功能。
二、虛擬主機分類
nginx下,一個server標籤就是一個虛擬主機。
1、基於IP的虛擬主機(幾乎不用)
實現基於ip的虛擬主機,可以在一塊物理網卡上綁定多個lP地址。這樣就能夠在使用單一網卡的同一個服務器上運行多個基於IP的虛擬主機。設置IP別名也非常容易,只須配置系統上的網絡接口,讓它監聽額外的lP地址。
2、基於域名的虛擬主機(經常使用)
基於域名的虛擬主機是最常見的一種虛擬主機。只需配置你的DNS服務器或者編輯/etc/hosts加入虛擬域名,將每個主機名映射到正確的lP地址,然後配置Nginx服務器,令其識別不同的主機名就可以了。這種虛擬主機技術,使很多虛擬主機可以共享同一個lP地址,有效解決了lP地址不足的問題。所以,如果沒有特殊要求使你必須用一個基於lP的虛擬主機,最好還是使用基於域名的虛擬主機。
3、基於端口的虛擬主機(很少使用)
基於端口的虛擬主機配置,使用端口來區分,瀏覽器使用域名或ip地址後面加上端口號訪問。一般用於公司內部網站,外部網站的管理後臺)
三、虛擬主機的配置方法
1、基於域名的虛擬主機配置
需要配置的域名:www.long.com blog.long.com
對應的目錄爲:www和blog
創建虛擬主機目錄
[root@localhost ~]# mkdir /usr/local/nginx-1.12.1/html/blog
[root@localhost ~]# mkdir /usr/local/nginx-1.12.1/html/www
創建一個簡單的網頁文件
[root@localhost ~]# echo 'www.long.com' >/usr/local/nginx-1.12.1/html/www/index.html
[root@localhost ~]# echo 'blog.long.com' >/usr/local/nginx-1.12.1/html/blog/index.html
添加/etc/hosts域名解析
[root@localhost ~]# vim /etc/hosts
192.168.10.10 www.long.com
192.168.10.10 blog.long.com
修改nginx主配置文件,在http大括號中添加虛擬主機
[root@localhost ~]# vim /usr/local/nginx-1.12.1/conf/nginx.conf
server {
root html/www;
listen 80;
server_name www.long.com;
index index.html index.htm;
}
server {
root html/blog;
listen 80;
server_name blog.long.com;
index index.html index.htm;
}
重新加載nginx,然後測試
[root@localhost ~]# /usr/local/nginx-1.12.1/sbin/nginx -s reload
[root@localhost ~]# curl www.long.com
www.long.com
[root@localhost ~]# curl blog.long.com
blog.long.com
2、基於端口的虛擬主機配置
需要配置的端口:8080和8081
對應的網頁目錄:8080和8081
創建虛擬主機目錄
[root@localhost ~]# mkdir /usr/local/nginx-1.12.1/html/8080
[root@localhost ~]# mkdir /usr/local/nginx-1.12.1/html/8081
創建一個簡單的網頁文件
[root@localhost ~]# echo 'this is 8080' >/usr/local/nginx-1.12.1/html/8080/index.html
[root@localhost ~]# echo 'this is 8081' >/usr/local/nginx-1.12.1/html/8081/index.html
修改nginx主配置文件,在http大括號中添加虛擬主機
[root@localhost ~]# vim /usr/local/nginx-1.12.1/conf/nginx.conf
server {
root html/8080;
listen 8080;
server_name www.long.com;
index index.html index.htm;
}
server {
root html/8081;
listen 8081;
server_name www.long.com;
index index.html index.htm;
}
重新加載nginx,然後測試
[root@localhost ~]# /usr/local/nginx-1.12.1/sbin/nginx -s reload
[root@localhost ~]# curl 192.168.10.10:8080
this is 8080
[root@localhost ~]# curl 192.168.10.10:8081
this is 8081
3、基於IP的虛擬主機配置
需要配置的IP地址:192.168.10.10和192.168.10.11
對應的網頁目錄:192.168.10.10和192.168.10.11
創建虛擬主機目錄
[root@localhost ~]# mkdir /usr/local/nginx-1.12.1/html/192.168.10.10
[root@localhost ~]# mkdir /usr/local/nginx-1.12.1/html/192.168.10.11
創建一個簡單的網頁文件
[root@localhost ~]# echo '192.168.10.10' >/usr/local/nginx-1.12.1/html/192.168.10.10/index.html
[root@localhost ~]# echo '192.168.10.11' >/usr/local/nginx-1.12.1/html/192.168.10.11/index.html
配置一個子網IP地址
[root@localhost ~]# ifconfig eno16777736:1 192.168.10.11/24
修改nginx主配置文件,在http大括號中添加虛擬主機
[root@localhost ~]# vim /usr/local/nginx-1.12.1/conf/nginx.conf
server {
root html/192.168.10.10;
listen 192.168.10.10:80;
server_name www.long.com;
index index.html index.htm;
}
server {
root html/192.168.10.11;
listen 192.168.10.11:80;
server_name www.long.com;
index index.html index.htm;
}
重新加載nginx,然後測試
[root@localhost ~]# /usr/local/nginx-1.12.1/sbin/nginx -s reload
[root@localhost ~]# curl 192.168.10.10
192.168.10.10
[root@localhost ~]# curl 192.168.10.11
192.168.10.11
以上就是三種虛擬主機的配置方法。