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:
1 | <old_location> <new_url>; |
old_location
can be specified in both regex or plan simple path.
The regex version looks likes this:
1 | ~^/showcase/(.*)?$ /projects/; |
The plan simple location redirect will look like:
1 | /contact-success/ /contact; |
So your redirects-map.conf
file should look like:
1 | /contact-success/ /contact; ~^/showcase/(.*)?$ /projects/; |
Setting up nginx
- Upload the
redirects-map.conf
to your server at/etc/nginx
- Add the following at the top of your nginx virtualhost file, which imports the file and creates a hash map:
1 | map $ uri $ redirected_url { default "none"; include /etc/nginx/redirects-map.conf; } |
Then in the server
block, write the following rule:
1 | # pre-defined redirects if ($ redirected_url != "none") { rewrite ^ $ redirected_url permanent; } |
Now your virtualhost conf file should look like:
1 | 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
;