简体   繁体   中英

Nginx reverse proxy to two services

I'm running nodeJS script..

  • on localhost:9001
  • it is running behind nginx reverse proxy
  • it accept request in the form of /v{{ version }}/{{ lang }}/...

So for example:

  • domain.com/api/v1/en/news
  • domain.com/api/v2/fr/news
  • domain.com/api/v3/en/news

Until now I had this is nginx

  location /api/ {
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   Host      $http_host;
    proxy_pass         http://localhost:9001/;
  }

and everything work.

My goal now is to run additional (identical) script on localhost:9002 which will accept v4 requests. And v3 , v2 and v1 will be still 'processed' by localhost:9001

So I want that request domain.com/api/v4/en/news is routed to localhost:9002

I put this above current rule like this

location ~* /api/v4/(.*)$ {
  proxy_set_header   X-Real-IP $remote_addr;
  proxy_set_header   Host      $http_host;
  proxy_pass         http://localhost:9002/v4/$1;
}

location /api/ {
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   Host      $http_host;
    proxy_pass         http://localhost:9001/;
  }

Request /v3/.. is routed to localhost:9001 (as expected) but /v4/.. returns 502 .

Any pointers?

location /api/v4/ {
  proxy_set_header   X-Real-IP $remote_addr;
  proxy_set_header   Host      $http_host;
  proxy_pass         http://localhost:9002/v4/;
}

location /api/ {
  proxy_set_header   X-Real-IP $remote_addr;
  proxy_set_header   Host      $http_host;
  proxy_pass         http://localhost:9001/;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM