Bulk redirection with Nginx

In this post, I am going to talk about how you can setup a redirects on a server for many individual paths. It uses the hash map

Creating files

Create a file called redirects-map.conf, each line of the file will represents a redirect rule. The format of line is:

old_location can be specified in both regex or plan simple path.

The regex version looks likes this:

The plan simple location redirect will look like:

So your redirects-map.conf file should look like:

Setting up nginx

  1. Upload the redirects-map.conf to your server at /etc/nginx
  2. Add the following at the top of your nginx virtualhost file, which imports the file and creates a hash map:

Then in the server block, write the following rule:

Now your virtualhost conf file should look like:

Note: Nginx has a limit on how big the redirects-map.conf can be, which is controlled via map_hash_bucket_size variable. If you get the following error, [emerg]: could not build the map_hash, you should increase the map_hash_bucket_size to account for the filesize. Say your redirects-map.conf is 30Kb, you set the map_hash_bucket_size variable in the http block of nginx.conf to 30720;