CentOS怎么配置Nginx虛擬主機

實驗環(huán)境

一臺最小化安裝的centos 7.3虛擬機

配置基本環(huán)境

1. 安裝nginx

yum?install?-y?epel-* yum?isntall?-y?nginx?vim

2. 建立虛機主機的站點根目錄

mkdir?/var/wwwroot mkdir?/var/wwwroot/site1 mkdir?/var/wwwroot/site2 echo?-e?"site1"?>>?/var/wwwroot/site1/index.html echo?-e?"site2"?>>?/var/wwwroot/site2/index.html

CentOS怎么配置Nginx虛擬主機

3. 關(guān)閉centos的防火墻

setenforce?0 systemctl?stop?firewalld systemctl?disable?firewalld

CentOS怎么配置Nginx虛擬主機

配置基于端口的虛擬主機

1. 編輯nginx配置文件

vim?/etc/nginx/conf.d/vhosts.conf

2. 添加以下內(nèi)容

server?{ ??listen?8081; ??root?/var/wwwroot/site1; ??index?index.html;  ??location?/?{ ??} } server?{ ?listen?8082; ?root?/var/wwwroot/site2; ?index?index.html;  ?location?/?{ ?} }

CentOS怎么配置Nginx虛擬主機

3. 啟動?nginx?服務(wù)

systemctl?start?nginx

4. 在宿主機訪問兩個站點

http://192.168.204.135:8081/
http://192.168.204.135:8082/

CentOS怎么配置Nginx虛擬主機?

CentOS怎么配置Nginx虛擬主機

配置基于域名的虛擬主機

1. 重新編輯nginx配置文件

vim?/etc/nginx/conf.d/vhosts.conf

2. 刪除原內(nèi)容,重新添加以下內(nèi)容

server?{ ??listen?80; ??server_name?site1.test.com; ??root?/var/wwwroot/site1; ??index?index.html;  ??location?/?{ ??} } server?{ ?listen?80; ?server_name?site2.test.com; ?root?/var/wwwroot/site2; ?index?index.html;  ?location?/?{ ?} }

CentOS怎么配置Nginx虛擬主機

3. 重啟?nginx?服務(wù)

systemctl?restart?nginx

4. 在windows上修改?hosts?文件

編輯?c:windowssystem32driversetchosts?文件,

添加以下內(nèi)容(根據(jù)實際情況自己修改)

192.168.204.135 site1.test.com

192.168.204.135 site2.test.com

CentOS怎么配置Nginx虛擬主機

5. 在宿主機訪問兩個站點

http://site1.test.com/
http://site2.test.com/

CentOS怎么配置Nginx虛擬主機?

CentOS怎么配置Nginx虛擬主機

配置基于ip的虛擬主機

1. 在虛擬機增加兩個ip地址

ifconfig?ens33:1?192.168.204.151 ifconfig?ens33:2?192.168.204.152

2. 重新編輯nginx配置文件

vim?/etc/nginx/conf.d/vhosts.conf

3. 刪除原內(nèi)容,重新添加以下內(nèi)容

server?{ ??listen?192.168.204.151:80; ??root?/var/wwwroot/site1; ??index?index.html;  ??location?/?{ ??} } server?{ ?listen?192.168.204.152:80; ?root?/var/wwwroot/site2; ?index?index.html;  ?location?/?{ ?} }

CentOS怎么配置Nginx虛擬主機

4. 重啟?nginx?服務(wù)

systemctl?restart?nginx

5. 在宿主機訪問兩個站點

http://192.168.204.151/
http://192.168.204.152/

CentOS怎么配置Nginx虛擬主機?

CentOS怎么配置Nginx虛擬主機

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點贊15 分享