If you have a very long list of entries, could be a good idea to keep them outside of the nginx configuration file:
map_hash_bucket_size 256; # see http://nginx.org/en/docs/hash.html
map $request_uri $new_uri {
include /etc/nginx/oldnew.map; #or any file readable by nginx
}
server {
listen 80;
server_name your_server_name;
if ($new_uri) {
return 301 $new_uri;
}
...
}
/etc/nginx/oldnew.map sample:
/my-old-url /my-new-url;
/old.html /new.html;
Be sure to end each line with a ";" char!
Moreover, if you need to redirect all URLs to another host, you could use:
return 301 http://example.org$new_uri;
Or, if you also need to redirect to another port:
return 301 http://example.org:8080$new_uri;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…