Nginx: mass permanent redirects

When I say “mass”, I mean reasonable amount of redirects, like a couple of thousands.

I had to migrate a blog to a new URL structure. This migration led to create about 600+ unique redirects without any shared patterns.
To solve this, I used a nginx configuration file containing all the redirects and I included it in my vhost configuration like this :

  • /etc/nginx/site-enabled/v3.conf
server {
    listen 443 ssl;
    [...]
    include /etc/nginx/conf.d/v3.redirects
    [...]
    location / {
    [...]
    }
}

 

  • /etc/nginx/conf.d/v3.redirects
[...]
rewrite ^/le-stress(.*) /conseils/relaxation/le-stress$1 permanent;
[...]

The v3.redirects file has been generated with a command line script.

It works like a charm for this amount of redirects. If you need to manage a lot more of them, take a look at lua and redis.