大网数据广告活动
服务器运维问答

Nginx handling relative paths causes 404 errors

· 52CE 我爱测

I have a problem that when I reverse proxy a page the files will relative paths are not found. Example working: https://lfportal.mohavecounty.us/bos/search.aspx?dbid=0&searchcommand=%28%7B%5BBOS%20Agenda%20Packets%5D%3A%5BMeeting…

问题原文

原帖链接 · adviner · CC BY-SA 4.0

I have a problem that when I reverse proxy a page the files will relative paths are not found.

Example working:

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

Which redirects to the search result:

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

On the file Page1.aspx there are paths using the relative path as shown in the example below

<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=">

Now with my redirect

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

The page loads but doesn't continue because it fails. It give allot of 404 and looking at the address of one the image above in Chrome developer and the 404 error shows the address:

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=

So it seems the root folder bos is missing. https://lfdocs.mohave.gov/Helper/TileData.aspx?

Should be URL: https://lfportal.mohavecounty.us/bos/Helper/TileData.aspx?

So it seems my proxy pass is not handling the relative attribute correctly. Below is my sample Nginx site conf. The problem is occurring in the second if the condition

if ($saved_redirect_location !~* "10.4.1.81") { .. }

My conf file:

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;
        }       
    }   
}

In Chrome developer I look at the custom header x-debug-message and has the value:

x-debug-message: http://10.4.1.81/bos/0/doc/1652027/Page1.aspx

I have tried removing the trailing slash under the location section from an article I read but it didn't work.

Any ideas on why the relative paths are not loading?

已采纳解答

原帖链接 · Tero Kilkanen · CC BY-SA 4.0

The relative paths are added by your backend application. nginx does not touch the paths in any way.

If your backend application uses relative paths, then your frontend URL must be at same depth as backend URL.

In your first example:

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

The URL is on fourth subdir level. This means browser can resolve ../../../ to /bos.

In your second example:

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

The URL is on second subdir level. Now, browser tries to resolve ../../../ URL, and it ends up being / URL.

My recommendation is that you replace the relative URLs with site-root relative URLs: /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

Another issue with your URLs is that the ampersand characters are URL encoded, which might lead to undesired end result.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-12-04)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。