这是我第一次遇到StackOverflow问题,请耐心等待。
Nginx为我们提供了许多不同的网站,我们有很多来自不同供应商的迁移客户的重定向等。我们已经设置了一个/ includes目录,其中包含我们迁移的每个域的重定向文件。有时,我们需要从包含get参数的URL写入重定向:
http://example.com/content/default.aspx?NewsId=28
为此,我们在名为example.com-redirects的nginx / includes文件中执行此操作
location ^~ /content/default.aspx {
if ($args ~ "NewsId=28") { rewrite ^ http://example.com/news? permanent; break; }
# add more statements like the one above
}
到目前为止,这对我们来说还算合适。不幸的是,我们需要做同样的事情,但对于可能具有相同get参数的不同域。当然nginx不允许重复的位置。
location ^~ /content/default.aspx {
if ($args ~ "NewsId=28") { rewrite ^ http://differentexample.org/news? permanent; break; }
}
我尝试了几种不同的解决方案,这些都给了我语法错误。我公司的任何人都不再是nginx专家,所以我真的可以帮助解决这个问题。我在位置块中添加了if ($host ~ "example.com")
,这给了我一个错误。我已经尝试在if ($host ~ "example.com")
块中添加位置块。两次nginx告诉我,我不能把它放在那里。
我通常在庞大的知识库中找到我的答案,这个知识库就是互联网,但似乎正在为此解决问题,我们在启动此客户端之前已经没有时间了。
答案 0 :(得分:0)
不使用if,而是使用变量。在虚拟主机配置中,您可以设置此变量。在包含您使用它的位置:
server {
...
set $redirect_host "example.org";
include /includes/news28.conf;
...
}
# include part
location /foo {
if ($args ~ "NewsId=28") { rewrite $scheme://$redirect_host/news? permanent; }
}
当然,您还需要在已使用此include的主机的服务器配置中设置该变量。希望这会有所帮助。