0

I have the following server settings which work for one URL but seem to fail on another 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;
}   

When I go to the following URL:

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

I get the following error from 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"

I did some research but I can't seem to figure it out. Any advice?

Thanks

Update

Here is what seems to be the problem the ones that valid are the ones with a valid URL. When it fails it's because the URL is invalid.

  1. Using echo $saved_redirect_location;

    Success

    URL:

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

    Return:

    https://10.4.1.81/Forms/RequestToSpeak

    Fails:

    URL:

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

    Returns:

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

  2. Using echo http://10.4.1.81$saved_redirect_location;

    Fails

    URL:

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

    Return:

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

    Success

    URL:

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

    Return:

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

using $saved_redirect_location to proxy_pass is there a way to check if the domain exists and if not to add it?

Update

My solution was to do the following:

   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;
    }       
}
2
  • Maybe your new location returned after redirect as Location: /bos/CookieCheck.aspx?redirect=%2fbos%2fDocView.aspx%3fdbid%3d0%26id%3d1652027%26page%3d1 without the scheme or domain name parts? Then you need to use something like proxy_pass http://10.4.1.81$saved_redirect_location; instead of proxy_pass $saved_redirect_location; Commented Dec 4, 2020 at 2:31
  • It is perfectly valid for the Location: to include a partial URL that begins with the path component. So, yes, you do need to deal with this. Also, answers should be posted below as an answer, so that people don't think it is part of the problem. Commented Dec 4, 2020 at 10:43

1 Answer 1

0

This is the solution that ended up working for me. Thanks, Ivan for the tip.

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.