Mass redirect using Nginx map file
Sometimes after upgrading or migrating an application you still need to keep old links working. A simple way to handle this in Nginx is to use a map file and redirect the requests to their new locations. If you are using HAProxy, you can find a similar example here: Haproxy.
This approach is useful when you have many URLs to redirect and don’t want to clutter the main configuration with dozens (or hundreds) of rewrite rules.
This approach is useful when you have many URLs to redirect and don’t want to clutter the main configuration with dozens (or hundreds) of rewrite rules.
Virtual Host Configuration
map_hash_bucket_size 256;
map $request_uri $new_uri {
include /etc/nginx/conf.d/redirects.map;
}
server {
listen 80;
...
if ($new_uri) {
return 302 $new_uri;
}
...
location / {
...
}
Redirect MAP
#cat /etc/nginx/conf.d/redirects.map
/old1/link1 https://domain.com/1234;
/old2/link2 https://domain.com/12345;
...
Human Logic, AI Syntax...
Note on Content: I'm a Systems Engineer, not a native English writer. To ensure my technical ideas are clear and accessible, I use AI tools to polish the grammar and style. The workflow is simple: I provide the logic, the code, and the real-world experience. The AI handles the "English-to-Human" translation layer. If you find a bug, that's on me. If you find a perfectly placed comma, that's probably the AI.
Comments
Post a Comment