Welcome! Log In Create A New Profile

Advanced

NginX RewriteMap : How to ?

Posted by DenisTRUFFAUT 
NginX RewriteMap : How to ?
December 31, 2011 09:57AM
Hi,

I've seen that a map keyword exists in NginX, but I'm wondering how to use it to realize multiple calls, with matched expressions, on the same map, in order to emulate the Apache RewriteMap feature.


According to the following Apache files :


vhosts
--------------------------------------------------------------------------------------------------------------
<VirtualHost mywebsite.com:8080>
DocumentRoot "..."
ServerName mywebsite.com
ServerAlias www.mywebsite.com
RewriteMap search-woman "txt:the/path/to/search-woman.conf"
<Directory "/var/www/mywebsite/">
...
Include "the/path/to/mywebsite.com.conf"
...
</Directory>
</VirtualHost>



search-woman.conf
--------------------------------------------------------------------------------------------------------------
short-hair &hairSizes[3]=1
medium-hair &hairSizes[4]=1
long-hair &hairSizes[5]=1
white-hair &hairColors[1]=1
blonde &hairColors[2]=1
brunette &hairColors[3]=1
grey-hair &hairColors[4]=1
black-hair &hairColors[5]=1
ginger &hairColors[6]=1
amber-eyes &eyeColors[1]=1
blue-eyes &eyeColors[2]=1
grey-eyes &eyeColors[3]=1
brown-eyes &eyeColors[5]=1
hazel-eyes &eyeColors[6]=1
red-eyes &eyeColors[7]=1
green-eyes &eyeColors[8]=1



mywebsite.conf
--------------------------------------------------------------------------------------------------------------
RewriteEngine on
...
RewriteRule ^/woman/([^/]+)/$ /index.php?page=search&sex[2]=1${search-woman:$1} [PT]
RewriteRule ^/woman/([^/]+)/([^/]+)/$ /index.php?page=search&sex[2]=1${search-woman:$1}${search-woman:$2} [PT]
RewriteRule ^/woman/([^/]+)/([^/]+)/([^/]+)/$ /index.php?page=search&sex[2]=1${search-woman:$1}${search-woman:$2}${search-woman:$3} [PT]




Urls of my OR search will look like :
http://www.mywebsite.com/woman/blonde/long-hair/blue-eyes/
http://www.mywebsite.com/woman/blonde/brunette/ginger/


The RewriteMap feature is very efficient in the way that if you have 100 rules, you are not forced to hand-write or generate the cartesian product (100x100 = 10 000 rules), which would overkill the server. Only the 100 rules are needed.

RewriteMap a very important feature to me.
The last to implement in order to evacuate definitively the Apache server.

BTW, I didn't find in the documentation an example on how to do several calls, with matched expressions, on the same map :O

Denis TRUFFAUT
Re: NginX RewriteMap : How to ?
January 08, 2012 12:55PM
Update :

The main problem is, currently, the map module don't perform uri transformations as expected, when called several times on the same uri :

When I call the url :

http://www.dtruffaut.skeetmeet.com/tryst/woman/blonde/long-hair/



With the following map and location :

map $uri $search_woman {
~short-hair &hairSizes[3]=1;
~medium-hair &hairSizes[4]=1;
~long-hair &hairSizes[5]=1;
~white-hair &hairColors[1]=1;
~blonde &hairColors[2]=1;
~brunette &hairColors[3]=1;
~grey-hair &hairColors[4]=1;
~black-hair &hairColors[5]=1;
~ginger &hairColors[6]=1;
~amber-eyes &eyeColors[1]=1;
~blue-eyes &eyeColors[2]=1;
~grey-eyes &eyeColors[3]=1;
~brown-eyes &eyeColors[5]=1;
~hazel-eyes &eyeColors[6]=1;
~red-eyes &eyeColors[7]=1;
~green-eyes &eyeColors[8]=1;
}

location ~* ^/woman/[^/]+/[^/]+/$ {
rewrite ^/woman/[^/]+/[^/]+/$ /index.php?page=search&sex[2]=1$search_woman$search_woman last;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
break;
}



The result is :

www.mywebsite.com/index.php?page=search&sex[2]=1&hairSizes[5]=1&hairSizes[5]=1



It doesn't transform on the already-transformed $uri, but only on the original $uri.
As long-hair is before blonde, I get long-hair twice... rather than blonde and long-hair.

The expected behavior is a step-by-step replacement.

I do not know how to achieve this... -_-'



Edited 2 time(s). Last edit at 01/08/2012 01:02PM by DenisTRUFFAUT.
Re: NginX RewriteMap : How to ?
January 08, 2012 01:01PM
A quick fix could be the introduction of a new variable, aiming the $uri which is currently replaced.

I suggest something like $rewrited_uri, $tmp_uri, ... etc.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 249
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