溫馨提示×

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

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

nginx基于端口如何配置虛擬主機(jī)

發(fā)布時(shí)間:2022-10-19 14:08:07 來(lái)源:億速云 閱讀:108 作者:iii 欄目:建站服務(wù)器

這篇文章主要介紹“nginx基于端口如何配置虛擬主機(jī)”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“nginx基于端口如何配置虛擬主機(jī)”文章能幫助大家解決問(wèn)題。

一、創(chuàng)建網(wǎng)站目錄及文件:

[root@localhost data]# tree /data
/data
└── wwwroot
    ├── www.1.com_8080
    │   └── index.html
    └── www.1.com_8081
        └── index.html

二、修改nginx.conf:

[root@localhost nginx]# vim /usr/local/nginx/conf/nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    keepalive_timeout  65;
    include vhost/*.conf;   #vhost目錄下會(huì)包含所有的虛擬主機(jī)的配置文件
}

三、創(chuàng)建虛擬主機(jī)的配置文件目錄:

[root@localhost conf]mkdir /usr/local/nginx/conf/vhost

四、創(chuàng)建虛擬主機(jī)配置文件:

[root@localhost nginx]# vim /usr/local/nginx/conf/vhost/www.1.com.8080.conf 
server{
    listen 8080;
    server_name 1.com www.1.com;
    index index.html;
    root /data/wwwroot/www.1.com_8080;
}
[root@localhost nginx]# vim /usr/local/nginx/conf/vhost/www.1.com.8081.conf  
server{
    listen 8081;
    server_name 1.com www.1.com;
    index index.html;
    root /data/wwwroot/www.1.com_8081;
}
[root@localhost nginx]# vim /usr/local/nginx/conf/vhost/default.conf          
server{
    listen 80 default_server;  #使用default_server指定nginx的默認(rèn)虛擬主機(jī)
    deny all;
}

若使用其他域名來(lái)訪問(wèn)虛擬主機(jī)時(shí),會(huì)匹配到默認(rèn)虛擬主機(jī),該配置會(huì)拒絕未定義的域名的虛擬主機(jī)。若不配置該選項(xiàng),默認(rèn)排在最前邊的server會(huì)成為默認(rèn)虛擬主機(jī)。

五、測(cè)試配置文件是否存在問(wèn)題:

[root@localhost root]# cd /usr/local/nginx/sbin
[root@localhost sbin]# ./nginx -t 
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file

六、當(dāng)配置文件修改時(shí),可以使用一下命令重新加載配置文件

[root@localhost sbin]# ./nginx -s reload

關(guān)于“nginx基于端口如何配置虛擬主機(jī)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。

向AI問(wèn)一下細(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