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

当我访问以下 URL 时:

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?重定向=%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

这是最终对我有用的解决方案。谢谢 Ivan 的提示。

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

相关内容