nginx學習筆記之基於端口的虛擬主機基於主機名的虛擬主機root、alias、index配置 實驗環境: centos 測試節點IP:172.16.3.101 基於端口的虛擬主機: vim /etc/nginx/nginx.conf # 向裏面的http {}裏面加入如下內容 server { # server定義一個虛擬主機 listen 8080; # 監聽本機所有IP端口8080 server_name www.test.com; # 虛擬主機名爲:www.test.com location / { # 根據用戶請求的URI決定是否匹配該location,如若匹配到,則將被該location中的配置所處理 root "/web/htdocs"; # web資源路徑映射 } } # 保存退出 # 創建目錄/web/htdocs mkdir -pv /web/htdocs vim /web/htdocs/index.html # 向裏面加入如下內容 hello,my serser_name is www.test.com,port is 8080 # 保存退出 # 測試,在遠端瀏覽器分別輸入:http://172.16.3.101 和 http://172.16.3.101:8080 # 如果顯示對應的結果,則表明基於端口的虛擬主機配置成功 配置基於主機名的虛擬主機 vim /etc/nginx/nginx.conf # 向裏面的http{}裏面加入如下內容 server { # server定義一個虛擬主機 listen 80; # 監聽本機所有IP端口:80 server_name www.test.com; # 虛擬主機名爲:www.test.com location / { # 根據用戶請求的RUL決定是否匹配該location,如果匹配到,則將被該location中的配置所處理 root "/web/htdocs"; # web資源路徑映射 } } server { # server定義一個虛擬主機 listen 80; # 監聽本機所有IP端口:80 server_name mail.test.com; # 虛擬主機名爲:mail.test.com location / { # 上面已經說了,此處不再重複 root "/web/mail"; # web資源路徑映射 } } mkdir -pv /web/mail vim /web/mail/index.html # 向裏面加入如下內容 hello,my server_name is mail.test.com # 保存退出 vim /web/htdocs/index.html # 向裏面加入如下內容 hello,my server_name is www.test.com # 保存退出 # 檢查其語法 [root@localhost conf]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful # 重啓nginx【由於此處修改了端口號,所以需要重啓nginx】 [root@localhost conf]# service nginx restart nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful Stopping nginx: [ OK ] Starting nginx: [ OK ] # 配置解析文件/etc/hosts【這裏訪問測試還是在本機進行的,所以我就修改本機的hosts文件】 vim /etc/hosts # 向裏面加入如下內容 172.16.3.101 www.test.com # 把主機名www.test.com解析爲172.16.3.101 172.16.3.101 mail.test.com # 把主機名mail.test.com解析爲172.16.3.101 # 保存退出 # 測試【在本機直接測試就行,前面已說,hosts文件已經修改好】 # 測試www.test.com虛擬主機 [root@localhost conf]# elinks -dump http://www.test.com hello,my server_name is www.test.com # 可知,www.test.com虛擬主機正常 # 測試mail.test.com虛擬主機 [root@localhost conf]# elinks -dump http://mail.test.com hello,my server_name is mail.test.com # 可知,mail.test.com虛擬主機正常 root和alias的區別: 我這裏先不說,且看下面的例子 vim /etc/nginx/nginx.conf # 把如下語句加入http{}裏面 server { listen 80; server_name www.test.com; location /root { root "/web/htdocs"; } } server { listen 80; server_name mail.test.com; location /alias { alias "/web/mail"; } } # 對於虛擬主機www.test.com mkdir -pv /web/htdocs/root/ vim /web/htdocs/root/index.html # 向裏面加入如下語句 hello,this is root type. # 保存,退出 # 對於虛擬主機mail.test.com mkdir -pv /web/mail vim /web/mail/index.html # 向裏面加入如下語句 hello,this is alias type. # 保存退出 # 語法檢查 nginx -t # nginx服務重啓 service nginx restart # 配置解析文件/etc/hosts,和上面那個實驗做得一樣,這裏就不重複啦 # 測試【直接在本機測試】 # 測試www.test.com虛擬主機 [root@localhost root]# elinks -dump http://www.test.com/root/ hello,this is root type. # 可知root類型是這樣訪問的,是這樣起作用的,其對應訪問路徑爲:/web/htdocs/root/index.html,不用我多說了吧。 # 測試mail.test.com虛擬主機 [root@localhost root]# elinks -dump http://mail.test.com/alias hello,this is alias type. # 可知alias類型是這樣訪問的,是這樣起作用的,其對應訪問路徑爲:/web/mail/index.html,不用我解釋了吧。 index配置:設置默認主頁面 看下面的操作吧 vim /etc/nginx/nginx.conf # 把如下內容放入裏面 server { listen 80; server_name www.test.com; location /root { root "/web/htdocs"; } } # nginx 語法檢查 nginx -t # nginx 服務重啓 service nginx restart # 創建其web資源路徑和文件 mkdir -pv /web/htdocs/root/ vim /web/htdocs/root/index.html # 向裏面加入如下語句 hello,this is root type. # 保存,退出 # 配置解析文件/etc/hosts vim /etc/hosts # 把如下語句加入其中 172.16.3.101 www.test.com # 保存退出 # 訪問【在本機測試訪問就行】 # 輸入如下語句,這裏沒有輸入主頁文件,但是也能正常訪問,可知nginx默認主頁文件爲index.html [root@localhost root]# elinks -dump http://www.test.com/root/ hello,this is root type. vim /etc/nginx/nginx.conf # 把剛纔加入的語句換成如下語句 server { listen 80; server_name www.test.com; location /root { root "/web/htdocs"; index test.html } } # nginx語法檢查 nginx -t # nginx 服務重啓 service nginx restart # 訪問【在本機測試訪問就行】 # 輸入如下語句,這裏沒有輸入主頁文件,但是不能正常訪問了 [root@localhost root]# elinks -dump http://www.test.com/root 403 Forbidden -------------------------------------------------------------------------- nginx/1.6.2 # 現在我把主頁文件名更改一下 mv /web/htdocs/root/index.html /web/htdocs/root/test.html # 再訪問一下 # 輸入如下語句,這裏沒有輸入主頁文件,可以正常訪問了 [root@localhost root]# elinks -dump http://www.test.com/root hello,this is root type. 現在應該明白index的作用了吧
nginx學習筆記之基於端口的虛擬主機基於主機名的虛擬主機root、alias、index配置
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.