Nginx处理相对路径会导致404错误

分享于 

6分钟阅读

互联网

  繁體

问题:

工作示例:


https://lfportal.mohavecounty.us/bos/search.aspx?dbid=0&searchcommand=%28{%5BBOS%20Agenda%20Packets%5D%3A%5BMeeting%20Date%5D%3D%2211/23/2020%22}%20&%20{%5BBOS%20Agenda%20Packets%5D%3A%5BMeeting%20Type%5D%3D%22Special%22}%20&%20{%5BBOS%20Agenda%20Packets%5D%3A%5BItem%20Number%5D%3D%22Item%20001%22}%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&amp;docID=1652027&amp;x=0&amp;y=1&amp;pageNum=1&amp;scale=3782&amp;ro=0&amp;time=1607098159588&amp;showAnn=1&amp;pageID=7493796&amp;search=">



现在重定向


https://lfdocs.mohave.gov/bos/search.aspx?dbid=0&searchcommand=%28{%5BBOS%20Agenda%20Packets%5D%3A%5BMeeting%20Date%5D%3D%2211/23/2020%22}%20&%20{%5BBOS%20Agenda%20Packets%5D%3A%5BMeeting%20Type%5D%3D%22Special%22}%20&%20{%5BBOS%20Agenda%20Packets%5D%3A%5BItem%20Number%5D%3D%22Item%20001%22}%29%20



页面加载,但由于失败而无法继续,


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=



https://lfdocs.mohave.gov/Helper/TileData.aspx

应该是URL:https://lfportal.mohavecounty.us/bos/Helper/TileData.aspx

下面是示例Nginx site conf。问题发生在第二个条件


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:

相对路径由后端应用程序添加,

在第一个示例中:


https://lfportal.mohavecounty.us/bos/0/doc/1652027/Page1.aspx



URL位于第四次目录级别,这意味着浏览器可以将../../../解析为/bos

在第二个示例中:


https://lfdocs.mohave.gov/bos/search.aspx?dbid=0&searchcommand=%28{%5BBOS%20Agenda%20Packets%5D%3A%5BMeeting%20Date%5D%3D%2211/23/2020%22}%20&%20{%5BBOS%20Agenda%20Packets%5D%3A%5BMeeting%20Type%5D%3D%22Special%22}%20&%20{%5BBOS%20Agenda%20Packets%5D%3A%5BItem%20Number%5D%3D%22Item%20001%22}%29%20



URL位于第二个子目录级别,现在,浏览器试图解析../../../ URL,它最终是/ URL。

我建议将相对URL替换为站点根相对URL:/bos/Helper/TileData.aspx?reposName=MohaveDocs&amp;docID=1652027&amp;x=0&amp;y=1&amp;pageNum=1&amp;scale=3782&amp;ro=0&amp;time=1607098159588&amp;showAnn=1&amp;pageID=7493796&amp;search



REL  Nginx  路径  errors  相对  404