我有一個問題,當我反向代理頁面時,文件將找不到相對路徑。
工作範例:
https://lfportal.mohavecounty.us/bos/search.aspx?dbid=0&searchcommand=%28%7B%5BBOS%20Agenda%20Packets%5D%3A%5BMeeting%20Date%5D%3D%2211/23/2020%22%7D%20%26%20%7B%5BBOS%20Agenda%20Packets%5D%3A%5BMeeting%20Type%5D%3D%22Special%22%7D%20%26%20%7B%5BBOS%20Agenda%20Packets%5D%3A%5BItem%20Number%5D%3D%22Item%20001%22%7D%29%20
重定向到搜尋結果:
https://lfportal.mohavecounty.us/bos/0/doc/1652027/Page1.aspx
在檔案 Page1.aspx 上有使用相對路徑的路徑,如下例所示
<img id="A_T0_0_1" unselectable="on" src="../../../Helper/TileData.aspx?reposName=MohaveDocs&docID=1652027&x=0&y=1&pageNum=1&scale=3782&ro=0&time=1607098159588&showAnn=1&pageID=7493796&search=">
現在我的重定向
https://lfdocs.mohave.gov/bos/search.aspx?dbid=0&searchcommand=%28%7B%5BBOS%20Agenda%20Packets%5D%3A%5BMeeting%20Date%5D%3D%2211/23/2020%22%7D%20%26%20%7B%5BBOS%20Agenda%20Packets%5D%3A%5BMeeting%20Type%5D%3D%22Special%22%7D%20%26%20%7B%5BBOS%20Agenda%20Packets%5D%3A%5BItem%20Number%5D%3D%22Item%20001%22%7D%29%20
頁面加載但由於失敗而無法繼續。它分配了 404 並在 Chrome 開發人員中查看上圖之一的位址,404 錯誤顯示了該位址:
https://lfdocs.mohave.gov/Helper/TileData.aspx?reposName=MohaveDocs&docID=1652027&x=0&y=1&pageNum=1&scale=3782&ro=0&time=1607098058369&showAnn=1&pageID=7493796&search=
所以看來根資料夾 bos 遺失了。 https://lfdocs.mohave.gov/Helper/TileData.aspx?
應該是網址: https://lfportal.mohavecounty.us/bos/Helper/TileData.aspx?
所以看來我的代理傳遞沒有正確處理相對屬性。下面是我的範例 Nginx 網站設定。如果條件滿足,則問題發生在第二個
if ($saved_redirect_location !~* "10.4.1.81") { .. }
我的conf檔:
server{
listen 443 ssl http2; # default_server;
server_name lfdocs.mohave.gov;
access_log /var/log/nginx/lfdocs_mohave_gov_access.log;
error_log /var/log/nginx/lfdocs_mohave_gov_error.log info;
include /etc/nginx/sites-available/mohave_gov_ssl.conf;
location / {
proxy_buffers 16 4k;
proxy_buffer_size 2k;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Port 443;
proxy_pass http://10.4.1.81/;
# This is used to handle the multiple redirect 301 that the server is doing
proxy_intercept_errors on;
error_page 301 302 307 = @handle_redirects;
}
location @handle_redirects {
set $saved_redirect_location '$upstream_http_location';
if ($saved_redirect_location ~* "10.4.1.81") {
add_header X-debug-message 1$saved_redirect_location always;
proxy_pass $saved_redirect_location;
}
if ($saved_redirect_location !~* "10.4.1.81") {
add_header X-debug-message http://10.4.1.81$saved_redirect_location always;
proxy_pass http://10.4.1.81$saved_redirect_location;
}
}
}
在 Chrome 開發人員中,我查看自訂標頭 x-debug-message 並具有以下值:
x-debug-message: http://10.4.1.81/bos/0/doc/1652027/Page1.aspx
我嘗試從我讀過的文章中刪除位置部分下的尾部斜杠,但它不起作用。
關於為什麼相對路徑未加載的任何想法?
答案1
相對路徑由您的後端應用程式新增。 nginx 不會以任何方式觸及路徑。
如果您的後端應用程式使用相對路徑,則您的前端 URL 必須與後端 URL 具有相同的深度。
在你的第一個例子:
https://lfportal.mohavecounty.us/bos/0/doc/1652027/Page1.aspx
該 URL 位於第四個子目錄層級。這意味著瀏覽器可以解析../../../
為/bos
.
在你的第二個例子中:
https://lfdocs.mohave.gov/bos/search.aspx?dbid=0&searchcommand=%28%7B%5BBOS%20Agenda%20Packets%5D%3A%5BMeeting%20Date%5D%3D%2211/23/2020%22%7D%20%26%20%7B%5BBOS%20Agenda%20Packets%5D%3A%5BMeeting%20Type%5D%3D%22Special%22%7D%20%26%20%7B%5BBOS%20Agenda%20Packets%5D%3A%5BItem%20Number%5D%3D%22Item%20001%22%7D%29%20
該 URL 位於第二個子目錄層級。現在,瀏覽器嘗試解析../../../
URL,最終得到的是/
URL。
我的建議是您將相對 URL 替換為網站根相對 URL:/bos/Helper/TileData.aspx?reposName=MohaveDocs&docID=1652027&x=0&y=1&pageNum=1&scale=3782&ro=0&time=1607098159588&showAnn=1&pageID=7493796&search
URL 的另一個問題是「&」字元是 URL 編碼的,這可能會導致不期望的最終結果。