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!

3 Answers 3

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;
}  
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.

4
  • 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, 2016 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, 2016 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 5, 2016 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, 2016 at 7:22
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .