nginx和try_files:在回退之前尝试使用重写的命名位置

我正在尝试网站切换。 我们希望通过使用301redirect来保留我们的外部链接。 我在一个命名的位置有一个冗长的redirect列表,类似于:

location @redirects { rewrite ^/path/one.html$ http://www.ourdomain.tld/one-but-different permanent; rewrite ^/path/two.html$ http://www.ourdomain.tld/two-but-different permanent; rewrite ^/path/three.html$ http://www.ourdomain.tld/three-but-different permanent; rewrite ^/path/four.html$ http://www.ourdomain.tld/four-but-different permanent; } 

(请注意,即使看起来我的示例显示了一个模式,但没有模式存在,换句话说,它们是一对一的redirect。)

我有一个CMS Web应用程序,目前我正在使用下面的try_files语句(它一直在努力回落到index.php脚本):

 location / { try_files $uri $uri/ /index.php; } 

现在我试图使用try_files来“查看”redirect的位置,并在返回到index.php之前处理重写。 喜欢这个:

 location / { try_files @redirects $uri $uri/ /index.php; } 

但是,CMS在处理404时每次都会触发回退。 换句话说,如果我尝试使用http://www.ourdomain.tld/path/one.html ,我可以获取我的CMS的404页面而不是redirect! 是否有可能首先“尝试”一个命名的位置,或者命名的位置是否是后备?

我确定我做错了。 不过,有人能指出我的方向吗?

nginx的/ 1.2.4

谢谢!

正确的方法是:

 location = /path/one.html { return 301 http://www.ourdomain.tld/one-but-different; } location = /path/two.html { return 301 http://www.ourdomain.tld/two-but-different; } etc... 

请尽量避免使用重写。 Nginx不是Apache。 URL重写是configurationWeb服务器的一种低效和棘手的方式。 Nginx更喜欢URL映射。 location前缀匹配是非常快速和高效的。

https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/

在大量(500+)redirect的情况下:

 map $uri $redirect_to { include /path/to/redirects.map; } server { location / { if ($redirect_to) { return 301 http://www.ourdomain.tld$redirect_to; } } } 

redirects.map:

 /path/one.html /one-but-different; /path/two.html /two-but-different; etc... 

另一种方法是使用地图 。

一个例子; closures我的头顶,所以先检查语法错误…:

 map $uri $new { /path/one.html http://www.example.com/new_path_one; /path/two.html http://www.example.com/new_path_two; } server { if ($new) { return 301 $new; } .... }