2

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.

1

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?

1
  • 1
    Thanks. I didn't realize the root directive was required.
    – Aupajo
    Jan 10 '13 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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