NginxHttpMapModule

I’m unclear how to mimic the Apache rewrite map using the
NginxHttpMapModule.

I’ve included a map in my nginx.conf file, as so:

===================================
map $uri $new {
default http://www.domain.com/home/;

/aa http://aa.domain.com/;
/bb http://bb.domain.com/;
/john http://my.domain.com/users/john/;
}

Then have trouble using the map without excuding all other rules in my
server configuration. The following rewrites everything, obviously, but
couldn’t figure out how to get a conditional match wrapped around it or
from it:

===================================
server {
listen 80;
server_name some.domain.com;
rewrite ^ $new redirect;

root /var/www/web;
index index.php;

access_log /var/log/httpd/nginx_access_log main;
error_log /var/log/httpd/nginx_error_log;

charset utf-8;

location / {

# If the file exists as a static file serve it directly without
# running all the other rewite tests on it
if (-f $request_filename) {
  expires max;
  break;
}

if ($request_filename !~ "\.(js|htc|ico|gif|jpg|png|css)$") {
  rewrite ^(.*) /index.html last;
}

}

I’m obviously missing something, but how would one wrap this Map
conditionally? If no “map” rules trigger, continue with "locations’…

Is this possible?

Thanks in advance!

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,14117,14117#msg-14117

To follow up, it’s really simple. You don’t need the Map module, just a
text file with rewrite directives, and the include directive.

Doh! Not sure why I went the confusing route…

location / {

include /path/to/rewrite_rules_x.txt;

+++++++++++++++++++++++++++++++

cat /path/to/rewrite_rules_x.txt

rewrite /aaa /someotherpage;
rewrite /bbb http://forum.nginx.org;

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,14117,14321#msg-14321

On Fri, Oct 16, 2009 at 10:54:31AM -0400, Goldcap wrote:

cat /path/to/rewrite_rules_x.txt

rewrite /aaa /someotherpage;
rewrite /bbb http://forum.nginx.org;

No, it’s better to use this configuration:

map $uri $new {
default “”;

/aa http://aa.domain.com/;
/bb http://bb.domain.com/;
/john http://my.domain.com/users/john/;
}

server {

if ($new) {
    rewrite  ^  $new;
}

...

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs