好的,所以我在我的 Ubuntu VPS 中遇到了一個不尋常的問題。訪問一個域會顯示另一個域的靜態 html 頁面。讓我們看看組態檔。
假設我有一個域為:site1.com 的站點。該域有 2 個應用在子域中完美運行:app1.site1.com 和 app2.site1.com。主頁沒有任何應用程式,只有一個簡單的 html 頁面,上面寫著:
'Welcome to Site 1. This site is currently under maintenance!'.
這是我想要的完美運行,但是在我將 Namecheap 中的新域(我們稱他為 site2.com)指向我的 VPS 后,問題出現了。現在,當我訪問 site2.com 時,我可以看到:
'Welcome to Site 1. This site is currently under maintenance!'.
那是一些真正的伏都教魔法。
這是 site1 nginx 組態檔的樣子:
server {
root /var/www/html/site1.com;
index index.html index.htm index.nginx-debian.html;
server_name site1.com www.site1.com;
location / {
try_files $uri $uri/ =404;
}
}
server {
root /var/www/app1.site1.com;
index index.html index.htm index.nginx-debian.html;
server_name app1.site1.com;
location / {
#try_files $uri $uri/ =404;
proxy_pass http://localhost:4000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
}
server {
root /var/www/app2.site1.com;
index index.html index.htm index.nginx-debian.html;
server_name app2.site1.com;
location / {
#try_files $uri $uri/ =404;
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
}
html檔案在 /var/www/html/site1.com/index.html
魔法的原因可能是什么?
我還嘗試為新域創建一個新的 nginx 檔案并創建一個新的服務器塊并指向它自己的 html 檔案,但它只是不顯示該 html 頁面,而是顯示 site1 頁面。
如果有人指出我正確的方向,那將非常有幫助。
uj5u.com熱心網友回復:
這通常發生在 Nginx 無法找到顯示另一個頁面的域的服務器塊時。因此 Nginx 會自動提供第一個可用的組態檔,在您的情況下是 site1 的頁面。
為避免這種情況,您可以nginx.conf使用以下配置在檔案中設定默認服務器:
server {
listen 80 default_server;
listen 443 ssl default_server;
server_name _;
ssl_certificate <path to cert>
ssl_certificate_key <path to key>
return 404;
}
當 Nginx 找不到與您的域匹配的任何服務器塊時,它會自動提供 404 頁面。
因此,請執行以下步驟:
- 在 .html 中添加一個 html 頁面
/var/www/html/site2.com。 - 為 site2 創建一個組態檔,并將其指向服務器塊中的 html 頁面。
- 安裝 SSL。
- 重啟 Nginx。
出現問題的另一個原因是 SSL。因此,請確保通過將 SSL 重新安裝到 site2 來重試。
有關更多資訊,請參閱此和此。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/347329.html
標籤:乌本图 nginx nginx-反向代理 nginx配置 ubuntu-20.04
上一篇:Haskell-GHC錯誤:找不到模塊“Prelude”/也許您還沒有安裝包“base-4.12.0.0”的分析庫?
