Trouble migrating Apache rewrites to nginx
October 20, 2012 11:26AM
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
Re: Trouble migrating Apache rewrites to nginx
October 20, 2012 11:50AM
CentOS 6.3, nginx 1.2.4, php+fpm 5.4.7. Nginx was installed from the nginx repository, and PHP was hand compiled.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 174
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready