# NginxHttpMapModule

**URL:** <https://www.ruby-forum.com/t/nginxhttpmapmodule/177379>\
**Category:** NGINX\
**Created:** [October 15, 2009, 11:41pm UTC](https://www.ruby-forum.com/t/nginxhttpmapmodule/177379 "2009-10-15T23:41:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Goldcap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ba9def/32.png) [@Goldcap](https://www.ruby-forum.com/u/Goldcap)\
**Post date:** [October 15, 2009, 11:41pm UTC](https://www.ruby-forum.com/t/nginxhttpmapmodule/177379/1 "2009-10-15T23:41:28Z")

</div>

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/](http://www.domain.com/home/);

# /aa [http://aa.domain.com/](http://aa.domain.com/); /bb [http://bb.domain.com/](http://bb.domain.com/); /john [http://my.domain.com/users/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](http://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:

> **[NginxHttpMapModule](https://forum.nginx.org/read.php?2,14117,14117#msg-14117)**
>
> 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

---

<div class="post-metadata">

**Author:** ![Goldcap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ba9def/32.png) [@Goldcap](https://www.ruby-forum.com/u/Goldcap)\
**Post date:** [October 16, 2009, 5:06pm UTC](https://www.ruby-forum.com/t/nginxhttpmapmodule/177379/2 "2009-10-16T17:06:21Z")

</div>

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](http://forum.nginx.org);  
…

Posted at Nginx Forum:

> **[Re: NginxHttpMapModule](https://forum.nginx.org/read.php?2,14117,14321#msg-14321)**
>
> 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

---

<div class="post-metadata">

**Author:** ![Goldcap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ba9def/32.png) [@Goldcap](https://www.ruby-forum.com/u/Goldcap)\
**Post date:** [October 16, 2009, 5:12pm UTC](https://www.ruby-forum.com/t/nginxhttpmapmodule/177379/3 "2009-10-16T17:12:47Z")

</div>

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](http://forum.nginx.org);  
> …

No, it’s better to use this configuration:

map $uri $new {  
default “”;

/aa [http://aa.domain.com/](http://aa.domain.com/);  
/bb [http://bb.domain.com/](http://bb.domain.com/);  
/john [http://my.domain.com/users/john/](http://my.domain.com/users/john/);  
}

server {

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

...

```
