這篇文章主要介紹“nginx基于端口如何配置虛擬主機”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“nginx基于端口如何配置虛擬主機”文章能幫助大家解決問題。
一、創建網站目錄及文件:
[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目錄下會包含所有的虛擬主機的配置文件
}三、創建虛擬主機的配置文件目錄:
[root@localhost conf]mkdir /usr/local/nginx/conf/vhost
四、創建虛擬主機配置文件:
[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的默認虛擬主機
deny all;
}若使用其他域名來訪問虛擬主機時,會匹配到默認虛擬主機,該配置會拒絕未定義的域名的虛擬主機。若不配置該選項,默認排在最前邊的server會成為默認虛擬主機。
五、測試配置文件是否存在問題:
[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
六、當配置文件修改時,可以使用一下命令重新加載配置文件
[root@localhost sbin]# ./nginx -s reload
關于“nginx基于端口如何配置虛擬主機”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。