Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect all the requests to api server with nginx

Tags:

nginx

I'm using Nginx as a reverse proxy to redirect api request to one my server. Unfortunately it is not working properly

what I'm trying to achieve is to proxy all requests like /api/v1/* to http://my-api-server/api/v1/*

here is the rule I have written

location /api/v1/ {
   proxy_pass http://my-api-server/api/v1/
}

but it doesn't work. any idea?

like image 362
Mazzy Avatar asked Apr 10 '15 09:04

Mazzy


2 Answers

Try

location /api/v1/ {
   proxy_pass http://my-api-server
}

In proxy_pass directive, if you specify the URI which is /api/v1/ in your case, all matched URIs will be replaced as the exactly specified one /api/v1/ but not /api/v1/*.

like image 149
Landys Avatar answered Oct 26 '22 00:10

Landys


You need to add the proxy_redirect attribute:

location /api/v1 {
    proxy_redirect  http://my-api-server/  /api/v1;
    proxy_pass http://my-api-server;
}
like image 31
gaolong zhao Avatar answered Oct 26 '22 00:10

gaolong zhao