我有一个问题,Nginxredirect到错误的url,如果没有尾随/提供。
我有haproxy设置委托多个服务器之间的请求。
curl输出:
➜ ~ curl -i www.ordify.com/register HTTP/1.1 301 Moved Permanently Server: nginx/1.1.19 Date: Tue, 14 Aug 2012 08:10:39 GMT Content-Type: text/html Content-Length: 185 Location: http://www.ordify.com:4000/register/ Connection: close <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx/1.1.19</center> </body> </html>
Nginxconfiguration:
server { listen *:4000; server_name ordify.com; access_log /var/log/nginx/website-com.log; rewrite_log on; error_page 405 = $uri; location / { root /home/website/en/; index index.html index.htm; } if ($host != 'www.ordify.com' ) { rewrite ^/(.*)$ http://www.ordify.com/$1 permanent; } rewrite /registrieren/ http://www.ordify.com/register/ permanent; rewrite /presse/ http://www.ordify.com/press/ permanent; }
我已经尝试使用http://wiki.nginx.org/HttpCoreModule#server_name_in_redirect没有成功。
if ($host != 'www.ordify.com' ) { rewrite ^/(.*)$ http://www.ordify.com/$1 permanent; }
这是一个无效的方法。 请参阅: http : //nginx.org/en/docs/http/converting_rewrite_rules.html
正确的方法是定义一个单独的服务器来捕获所有其他域。 服务器名称可以使用正则expression式来定义。 尝试这个:
server { listen *:4000; server_name www.ordify.com; ... } server { listen *:4000; server_name ~^(?!www)\.ordify\.com$; return 301 http://www.ordify.com$request_uri; ... }
?!www – 负向预测 ,意思是匹配没有跟随www东西。