Hi,
I'm trying to migrate our PHP app to nginx + fpm, and I'm mostly there. We have a set of rewrite rules that will rewrite a URL from www.example.com/clientA into %{DOCUMENT_ROOT}/clients/clientA. The URL does not change from www.example.com/clientA in the client's browser.
Here are the current rewrite rules. They split the URL to get the client name and the path requested into 2 variables. TESTDIR and TESTFILE. We then do several tests to see where things exist so we can rewrite the URL correctly. The first problem I'm having is even just getting the 2 variables set. I'm thinking I might have to use perl or lua to do it.
RewriteEngine On
RewriteRule ^/([^/]+)/(.*)$ - [NS,E=TESTDIR:$1,E=TESTFILE:$2]
RewriteCond %{DOCUMENT_ROOT}/clients/%{ENV:TESTDIR} -d
RewriteRule ^/([^/^\.]+)$ %{DOCUMENT_ROOT}/ [L]
RewriteCond %{DOCUMENT_ROOT}/clients/%{ENV:TESTDIR}/%{ENV:TESTFILE} -f
RewriteRule ^/([^/]+)/(.*)$ %{DOCUMENT_ROOT}/clients/$1/$2 [L]
RewriteCond %{DOCUMENT_ROOT}/clients/%{ENV:TESTDIR} -d
RewriteCond %{DOCUMENT_ROOT}/%{ENV:TESTFILE} -f
RewriteRule ^/([^/]+)/(.*)$ %{DOCUMENT_ROOT}/$2 [L]
RewriteCond %{DOCUMENT_ROOT}/clients/%{ENV:TESTDIR} -d
RewriteCond %{DOCUMENT_ROOT}/%{ENV:TESTFILE} -d
RewriteRule ^/([^/]+)/(.*)$ %{DOCUMENT_ROOT}/$2/ [L]
Once I've gotten the variables set, I'm thinking if statements are how I'd do the rewrites.
if -d $document_root/clients/$testdir {
set $found = "D";
}
if -f $document_root/clients/$testdir/$testfile {
set $found = "${found}F";
}
if $found = "D" {
rewrite ^/([^/]+)/(.*)$ $document_root/ last;
}
if $found = "DF" {
rewrite ^/([^/]+)/(.*)$ $document_root/clients/$1/$2 last;
}
if -f $document_root/$testfile {
$found = "${found}f";
}
if $found = "Df" {
rewrite ^/([^/]+)/(.*)$ $document_root/$2 last;
}
if -d $document_root/$testfile {
$found = "${found}d";
}
if $found = "Dd" {
rewrite ^/([^/]+)/(.*)$ $document_root/$2/ last;
}
Am I even close?
Thanks,
Damon