2

I would like to do the following in my NGINX configuration: I want to proxy pass the path and query parameters but not include the first path parameter (path1).

Request URL

https://my-domain.com/path1/path2?query1=some-query

I want this to proxy_pass to

http://localhost:8000/path2?query1=some-query

I have tried with the following location block

location /path1/path2/ {
  proxy_pass http://localhost:8000$request_uri;
  proxy_http_version 1.1;
}

However, this does not go to the correct endpoint because I think it includes the path1 parameter too. I have several different path2 routes and so I want to be able to catch and redirect them all easily - without having to manually define each one like this:

location /path1/path2a/ {
  proxy_pass http://localhost:8000/path2a?query1=some-query;
  proxy_http_version 1.1;
}

location /path1/path2b/ {
  proxy_pass http://localhost:8000/path2b?query1=some-query;
  proxy_http_version 1.1;
}

location /path1/path2c/ {
  proxy_pass http://localhost:8000/path2c?query1=some-query;
  proxy_http_version 1.1;
}

ADDITIONAL EDIT:

I also cannot simply do

location /path1/ {
  proxy_pass http://localhost:8000/;
  proxy_http_version 1.1;
}

because I already have the location /path1/ block that gets upgraded to a WebSocket connection at a different endpoint:

location /path1/ {
  proxy_pass http://localhost:9000/;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "Upgrade";
}

I have searched a lot online but cannot find a configuration that works for my specific scenario.

KayBay
  • 719
  • 1
  • 7
  • 10

1 Answers1

5

This is really simple:

location /path1/ {
    proxy_pass http://localhost:8000/;
    proxy_http_version 1.1;
}

Read this Q/A for details.

Update

This solution isn't usable after OP clarifies his question.

If all of the additional paths share common suffix path2:

location /path1/path2 {
    rewrite ^/path1(.*) $1 break; # remove '/path1' URI prefix
    proxy_pass http://localhost:8000;
    proxy_http_version 1.1;
}

If they aren't, replace location /path1/path2 with location ~ ^/path1/(?:path2a|path2b|path2c).

If you need to pass to the backend query arguments that are different from those came with the request, use

set $args query1=some-query;

within the location block.

Ivan Shatsky
  • 13,267
  • 2
  • 21
  • 37
  • Hi Ivan, thanks for your comment. I forgot to specify why I also cannot do that. I have just modified the question with an additional note. – KayBay Jun 10 '20 at 13:27