>/var/wwwroot/site1/index.htmlecho-e"site2">>/var/"/> >/var/wwwroot/site1/index.htmlecho-e"site2">>/var/">

首頁  >  文章  >  運維  >  CentOS怎麼設定Nginx虛擬主機

CentOS怎麼設定Nginx虛擬主機

PHPz
PHPz轉載
2023-05-30 21:45:291361瀏覽

實驗環境

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

設定基本環境

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. 關閉centos的防火牆

setenforce 0
systemctl stop firewalld
systemctl disable firewalld

CentOS怎麼設定Nginx虛擬主機

設定基於連接埠的虛擬主機

1. 編輯nginx設定檔

<pre class="brush:bash;">vim /etc/nginx/conf.d/vhosts.conf</pre>

2. 新增以下內容

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 服務

systemctl start nginx

4. 在宿主機器存取兩個網站

http://192.168 .204.135:8081/
#http://192.168.204.135:8082/

CentOS怎麼設定Nginx虛擬主機 

CentOS怎麼設定Nginx虛擬主機

CentOS怎麼設定Nginx虛擬主機#設定基於網域的虛擬主機

1. 重新編輯nginx設定檔<pre class="brush:bash;">vim /etc/nginx/conf.d/vhosts.conf</pre>

2. 刪除原始內容,重新新增以下內容

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 / {
 }
}

3. 重啟 nginx 服務

<pre class="brush:bash;">systemctl restart nginx</pre>

4. 在windows上修改 

hosts

 檔案#編輯 

c:\windows\system32\drivers\etc\hosts

 文件,新增以下內容(依實際情況自行修改)

CentOS怎麼設定Nginx虛擬主機#192.168.204.135 site1 .test.com

192.168.204.135 site2.test.com
##5. 在宿主機上訪問兩個網站

CentOS怎麼設定Nginx虛擬主機http://site1.test.com/

CentOS怎麼設定Nginx虛擬主機http://site2.test.com/

 

配置基於ip的虛擬主機

CentOS怎麼設定Nginx虛擬主機1. 在虛擬機器增加兩個ip位址

ifconfig ens33:1 192.168.204.151
ifconfig ens33:2 192.168.204.152

2. 重新編輯nginx設定檔<pre class="brush:bash;">vim /etc/nginx/conf.d/vhosts.conf</pre>3. 刪除原始內容,重新新增下列內容

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 / {
 }
}

4. 重新啟動 nginx
 服務<pre class="brush:bash;">systemctl restart nginx</pre>5. 在宿主機造訪兩個網站

http://192.168.204.151/CentOS怎麼設定Nginx虛擬主機

#http://192.168. 204.152/CentOS怎麼設定Nginx虛擬主機

########## #############

以上是CentOS怎麼設定Nginx虛擬主機的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除