Nginx之多站點設定(7)
阿新 • • 發佈:2022-05-11
在真實的伺服器環境裡,為了充分利用伺服器資源,一臺nginx web伺服器同時會配置N個虛擬域名主機,即多個域名對於同樣一個80埠。然後伺服器IP數量很多,也可以配置基於多個IP對應同一個埠。這裡介紹兩個種類,方法基本相同,實現效果不同。
方法1:同個埠,訪問域名不同,訪問頁面不同
vim修改nginx.conf server段配置如下:
server { listen 80; server_name www.a.com; #access_log logs/host.access.log main; location / { root html/a; index index.html index.htm; } } server { listen 80; server_name www.b.com; #access_log logs/hosts.access.log main; location / { root html/b; index index.html index.htm; } }
解釋:建立兩個不同的目錄mkdir -p /usr/local/nginx/html/{a,b}
,然後分別在兩個目錄建立兩個不同的index.html網站頁面即可。通過客戶端配置hosts指向兩個域名,然後在IE瀏覽器訪問測試效果。
方法2:同個埠,訪問一級域名相同,二級域名不同,訪問頁面不同。(根據域名後端區分訪問頁面)
vim修改nginx.conf server段配置如下:
server { listen 8089; #這裡使用非常規埠,只需要解析到一下使用到的sjb.suhuayue.com域名中即可 server_name sjb.suhuayue.com _; access_log logs/host.access.log main; error_log logs/error.log; location / { index index.html index.htm; } location ^~ /a/ { add_header X-Frame-Options DENY; proxy_set_header X-Client-Really-IP $remote_addr; alias /opt/qypay/a/; index index.html index/htm; } location ^~ /b/ { add_header X-Frame-Options DENY; proxy_set_header X-Client-Really-IP $remote_addr; alias /opt/qypay/b/; index index.html index/htm; } }
解釋:以上配置成功後,sjb.suhuayue.com/a/為頁面一,sjb.suhuayue.com/b/為頁面二。
注意:
1、以上配置檔案中alias /opt/qypay/b/;
這裡必須以/
結尾,因為指定的是一個目錄
2、如果顯示頁面的時候,頁面能讀取到,但是顯示不出來,那麼就是解析的問題,可配合前端進行詢問前端為哪種解析
3、訪問域名的時候,必須以/
結尾,如:http://alias /opt/qypay/b/
是正確的,http://alias /opt/qypay/b
是錯誤的