我对nginx很新,但是以前有过apacheredirect的经验。 我将一个子域名转换为一个子文件夹,并需要一些帮助,让我的重写规则正确的交换机。 子域托pipe在apache服务器上,新的子目录托pipe在我的新nginx服务器上。
下面是包含我所需的redirect的工作htaccess文件:
Options +FollowSymlinks RewriteEngine On RewriteCond %{HTTP_HOST} ^france-property\.frenchentree\.com$ [NC] RewriteCond %{QUERY_STRING} ^$ RewriteRule ^for-sale/(.*)$ http://www.frenchentree.com/property-for-sale/search/$1? [R=301,NE,NC,L] RewriteCond %{HTTP_HOST} ^france-property\.frenchentree\.com$ [NC] RewriteCond %{QUERY_STRING} ^$ RewriteRule ^for-sale$ http://www.frenchentree.com/property-for-sale/search? [R=301,NE,NC,L] RewriteCond %{HTTP_HOST} ^france-property\.frenchentree\.com$ [NC] RewriteCond %{QUERY_STRING} ^$ RewriteRule ^for-sale-details(.*) http://www.frenchentree.com/property-for-sale/details$1? [R=301,NE,NC,L] RewriteCond %{HTTP_HOST} ^france-property\.frenchentree\.com$ [NC] RewriteCond %{QUERY_STRING} ^$ RewriteRule ^buyer-registration(.*)$ http://www.frenchentree.com/property-for-sale/buyer-registration? [R=301,NE,NC,L] RewriteCond %{HTTP_HOST} ^france-property\.frenchentree\.com$ [NC] RewriteCond %{QUERY_STRING} ^$ RewriteRule ^advertise-with-us(.*)$ http://www.frenchentree.com/property-for-sale/advertise-with-us? [R=301,NE,NC,L] RewriteCond %{HTTP_HOST} ^france-property\.frenchentree\.com$ [NC] RewriteCond %{QUERY_STRING} ^$ RewriteRule ^favourites(.*)$ http://www.frenchentree.com/property-for-sale/favourites? [R=301,NE,NC,L] RewriteCond %{HTTP_HOST} ^france-property\.frenchentree\.com$ [NC] RewriteCond %{QUERY_STRING} ^$ RewriteRule ^saved-searches(.*)$ http://www.frenchentree.com/property-for-sale/saved-searches? [R=301,NE,NC,L] RewriteCond %{HTTP_HOST} ^france-property\.frenchentree\.com$ [NC] RewriteCond %{QUERY_STRING} ^$ RewriteRule ^system(.*)$ http://www.frenchentree.com/property-for-sale/system$1? [R=301,NE,NC,L] RewriteCond %{HTTP_HOST} ^france-property\.frenchentree\.com$ [NC] RewriteCond %{QUERY_STRING} ^$ RewriteRule ^$ http://www.frenchentree.com/property-for-sale/? [R=301,NE,NC,L]
我已经运行这个通过winginx转换器,它已经输出了以下内容:
# nginx configuration location / { if ($http_host ~* "^france-property\.frenchentree\.com$"){ rewrite ^/for-sale/(.*)$ http://www.frenchentree.com/property-for-sale/search/$1? redirect; } if ($http_host ~* "^france-property\.frenchentree\.com$"){ rewrite ^/for-sale$ http://www.frenchentree.com/property-for-sale/search? redirect; } if ($http_host ~* "^france-property\.frenchentree\.com$"){ rewrite ^/for-sale-details(.*) http://www.frenchentree.com/property-for-sale/details$1? redirect; } if ($http_host ~* "^france-property\.frenchentree\.com$"){ rewrite ^/buyer-registration(.*)$ http://www.frenchentree.com/property-for-sale/buyer-registration? redirect; } if ($http_host ~* "^france-property\.frenchentree\.com$"){ rewrite ^/advertise-with-us(.*)$ http://www.frenchentree.com/property-for-sale/advertise-with-us? redirect; } if ($http_host ~* "^france-property\.frenchentree\.com$"){ rewrite ^/favourites(.*)$ http://www.frenchentree.com/property-for-sale/favourites? redirect; } if ($http_host ~* "^france-property\.frenchentree\.com$"){ rewrite ^/saved-searches(.*)$ http://www.frenchentree.com/property-for-sale/saved-searches? redirect; } if ($http_host ~* "^france-property\.frenchentree\.com$"){ rewrite ^/system(.*)$ http://www.frenchentree.com/property-for-sale/system$1? redirect; } if ($http_host ~* "^france-property\.frenchentree\.com$"){ rewrite ^/$ http://www.frenchentree.com/property-for-sale/? redirect; } }
我怀疑上述有两个原因… 1.因为子域名托pipe在另一台服务器上,所以我怀疑我需要一个服务器级别redirect,如下所示:
server { server_name france-property.frenchentree.com; rewrite ^/for-sale/(.*) $scheme://www.frenchentree.com/property-for-sale/search/$1 permanent; rewrite ^/for-sale-details(.*) $scheme://www.frenchentree.com/property-for-sale/details$1 permanent; rewrite ^/buyer-registration(.*) $scheme://www.frenchentree.com/property-for-sale/buyer-registration$1 permanent; etc etc etc }
2.因为我明白如果语句不能在nginx中使用,是否可以避免。
有人可以指出我正确的方向,如果只有一个例子,我可以复制? 我通常只是做一些testing,但是,网站是现场和function,是相当受欢迎,所以想要得到它的权利第一次!
先进的非常感谢!
干杯
马特
不惜一切代价自动化工具,根据定义,缺乏正确转换规则的大脑。 Apache和nginx之间没有简单的1对1匹配。 他们有两种不同的心态。 因此,您需要了解您正在做什么,并使用nginxconfiguration它的方式。 这是恕我直言,一个复杂的问题超出了自动化工具的范围。
关于规则:
RewriteCond %{HTTP_HOST}与主机匹配相关。 在nginx方面,它是通过server_name完成的。 语法可能不同:检查文档。 RewriteCond %{QUERY_STRING} ^$表示您明确希望QUERY_STRING值为空。 如果这对你是必须的,那么使用一个匹配$args为空的if块。 这是一个例外,因为我看不到一个更好的方法: if应尽可能避免。 你应该把if块放在隔离正确的URI的location 。 location和return (首选方式)或rewrite (尽可能避免)组合来replace。 当您使用location ,尽可能使用前缀位置,因为正则expression式位置评估确实较慢。 基于性能,最好尽可能地避免它们。 要得到一个干净和可扩展的configuration的build议,请听nginx发明者在nginx.conf用户会议上主持一个谈话: 使用nginx进行可伸缩configuration