在真實的服務器環境,爲了充分利用服務器資源,一臺nginx web服務器會同時配置N個虛擬主機,這樣可以充分利用服務器的資源,方便管理員的統一管理
配置nginx虛擬主機首先肯定要先部署咱們的nginx,具體nginx部署安裝請移步 nginx部署,配置nginx虛擬主機需要大家瞭解nginx配置文件以及nginx的各個目錄,若需要請移步 nginx目錄結構與配置文件詳解
配置nginx虛擬主機有三種方法:基於ip地址的虛擬主機、基於域名的虛擬主機以及基於端口的虛擬主機,下面爲大家逐一講解
基於域名的虛擬主機
基於域名的虛擬主機原理:相同IP地址,相同端口、不同的域名。也就是說多個虛擬主機之間共用一個ip地址以及一個端口(80),區分各個主機之間使用不同的域名,當然訪問的時候也就只能使用域名進行訪問了,基於域名的虛擬主機是最常用的方式
配置:
http{
......省略其他代碼
#第一臺虛擬主機
server {
listen 80;
server_name a.jkyst.xyz; #這裏域名一定不要重複
access_log logs/a.access.log; #日誌需求可以根據自己的要求去做,如果覺得日誌無所謂分不分開大可以放到一起
location{
root html/a; #這裏是網站的根目錄,注意爲了測試一定要分開,裏面寫上不同的html
index index.html index.htm;
}
......這裏省略其他代碼
}
#第二臺虛擬主機
server{
listen 80;
server_name b.jkyst.xyz;
access_log logs/b.access.log;
location{
root html/b;
index index.html index.htm;
}
......這裏省略其他代碼
}
#第三臺虛擬主機
server{
listen 80;
server_name c.jkyst.xyz;
access_log logs/c.access.log;
location{
root html/c;
index index.html index.htm;
}
}
}
基於端口的虛擬主機
基於端口的虛擬主機原理:相同IP地址,相同域名,不同的端口;也就是說多個虛擬主機之間擁有相同的IP地址和域名,使用端口不同的方式區分不同虛擬主機,當然訪問的時候就不可以使用默認的端口(80)去訪問了
配置
http{
......省略其他代碼
#第一臺虛擬主機
server {
listen 8000;
server_name www.jkyst.xyz;
access_log logs/a.access.log;
location{
root html/a;
index index.html index.htm;
}
......省略其他代碼
}
#第二臺虛擬主機
server{
listen 8001;
server_name www.jkyst.xyz;
access_log logs/b.access.log;
location{
root html/b;
index index.html index.htm;
}
......省略其他代碼
}
#第三臺虛擬主機
server{
listen 8002;
server_name www.jkyst.xyz;
access_log logs/c.access.log;
location{
root html/c;
index index.html index.htm;
}
......省略其他代碼
}
}
基於IP的虛擬主機
基於IP地址的虛擬主機原理:相同端口,相同域名,不同的IP地址;也就是說多個虛擬主機之間擁有相同的端口和域名,使用IP地址不同的方式區分不同虛擬主機,當然訪問的時候就需要使用不同IP或者域名去訪問了,綁定域名時也就需要不同域名綁定不同ip地址,但不可一個域名綁定多個ip
配置
http{
......省略其他代碼
#第一臺虛擬主機
server {
listen IP地址:80;
server_name a.jkyst.xy或者ip地址;
access_log logs/a.access.log;
location{
root html/a;
index index.html index.htm;
}
......省略其他代碼
}
#第二臺虛擬主機
server{
listen IP地址:80;
server_name b.jkyst.xyz或者IP地址;
access_log logs/b.access.log;
location{
root html/b;
index index.html index.htm;
}
......省略其他代碼
}
#第三臺虛擬主機
server{
listen IP地址:80;
server_name c.jkyst.xyz或者IP地址;
access_log logs/c.access.log;
location{
root html/c;
index index.html index.htm;
}
......省略其他代碼
}
}
實戰
我這裏爲大家演示一種虛擬主機的實現,因爲基於域名的虛擬主機比較常見這裏就爲大家演示這種虛擬主機
環境介紹
操作系統版本:
root@jia:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 19.10
Release: 19.10
Codename: eoan
root@jia:~# cat /proc/version
Linux version 5.3.0-18-generic (buildd@lcy01-amd64-027) (gcc version 9.2.1 20190909 (Ubuntu 9.2.1-8ubuntu1)) #19-Ubuntu SMP Tue Oct 8 20:14:06 UTC 2019
nginx版本:
root@jia:~# nginx -v
nginx version: nginx/1.16.1 (Ubuntu)
配置nginx.conf
配置文件位置:/etc/nginx/conf.d/*.conf
nginx安裝方式不同位置不同,具體請查看自己的配置文件位置
下面是配置文件內容:
root@jia:/var/www/html# cat /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name a.jkyst.xyz;
location / {
root /var/www/html/a;
index index.html index.htm;
}
}
server {
listen 80;
server_name b.jkyst.xyz;
location / {
root /var/www/html/b;
index index.html index.htm;
}
}
server {
listen 80;
server_name c.jkyst.xyz;
location / {
root /var/www/html/c;
index index.html index.htm;
}
記住修改配置文件後一定要重新啓動nginx
root@jia:~# systemctl restart nginx
創建主頁文件
首先創建存放主頁html文件的目錄
root@jia:~# cd /var/www/html/
root@jia:/var/www/html# mkdir a b c
#生成主頁文件
root@jia:/var/www/html# echo a.jkyst.xyz > a/index.html
root@jia:/var/www/html# echo b.jkyst.xyz > b/index.html
root@jia:/var/www/html# echo c.jkyst.xyz > c/index.html
修改host文件
這裏修改hosts文件需要注意一下,在那個PC上面驗證就在那個PC上面修改host文件
windows主機hosts文件路徑:C:\Windows\System32\drivers\etc
linux版本hosts文件路徑:/etc/
需要寫入的內容
127.0.0.1 a.jkyst.xyz //127.0.0.1是nginx服務器的IP地址,我這裏使用的是同一臺PC
127.0.0.1 b.jkyst.xyz
127.0.0.1 c.jkyst.xyz
測試
root@jia:~# curl a.jkyst.xyz
a.jkyst.xyz
root@jia:~# curl b.jkyst.xyz
b.jkyst.xyz
root@jia:~# curl c.jkyst.xyz
c.jkyst.xyz
OK發現訪問的都是我想要得到的域名,證明基於域名的虛擬主機配置成功