nginx:如何从给定列表中批量进行永久重定向? [英] nginx: How to mass permanent redirect from a given list?

查看:173
本文介绍了nginx:如何从给定列表中批量进行永久重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约400个将在新版本中更改的网址,由于某些原因,我无法在新网站中重复相同类型的网址结构.

I have about 400 url that will change in the new version and for some reasons I can't repeat the same type of url structure in the new website.

我的问题是,我可以给nginx一个网址列表(是的,我知道400个网址),然后简单地告诉他每个人都将转到另一个网址?

My question is, can I give a url list to nginx (yeah I know the 400 ones), and tell him simply that each one of them are going to another url?

就像我说的url结构会有所不同,所以我不能使用任何类型的模式.

Like I said the url structure will be different so I can't use any type of pattern.

提前谢谢.

推荐答案

如果条目列表非常长,最好将它们保留在nginx配置文件之外:

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 示例:

/my-old-url /my-new-url;
/old.html /new.html;

确保每行以;"结尾字符!

Be sure to end each line with a ";" char!

此外,如果您需要将所有URL重定向到另一台主机,则可以使用:

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;

这篇关于nginx:如何从给定列表中批量进行永久重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆