前面講了如何配置基於IP的虛擬主機,大家可以去這里看看nginx系列文章:https://www.cnblogs.com/zhangweizhong/category/1529997.html
今天就來講講Nginx如何基於端口的虛擬主機。
需要說明的是:由於本文章是nginx系列文章中的一篇,文章里面很多其他的配置,可能前面的文章已經說講過,然后后續就沒有在介紹,如果出現有些配置沒有講,大家可能需要去看看前面的文章。
應用場景
nginx對外提供81和82兩個端口監聽服務。
請求81端口則請求html81目錄下的html
請求82端口則請求html82目錄下的html
准備環境
1. 創建192.168.78.132虛擬機,保證本地電腦和虛擬網絡通暢。
2. 在192.168.78.132上安裝nginx。
html目錄創建
將原來nginx的html目錄拷貝兩個目錄 html81和html82,為了方便測試需要修改每個目錄下的index.html內容使之個性化。
配置虛擬主機
修改/usr/local/nginx/conf/nginx.conf文件,添加兩個虛擬主機,如下:vi /usr/local/nginx/conf/nginx.conf
#user nobody; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; #配置虛擬主機 server { #監聽的ip和端口,配置80 listen 80; #虛擬主機名稱這里配置ip地址 server_name 192.168.101.3; #所有的請求都以/開始,所有的請求都可以匹配此location location / { #使用root指令指定虛擬主機目錄即網頁存放目錄 #比如訪問http://ip/test.html將找到/usr/local/html3/test.html #比如訪問http://ip/item/test.html將找到/usr/local/html3/item/test.html root /usr/local/nginx/html80; #指定歡迎頁面,按從左到右順序查找 index index.html index.htm; } } #配置虛擬主機 server { listen 8080; server_name 192.168.101.3; location / { root /usr/local/nginx/html8080; index index.html index.htm; } } }
測試
重新加載配置nginx配置文件,查看端口監聽狀態:
訪問http://192.168.78.132:81
訪問http://192.168.78.132:82
最后
以上,就把nginx 基於ip的配置虛擬主機講完了。后面會繼續講基於域名配置虛擬主機。