您好,登錄后才能下訂單哦!
這篇文章主要講解了“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
3. 關(guān)閉centos的防火墻
setenforce 0 systemctl stop firewalld systemctl disable firewalld
配置基于端口的虛擬主機(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 / { } }
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/
配置基于域名的虛擬主機(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 / { } }
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
5. 在宿主機(jī)訪問兩個(gè)站點(diǎn)
http://site1.test.com/
http://site2.test.com/
配置基于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 / { } }
4. 重啟 nginx
服務(wù)
systemctl restart nginx
5. 在宿主機(jī)訪問兩個(gè)站點(diǎn)
http://192.168.204.151/
http://192.168.204.152/
感謝各位的閱讀,以上就是“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)注!
免責(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)容。