溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

CentOS怎么配置Nginx虛擬主機(jī)

發(fā)布時(shí)間:2022-04-12 15:37:46 來(lái)源:億速云 閱讀:238 作者:iii 欄目:編程語(yǔ)言

這篇文章主要講解了“CentOS怎么配置Nginx虛擬主機(jī)”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“CentOS怎么配置Nginx虛擬主機(jī)”吧!

實(shí)驗(yàn)環(huán)境

一臺(tái)最小化安裝的centos 7.3虛擬機(jī)

配置基本環(huán)境

1. 安裝nginx

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

2. 建立虛機(jī)主機(jī)的站點(diǎn)根目錄

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虛擬主機(jī)

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

setenforce 0
systemctl stop firewalld
systemctl disable firewalld

CentOS怎么配置Nginx虛擬主機(jī)

配置基于端口的虛擬主機(jī)

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虛擬主機(jī)

3. 啟動(dòng) nginx 服務(wù)

systemctl start nginx

4. 在宿主機(jī)訪問兩個(gè)站點(diǎn)

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

CentOS怎么配置Nginx虛擬主機(jī) 

CentOS怎么配置Nginx虛擬主機(jī)

配置基于域名的虛擬主機(jī)

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虛擬主機(jī)

3. 重啟 nginx 服務(wù)

systemctl restart nginx

4. 在windows上修改 hosts 文件

編輯 c:\windows\system32\drivers\etc\hosts 文件,

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

192.168.204.135 site1.test.com

192.168.204.135 site2.test.com

CentOS怎么配置Nginx虛擬主機(jī)

5. 在宿主機(jī)訪問兩個(gè)站點(diǎn)

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

CentOS怎么配置Nginx虛擬主機(jī) 

CentOS怎么配置Nginx虛擬主機(jī)

配置基于ip的虛擬主機(jī)

1. 在虛擬機(jī)增加兩個(gè)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虛擬主機(jī)

4. 重啟 nginx 服務(wù)

systemctl restart nginx

5. 在宿主機(jī)訪問兩個(gè)站點(diǎn)

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

CentOS怎么配置Nginx虛擬主機(jī) 

CentOS怎么配置Nginx虛擬主機(jī)

感謝各位的閱讀,以上就是“CentOS怎么配置Nginx虛擬主機(jī)”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)CentOS怎么配置Nginx虛擬主機(jī)這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI