20

I am in the situation to give our marketing department the opportunity to maintain their redirects by their own. Until now, they passed the information to the IT department and we maintained it for them in nginx.conf.

Some of these guys are quite familiar with redirections in IIS or even in Apache but it is no option to give them direct access to the nginx configuration.

I see, that there is no nginx support for .htaccess files which I could give access to and I would also prefer not to grant write access to an conf-file that nginx includes. I expect, that our marketing will break our nginx setup within hours...

Is there a secure possibility without giving them access to the heart of our load balancer?

1 Answer 1

28

There is no built in way to properly isolate the rewrite configuration like that. There are three approaches you could take.

Map module include

The map module allows you to include mappings from a separate file. Nginx still has to be reloaded after the file is changed, and the mapping file must be syntactically correct, but it does limit what can be done.

nginx.conf:

map $uri $new {
    include /etc/nginx/marketing.map;
}

server {
    ...
    if ($new) {
        rewrite ^ $new redirect;
    }
    ...
}

marketing.map:

/about  /company/about-us;
~^/people/(?<person>.*)$    /company/people/$person;

Pre-process configuration

The first is to write a script which transform the redirects from some format which you define into nginx configuration. For example, given a list of space separated redirects:

/foo/(.*) /bar/$1

and a script:

#!/bin/sh
while read SOURCE DEST; do
    echo "rewrite $SOURCE $DEST permanent;"
done < redirects.txt > redirects.conf

to form the following configuration:

rewrite /foo/(.*) /bar/$1 permanent;

You'd then want to run nginx -t on the entire configuration to check that it's valid before reloading.

On-the-fly processing

The second option is to use ngx_lua, ngx_perl or ngx_js to implement reading and processing your redirect configuration in nginx itself. For example, the rewrite_by_lua directive allows you to execute Lua code to construct a rewrite. You need to be careful about preformance however since you'll be interpreting code for every request.

4
  • 1
    The map module does provided a slightly restricted way to configure redirects.
    – mgorven
    Oct 24, 2012 at 22:36
  • 1
    The map method looks very promising! So, I will start writing a simple website to maintain the map-file. Thank you!
    – Sascha
    Oct 26, 2012 at 6:08
  • 2
    Sadly, at this point (2014 fall) the ngx_js project was last touched over two years ago. ngx_lua is very active and well maintained, however.
    – Ville
    Sep 28, 2014 at 21:48
  • Now is available nginScript (alpha quality) that allows to use a subset of js.
    – lifeisfoo
    Nov 13, 2016 at 16:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .