Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have an nginx map inside a configuration that looks like this:

map $uri $route {
  /route_a /path/to/a.html;
  /route_b /path/to/b.html;
}

server {    
  listen 8080;
  server_name example.dev;
}

I tried rewrite inside the server block:

rewrite  ^ $route break;

But got a 404 when trying to access /route_a.

I tried location and alias inside the server block:

location ~ ^ {
  alias $route;
}

Which worked, but served my content as application/octet-stream.

What's the correct way to do this?

My constraint is that the URI paths-to-files must be defined inside the map block.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You should check your error log to see what actually happens. This configuration works for me:

map $uri $route {
    /route_a /path/to/a.html;
    /route_b /path/to/b.html;
}

server {
    root /;
    listen 8080;
    server_name localhost;

    location / {
        rewrite ^ $route break;
    }
}

Of course, /path/to/[ab].html must exists.

Maybe you just forget to set right path using "root" directive?

share|improve this answer
1  
Thanks. I didn't realize the root directive was required. –  Aupajo Jan 10 '13 at 21:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.