Search code examples
nginxproxynginx-reverse-proxynginx-confighttp-proxy

Nginx proxy_pass part of url and params


I need to proxy request From my server to another Two options should be realized

First:

From https://my-site.com/subjects/custom_id to https://another-site.com/subjects/custom_id

Example:

https://my-site.com/subjects/1 to https://another-site.com/subjects/1

https://my-site.com/subjects/2 to https://another-site.com/subjects/2

Nginx config (works fine):

  location ~ ^/subjects/?(.*) {
     resolver 8.8.8.8;
     proxy_pass https://another-site.com/subjects/$1$is_args$args;
   }

Second:

From https://my-site.com/subjects?code=custom_code to https://another-site.com/subjects?code=custom_code

Example:

https://my-site.com/subjects?code=1 to https://another-site.com/subjects?code=1

https://my-site.com/subjects?code=2 to https://another-site.com/subjects?code=2

How to write Nginx config for this case ?


Solution

  • Fixed:

    location ^~ /subjects {
         resolver 8.8.8.8;
         proxy_pass https://another-site.com/subjects;
    }