1

Please help, I am trying to set up a dynamic reverse proxy so that I won't require to make direct changes to nginx. What I want to achieve is to create a default locations say:

location ~ ^/staging/v1/(.*) {
      resolver 4.2.2.2
      proxy_pass https://$1$is_args$args;
}

so assuming that the Nginx reverse proxy server URL is example.com, and I visit example.com/staging/v1/test.com, the reverse proxy server will proxy my request to test.com with all the paths and arguments.

However, this seems to work with a lot of errors. First is that it loads blank page. but if I add the domain on the nginx location root like:

  location / {
           proxy_pass  test.com;
   }

this will make requests like example.com/staging/v1/test.com/app/login.png to start working.

Please I need help

1 Answers1

1

You need to send valid Host HTTP header with your request. You'll need to split the rest of your URI in two parts and use the first one to set it using proxy_set_header directive. Check this answer to find out more.

Since the hostname part obviously cannot include slash, you can use the following regex pattern instead:

location ~ ^/staging/v1/([^/]+)(?:/(.*))? {
    resolver 4.2.2.2;
    proxy_set_header Host $1;
    proxy_pass https://$1/$2$is_args$args;
}
Ivan Shatsky
  • 3,545