Nginx 傳送到用戶端時 URL 路徑前綴無效

Nginx 傳送到用戶端時 URL 路徑前綴無效

我有以下伺服器設置,適用於一個 URL,但似乎在另一個 URL 上失敗。

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';
    proxy_pass $saved_redirect_location;
}   

當我造訪以下網址時:

https://lfdocs.mohave.gov/bos/0/doc/1652027/Page1.aspx

我從 Nginx 收到以下錯誤

2020/12/03 19:10:40 [error] 31251#31251: *1 invalid URL prefix in "/bos/CookieCheck.aspx?redirect=%2fbos%2fDocView.aspx%3fdbid%3d0%26id%3d1652027%26page%3d1" while sending to client, client: 10.10.82.151, server: lfdocs.mohave.gov, request: "GET /bos/0/doc/1652027/Page1.aspx HTTP/2.0", host: "lfdocs.mohave.gov"

我做了一些研究,但我似乎無法弄清楚。有什麼建議嗎?

謝謝

更新

這似乎是問題所在,有效的內容是具有有效 URL 的內容。如果失敗,則是因為 URL 無效。

  1. 使用 echo $saved_redirect_location;

    成功

    網址:

    https://lfdocs.mohave.gov/Forms/RequestToSpeak

    返回:

    https://10.4.1.81/Forms/RequestToSpeak

    失敗

    網址:

    https://lfdocs.mohave.gov/bos/0/doc/1652027/Page1.aspx

    返回:

    /bos/CookieCheck.aspx?redirect=%2fbos%2fDocView.aspx%3fdbid%3d0%26id%3d1652027%26page%3d1

  2. 使用 echo http://10.4.1.81$saved_redirect_location;

    失敗

    網址:

    https://lfdocs.mohave.gov/Forms/RequestToSpeak

    返回:

    http://10.4.1.81https://10.4.1.81/Forms/RequestToSpeak

    成功

    網址:

    https://lfdocs.mohave.gov/bos/0/doc/1652027/Page1.aspx

    返回:

    http://10.4.1.81/bos/CookieCheck.aspx?redirect=%2fbos%2fDocView.aspx%3fdbid%3d0%26id%3d1652027%26page%3d1

使用 $saved_redirect_location 到 proxy_pass 有沒有辦法檢查域是否存在以及如果不添加它?

更新

我的解決方案是執行以下操作:

   location @handle_redirects {
    set $saved_redirect_location '$upstream_http_location';

    # If IP exists just proxy pass
    if ($saved_redirect_location ~* "10.4.1.81") {
        proxy_pass $saved_redirect_location;
    }

    # If IP doesnt exist append it first
    if ($saved_redirect_location !~* "10.4.1.81") {
        proxy_pass http://10.4.1.81$saved_redirect_location;
    }       
}

答案1

這是最終對我有用的解決方案。謝謝伊凡的提示。

location @handle_redirects {
  set $saved_redirect_location '$upstream_http_location';

  # If IP exists just proxy pass
  if ($saved_redirect_location ~* "10.4.1.81") {
    proxy_pass $saved_redirect_location;
  }

  # If IP doesnt exist append it first
  if ($saved_redirect_location !~* "10.4.1.81") {
    proxy_pass http://10.4.1.81$saved_redirect_location;
  }       
}

相關內容