0

I hope you can help me with a little but tricky nginx problem:

I want to configure a dynamic location-block like this:

location /test1/* {  
    proxy_pass destination.com/api/*;  
    proxy_buffering off;  
}  

If the URL is .../test1/folder1 the request should be passed to destination.com/api/folder1

I've tried it already with the variable $request_uri proxy_pass destination.com/api/$request_uri; - it didn't work for me.

EDIT: The site "/test1/" itself should NOT be redirected - only the part after "/test1/". How is that possible?

UPDATE 11.02.2016: Still no solution :(

Thanks for your help/answers!

varlog
  • 23
  • 2
  • 6

3 Answers3

0

You could rewrite the URI before pass it to the server like this:

location /test1/ {
    proxy_set_header    Host destination.com;
    proxy_redirect http://destination.com/api/ http://$http_host/test1/;
    rewrite ^/test1/(.*)$ /api/$1 break;
    proxy_pass destination.com;
} 

Hope this help.

Update: Add proxy_set_header to change Host to correct domain name. Also add proxy_redirect to correct the Location in response header if needed.

Thang Pham
  • 71
  • 3
  • I'm sorry but this doesn't help. The "destination" is a remote system. The path does not exist on the local nginx system. – varlog Jan 28 '16 at 14:32
  • Which path did you mean here? For a remote system with different domain, let's add some config as I edited. – Thang Pham Jan 28 '16 at 17:13
  • This looks good and seems to work, BUT "/test1/" itself should NOT be redirected - only the part after "/test1/". How is that possible? – varlog Feb 05 '16 at 13:14
  • Sorry for late reply, but hope this help. In your case, let's change 4th line to: `rewrite ^/test1/(.+)$ /api/$1 break;` – Thang Pham Feb 16 '16 at 07:22
0

You don't need * in location. And you must have protocol in proxy_pass. This should work:

location /test1/ {  
    proxy_pass http://destination.com/api/;
    proxy_buffering off;
}  
Alexey Ten
  • 8,435
  • 1
  • 34
  • 36
0

I've found a simple solution. 3 location blocks:

location = /test1/ --> stay local
proxy_pass local-upstream$request_uri;
rewrite ^(.*) localdomain;

location = /test1 --> stay local
proxy_pass local-upstream$request_uri;
rewrite ^(.*) localdomain;

location /test1/ {
proxy_pass destination.com/api/;
}

The upstream is defined in the main config file:

upstream local-upstream {
server ip-address;
}

Thats all.

varlog
  • 23
  • 2
  • 6