Bulk redirection with Nginx

python

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> <new_url>; 

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

The regex version looks likes this:

~^/showcase/(.*)?$   /projects/; 

The plan simple location redirect will look like:

/contact-success/ /contact; 

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

/contact-success/ /contact; ~^/showcase/(.*)?$   /projects/; 

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:
map $  uri $  redirected_url {     default "none";     include /etc/nginx/redirects-map.conf; } 

Then in the server block, write the following rule:

# pre-defined redirects if ($  redirected_url != "none") {     rewrite ^ $  redirected_url permanent; } 

Now your virtualhost conf file should look like:

map $  uri $  redirected_url {     default "none";     include /etc/nginx/redirects-map.conf; }  server {     listen        443 ssl http2;     server_name   example.org;      # pre-defined redirects     if ($  redirected_url != "none") {         rewrite ^ $  redirected_url permanent;     }      # rest of the rules  } 

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;

About Open Source Projects

My name is Berkant, 21 years old programmer. Use Opensourceprojects.org as a new resource. Improve yourself by learning new code sequences and modifying them. Do not forget that nothing is impossible, everything is in your fingers!

View all posts by Open Source Projects →

Leave a Reply

Your email address will not be published. Required fields are marked *