A
A
Alexey Skvortsov2022-03-09 22:04:30
Nginx
Alexey Skvortsov, 2022-03-09 22:04:30

How to forward requests for api url to another upstream?

There is the following site setup on nginx, it is necessary to redirect all requests from example.com/api/ to the api_backend servers, and all other requests to site_backend. I can't figure out where I made a mistake.

location ~ /api {
  rewrite ^/(.*)$ /index.php?/$1 last;

  location ~ \.php$ {
    try_files      $fastcgi_script_name =404;
    fastcgi_pass api_backend;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $request_filename;
  }
}

location / {
  rewrite ^/(.*)$ /index.php?/$1 last;

  location ~ \.php$ {
    try_files      $fastcgi_script_name =404;
    fastcgi_pass site_backend;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $request_filename;
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Karabanov, 2022-03-09
@karabanov

upstream api_backend {
    server blablabla;
}

upstream site_backend {
    server lalala;
}

server {
    listen 80;

    server_name example.com;

    location /api/ {
      proxy_pass http://api_backend/;
    }

    location / {
      proxy_pass http://site_backend/;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question