How to pass an html site from another server via nginx reverse proxy?

cn flag

I am trying to load an Angular app from another internal service when calling localhost:81/internal?pageId=10. But all that happens is that nginx redirect me in the browser to localhost:80.

I have this configuration:

  location ~ /internal(?<section>.+) {
    proxy_set_header Host $host;

As I've read in multiple other questions, that should work, but for some reason nginx redirects me to port 80.

Request headers:

GET /internal/?pageId=10
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
Connection: keep-alive
Host: localhost:81
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36

Response headers:

HTTP/1.1 302 Found
Server: nginx/1.21.6
Date: Thu, 19 May 2022 09:15:34 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Location: http://localhost/public/internal?pageId=10
X-Powered-By: Jetty(9.4.40.v20210413)

Is this even possible? And if so, how do I fix this?

Ivan Shatsky avatar
gr flag
Maybe your internal service does not expect the `Host` header being set to `$host` (which is equal to `localhost:81` in your case) and you should use `proxy_set_header Host localhost;` instead? Additionally, the way you are using your query string is not being passed to the upstream (unless you add the `$is_args$args` suffix to your `proxy_pass` directive).
Apollo avatar
cn flag
The internal server does not check the headers. I just copied the lines that I found on StackExchange to make sure it is not an issue there. Can you give an example how to add $is_args$args? Do I replace the $section?
Ivan Shatsky avatar
gr flag
No, just add it to the end of URI passed to the internal server: `proxy_pass$section$is_args$args;`. You can check what happens requesting the required URI directly from the internal server: `curl -v http://localhost/public/internal/?pageId=10`, `curl -v -H 'Host: localhost:81' http://localhost/public/internal/?pageId=10`. An example from your question (`localhost:81/internal?pageId=10`) does not match the headers you provided (`localhost:81/internal/?pageId=10`, note the slash) unless some internal rewrite happens somewhere in your nginx config.

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.