Score:0

nginx to not modify query only redirect for reverse proxy

cn flag

I have to put a nginx reverse proxy in front of a web service for TLS termination which does 302 redirection only with query part of the URI:

GET /path/to/document?query=1
Host: 127.0.0.1

Returns 302 with 
Location: ?query=2

nginx always builds the full URI but omitting the document, therefore the redirects don't work

GET /path/to/document?query=1
Host: example.com

Returns 302 with 
Location: https://example.com/path/to/?query=2

I tried with proxy_redirect off;, but that doesn't change anything. What am I doing wrong?

Here's the config I currently use:

location / {
    client_max_body_size 50M;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $http_host;
    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 $scheme;
    proxy_set_header X-Forwarded-Ssl on;
    proxy_set_header X-Frame-Options SAMEORIGIN;
    proxy_pass http://ip.of.the.host/;
    proxy_redirect off;
}

I could set proxy_redirect ~\?(.*)$ https://example.com/path/to/document?$1; but I'm sure I'll break something else.

How can I make nginx to just reply with the same Location path the upstream server does?

us flag
Have you made sure that it is actually nginx that is modifying the `Location` header? Have you checked the response from the upstream server that it really has this odd `Location` header when request comes from nginx?
Score:0
cn flag

Thanks to @Tero Kilkanen I did a tcpdump on the nginx server and found out the upstream server was actually providing the wrong Location header.

After going through every header nginx sent to the upstream server and I still didn't get the same answer via curl I finally found the culprit: HTTP version!

nginx uses http/1.0 per default, and curl uses http/1.1, that's why in my previous testing this never came up.

Setting proxy_http_version 1.1; fixes the problem!

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.