Nginx – Redirecting Magento’s admin to separate backend

magentonginx

I need the Magento admin to be sent to a separate backend from the store. I have 2 issues:

  1. Don't know how to properly match both index.php/admin and /admin with a regex (index.php/admin works, as it is matching second location I have below);
  2. When I get Nginx to match the location (currently only /admin), I don't know how to properly forward the request so i get "File not found" from the backend as it is trying to open as a php file instead of sending to the main handler index.php.

This is what I currently have for the admin

location ~ ^/(admin|index.php/admin)/ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass server1:9000;
    #fastcgi_index index.php;
    include fastcgi_params;
}

This is the 2nd directive that is matching index.php/admin and the whole site

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass server2:9000;
    fastcgi_index index.php;
    include fastcgi_params;
}

Best Answer

Instead of using two separate location directives I placed an if clause inside a single location:

location ~ \.php$ {
  try_files $uri =404;
  fastcgi_split_path_info ^(.+\.php)(/.+)$;
  fastcgi_pass server2:9000;
  if ($request_uri ~ /admin/) {
    fastcgi_pass server1:9000;  
  }
  fastcgi_index index.php;
  include fastcgi_params;
}