1

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_pass http://192.168.1.100:8080/public/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?

Apollo
  • 113
  • 3
  • 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). – Ivan Shatsky May 19 '22 at 09:42
  • 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? – Apollo May 20 '22 at 05:30
  • No, just add it to the end of URI passed to the internal server: `proxy_pass http://192.168.1.100:8080/public/internal$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. – Ivan Shatsky May 20 '22 at 07:38

0 Answers0