2.nginx--配置虛擬主機
阿新 • • 發佈:2018-11-09
一、通過埠號區分虛擬主機:
1、進入/usr/local/nginx/confz 目錄中,vim nginx.conf 開啟這個檔案,能看到裡面配置了一個server節點,一起server節點就是一個虛擬主機
如:server { ---一個server節點就是一個虛擬主機 listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; --這個nginx安裝目錄下的html資料夾 index index.html index.htm; } }
2、使用notepad++的sftp外掛,連線nginx伺服器,修改nginx.conf檔案(因為要修改的內容較多,在window下修改方便一點)
怎麼安裝和使用sftp外掛見百度
3、在nginx.conf檔案中配置一個新的server節點,通過埠區分虛擬主機
server { listen 81; --這裡使用81埠 server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html81; --指向html81目錄,所以要在同級目錄下複製一份html81資料夾 index index.html index.htm; } }
4、啟動nginx後,訪問81埠,就能訪問html81資料夾下面的index.html
二、通過域名區分虛擬主機:
1、知識點:一個域名只對應一個ip地址,我們訪問別的網站,其實都是通過ip地址來訪問的。
當我們在瀏覽器輸入網站的域名時,電腦的DNS伺服器會把域名解析成ip地址來訪問。
但是,一個ip地址可以繫結多個域名
如果在本機的hosts檔案中配置了ip地址和域名的對應關係,則訪問域名就不會通過DNS伺服器去解析域名,會直接訪問
在host檔案中繫結的ip
2、在nginx.conf新配置兩個server節點,讓他們的埠號都是80,但是server_name不一樣,通過域名的不同,訪問不同的網站
server {
listen 80;
server_name www.test.com; --域名不同
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html-test; --訪問的資料夾資源不同
index index.html index.htm;
}
}
server {
listen 80;
server_name www.test1.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html-test1;
index index.html index.htm;
}
}
然後在自己本機的hosts檔案中分別配置 www.test.com、www.test1.com都繫結伺服器的ip,就能通過這兩個域名訪問伺服器中不同的資源