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.
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
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:
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;
}
}
Location: /bos/CookieCheck.aspx?redirect=%2fbos%2fDocView.aspx%3fdbid%3d0%26id%3d1652027%26page%3d1without the scheme or domain name parts? Then you need to use something likeproxy_pass http://10.4.1.81$saved_redirect_location;instead ofproxy_pass $saved_redirect_location;