NetScaler

将Apache mod_rewrite规则迁移到默认策略语法

Apache HTTP Server提供了一个名为mod_rewrite的引擎,用于重写HTTP请求url。如果将mod_rewrite规则从Apache迁移到NetScaler,就可以提高后端服务器的性能。此外,由于NetScaler通常对多个(有时是数千个)Web服务器进行负载平衡,在将规则迁移到NetScaler之后,您就拥有了对这些规则的单一控制点。

下面是mod_rewrite函数的示例,以及将这些函数转换为NetScaler上的Rewrite和Responder策略。

将URL变体转换为规范URL

对于某些Web服务器,一个资源可以有多个url。尽管必须使用和分发规范url,但其他url可以作为快捷方式或内部url存在。您可以确保用户看到规范的URL,而不管用于发出初始请求的URL是什么。

在以下示例中,URL用户/ ~被转换为/ u /用户

用于转换URL的Apache mod_rewrite解决方案

RewriteRule ^/~([^/]+)/?(.*) /u/$1/$2[R] 

URL转换的解决方案

add responder action act1 redirect '"/u/"+HTTP.REQ.URL.AFTER_STR("/~")' -bypassSafetyCheck yes add responder policy pol1 'HTTP.REQ.URL.STARTSWITH("/~") && HTTP.REQ.URL.LENGTH.GT(2)' act1 bind responder global pol1 100 

将主机名变体转换为规范主机名

您可以强制使用特定的主机名来访问站点。例如,您可以强制使用www.example.com而不是example.com。

Apache mod_rewrite解决方案,用于为运行在非80端口上的站点强制执行特定的主机名

RewriteCond %{HTTP_HOST} !^www.example.com RewriteCond %{HTTP_HOST} !^$ RewriteCond %{SERVER_PORT} !^80$ RewriteRule ^/(.*) http://www.example.com:%{SERVER_PORT}/$1 [L,R] 

Apache mod_rewrite解决方案,用于为运行在80端口上的站点强制执行特定的主机名

^www.example.com RewriteCond %{HTTP_HOST} !^$ RewriteRule ^/(.*) http://www.example.com/$1 [L,R] 

为在非80端口上运行的站点强制执行特定主机名的解决方案

add responder action act1 redirect '"http://www.example.com:"+CLIENT.TCP.DSTPORT+HTTP.REQ.URL' -bypassSafetyCheck yes add responder policy pol1 '!HTTP.REQ.HOSTNAME.CONTAINS("www.example.com")&&!HTTP.REQ.HOSTNAME.EQ("")&&!HTTP.REQ.HOSTNAME.PORT.EQ("")&&!——NeedCopy >

为在端口80上运行的站点强制执行特定主机名的解决方案

add responder action act1 redirect '"http://www.example.com"+HTTP.REQ.URL' -bypassSafetyCheck yes add responder policy pol1 '!HTTP.REQ.HOSTNAME.CONTAINS("www.example.com")&&!——NeedCopy >

移动文档根目录

Web服务器的文档根目录通常基于URL“/”。但是,文档根目录可以是任何目录。如果流量从顶级的“/”目录更改到另一个目录,则可以将其重定向到文档根。

在以下示例中,将文档根目录从/更改为/e/www。前两个示例只是将一个字符串替换为另一个字符串。第三个示例更加通用,因为除了替换根目录之外,它还保留了URL的其余部分(路径和查询字符串),例如,将/example/file.html重定向到/e/www/example/file.html。

用于移动文档根的Apache mod_rewrite解决方案

RewriteRule ^/$ /e/www/ [R] 

移动文档根的解决方案

add responder action act1 redirect ' ' /e/www/ ' ' -bypassSafetyCheck yes add responder policy pol1 'HTTP.REQ.URL.EQ("/")' act1 bind responder global pol1 100 

移动文档根并向请求附加路径信息的解决方案

add responder action act1 redirect '"/e/www"+HTTP.REQ。URL' -bypassSafetyCheck yes add responder policy pol1 '! http . req .URL. startwith ("/e/www/")' act1 bind responder global pol1 100 END 

将主目录移动到新的web服务器

您可能希望将发送到Web服务器上主目录的请求重定向到另一个Web服务器。例如,如果随着时间的推移,新的Web服务器正在取代旧的Web服务器,当您将主目录迁移到新位置时,您需要将对已迁移的主目录的请求重定向到新的Web服务器。

在下面的示例中,新Web服务器的主机名为newserver

用于重定向到另一个Web服务器的Apache mod_rewrite解决方案

RewriteRule ^/(.+) http://newserver/$1 [R,L] 

用于重定向到另一个Web服务器的NetScaler解决方案(方法1)

添加响应器动作act1重定向'"http://newserver"+HTTP.REQ。URL' -bypassSafetyCheck yes add responder policy pol1 'HTTP.REQ.URL.REGEX_MATCH(re#^/(.+)#)' act1 bind responder global pol1 100 END 

重定向到另一个Web服务器的解决方案(方法2)

添加响应器动作act1重定向'"http://newserver"+HTTP.REQ。URL' -bypassSafetyCheck yes add responder policy pol1 'HTTP.REQ.URL.LENGTH.GT(1)' act1 bind responder global pol1 100 END 

使用结构化的主目录

通常,拥有数千用户的站点具有结构化的主目录布局。例如,每个主目录可能驻留在使用用户名的第一个字符命名的子目录下。例如,jsmith (/~jsmith/anypath)的主目录可能是/home/j/smith/.www/anypath的主目录rvalveti (/ ~ rvalveti / anypath)可能是/home/r/rvalveti/.www/anypath

以下示例将请求重定向到主目录。

结构化主目录的Apache mod_rewrite解决方案

RewriteRule ^ / ~ (([a - z]) [a-z0-9] +) (. *) /home/$ 2 / 1美元/。www 3美元< !——NeedCopy >

结构化主目录的解决方案

NetScaler解决方案为结构化的主目录添加重写动作act1替换'HTTP.REQ。URL' '"/home/"+ HTTP.REQ.URL.AFTER_STR("~").PREFIX(1)+"/"+ HTTP.REQ.URL.AFTER_STR("~").BEFORE_STR("/")+"/.www"+HTTP.REQ.URL.SKIP(\'/\',1)' -bypassSafetyCheck是添加重写策略pol1 ' http . req .URL. path . startwith ("/~")' act1绑定重写全局pol1 100 

将无效的url重定向到其他web服务器

如果URL无效,则必须将其重定向到必须的Web服务器。例如,如果在URL中命名的文件在URL中命名的服务器上不存在,则必须重定向到另一个Web服务器。

在Apache上,可以使用mod_rewrite执行此检查。在NetScaler上,HTTP调用可以通过在服务器上运行脚本来检查服务器上的文件。在下面的NetScaler示例中,一个名为file_check.cgi的脚本处理URL并使用该信息检查服务器上是否存在目标文件。脚本返回TRUE或FALSE, NetScaler使用脚本返回的值来验证策略。

除了执行重定向之外,NetScaler还可以添加自定义标头,或者像第二个NetScaler示例一样,在响应体中添加文本。

如果URL错误,用于重定向的Apache mod_rewrite解决方案

RewriteCond /your/docroot/%{REQUEST_FILENAME} !-f RewriteRule ^(.+) http://webserverB.com/$1 [R] 

用于URL错误重定向的NetScaler解决方案(方法1)

add HTTPCallout调用集策略HTTPCallout Call -IPAddress 10.102.59.101 -port 80 -hostExpr '"10.102.59.101"' -returnType BOOL -ResultExpr 'HTTP.RES.BODY(100). contains ("True")' -urlStemExpr '"/cgi-bin/file_check.cgi"' -parameters query=http.req.url. url. add - HTTPCallout Call -IPAddress 10.102.59.101 -port 80 -hostExpr '"path -headers Name("ddd") add responder action act1 redirect '"http://webserverB.com"+HTTP.REQ.URL' -bypassSafetyCheck yes add responder policy pol1 '!SYS.HTTP_CALLOUT(call)' act1绑定响应器全局pol1 100 

在URL错误时重定向的NetScaler解决方案(方法2)

add HTTPCallout调用集策略HTTPCallout Call -IPAddress 10.102.59.101 -port 80 -hostExpr '"10.102.59.101"' -returnType BOOL -ResultExpr 'HTTP.RES.BODY(100). contains ("True")' -urlStemExpr '"/cgi-bin/file_check.cgi"' -parameters query=http.req.url. url. add - HTTPCallout Call -IPAddress 10.102.59.101 -port 80 -hostExpr '"add responder action act1 respondwith '"HTTP/1.1 302 Moved temporary \r\nLocation: http://webserverB.com"+HTTP. req . url +"\r\n\r\nHTTPCallout Used"' -bypassSafetyCheck yes add responder policy pol1 '!SYS.HTTP_CALLOUT(call)' act1绑定响应器全局pol1 100 

基于时间重写URL

您可以根据时间重写URL。下面的示例将example.html的请求更改为example.day.html或example.night.html,具体取决于一天中的时间。

基于时间重写URL的Apache mod_rewrite解决方案

RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700 RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900 RewriteRule ^example\.html$ example.day.html [L] RewriteRule ^example\.html$ example.night.html 

基于时间重写URL的NetScaler解决方案

在'HTTP.REQ.URL.PATH.SUFFIX(\'.\',0)' ' ' '之前添加重写动作act1 insert_' '添加重写动作act2 insert_before 'HTTP.REQ.URL。PATH.SUFFIX(\'.\',0)' '"night."' add rewrite policy pol1 'SYS.TIME.WITHIN(LOCAL 07h 00m,LOCAL 18h 59m)' act1 add rewrite policy pol2 'true' act2 bind rewrite global pol1 101 bind rewrite global pol2 102 

重定向到新文件名(用户不可见)

如果重命名一个网页,您可以继续支持旧的URL以实现向后兼容,同时防止用户识别页面已重命名。

在下面的前两个示例中,基目录为/ ~例子.第三个例子容纳了URL中任何基本目录和查询字符串的存在。

用于管理固定位置的文件名更改的Apache mod_rewrite解决方案

RewriteEngine on RewriteBase /~example/ RewriteRule ^foo\.html$ bar.html 

用于管理固定位置的文件名更改的NetScaler解决方案

add rewrite action act1 replace 'HTTP.REQ.URL.AFTER_STR("/~example").SUBSTR("foo.html")' '"bar.html"' add rewrite policy pol1 'HTTP.REQ.URL.ENDSWITH("/~example/foo.html")' act1 bind rewrite global pol1 100 

文件名更改的解决方案,而不管URL中的基目录或查询字符串

添加重写动作act1替换'HTTP.REQ.URL.PATH.SUFFIX(\'/\',0)' '"bar.html"'添加重写策略pol1 'HTTP.REQ.URL.PATH.CONTAINS("foo.html")' act1绑定重写全局pol1 100 

重定向到新文件名(用户可见的URL)

如果重命名一个网页,为了向后兼容,您可能希望继续支持旧的URL,并允许用户通过更改浏览器中显示的URL看到页面已重命名。

在下面的前两个示例中,重定向发生在基目录为/ ~例子.第三个例子容纳了URL中任何基本目录和查询字符串的存在。

Apache mod_rewrite解决方案,用于更改浏览器中显示的文件名和URL

RewriteBase /~example/ RewriteRule ^old\.html$ new.html [R] 

NetScaler解决方案,用于修改浏览器中显示的文件名和URL

add responder action act1 redirect 'HTTP.REQ.URL.BEFORE_STR("foo.html")+"new.html"' -bypassSafetyCheck yes add responder policy pol1 'HTTP.REQ.URL.ENDSWITH("/~example/old.html")' act1 bind responder global pol1 100 

如何更改浏览器中显示的文件名和URL,而不考虑URL中的基目录或查询字符串

add responder action act1重定向'HTTP.REQ.URL.PATH.BEFORE_STR("old.html")+"new.html"+HTTP.REQ.URL.AFTER_STR("old.html")' -bypassSafetyCheck yes添加responder policy pol1 'HTTP.REQ.URL.PATH.CONTAINS("old.html")' act1绑定responder global pol1 100 

容纳依赖于浏览器的内容

为了适应特定于浏览器的限制—至少对于重要的顶级页面—有时需要对浏览器类型和版本设置限制。例如,您可能希望为最新的Netscape变体设置一个最大版本,为Lynx浏览器设置一个最小版本,为所有其他浏览器设置一个平均特性版本。

下面的例子作用于HTTP报头“User-Agent”,这样,如果这个报头以“Mozilla/3”开头,则页面MyPage.html是重写MyPage.NS.html.如果浏览器是“Lynx”或“Mozilla”版本1或2,则URL为MyPage.20.html.所有其他浏览器都接收该页面MyPage.32.html

针对浏览器特定设置的Apache mod_rewrite解决方案

RewriteCond % {HTTP_USER_AGENT} ^ Mozilla / 3。*重写Rule ^MyPage\.html$ MyPage.NS.html [L] RewriteCond %{HTTP_USER_AGENT} ^Lynx/.* [OR] RewriteCond %{HTTP_USER_AGENT} ^Mozilla/[12].* RewriteRule ^MyPage\.html$ MyPage.20.html [L] RewriteRule ^fMyPage\.html$ MyPage.32.html [L] NetScaler solution for browser-specific settings add patset pat1 bind patset pat1 Mozilla/1 bind Patset pat1 Mozilla/2 bind patset pat1 Lynx bind Patset pat1 Mozilla/3 add rewrite action act1 insert_before 'HTTP.REQ.URL.SUFFIX' '"NS."' add rewrite action act2 insert_before 'HTTP.REQ.URL.SUFFIX' '"20."' add rewrite action act3 insert_before 'HTTP.REQ.URL.SUFFIX' '"32."' add rewrite policy pol1 'HTTP.REQ.HEADER("User-Agent").STARTSWITH_INDEX("pat1").EQ(4)' act1 add rewrite policy pol2 'HTTP.REQ.HEADER("User-Agent").STARTSWITH_INDEX("pat1").BETWEEN(1,3)' act2 add rewrite policy pol3 '!HTTP.REQ.HEADER("User-Agent").STARTSWITH_ANY("pat1")' act3 bind rewrite global pol1 101 END bind rewrite global pol2 102 END bind rewrite global pol3 103 END 

阻止机器人进入

您可以阻止机器人从一个特定目录或一组目录检索页面,以减少来往于这些目录的流量。可以根据特定的位置限制访问,也可以根据User-Agent HTTP头中的信息阻止请求。

在以下示例中,要阻止的Web位置为/ / foo /弧/ ~例子,需要屏蔽的IP地址分别为1.1.1.1和10.10.10.10,机器人名称为NameOfBadRobot

阻塞路径和User-Agent头的Apache mod_rewrite解决方案

RewriteCond % {HTTP_USER_AGENT} ^ NameOfBadRobot。*重写econd %{REMOTE_ADDR} ^123\.45\.67\。[8]美元RewriteRule ^ / / foo /弧/ ~例子。+ - [f] 

用于阻塞路径和User-Agent头的NetScaler解决方案

add responder action act1 respondwith '"HTTP/1.1 403 Forbidden\r\n\r\n"' add responder policy pol1 'HTTP. req . header ("User_Agent").STARTSWITH("NameOfBadRobot")&& client . ip . src . eq (123.45.67.8)&& client . ip . src . eq (123.45.67.9) && HTTP. req . url .STARTSWITH("/~example/foo/arc")' act1 bind responder global pol1 100 

阻止对内联图像的访问

如果您发现人们经常到您的服务器复制内联图形以供自己使用(并产生不必要的流量),那么您可能需要限制浏览器发送HTTP referer头的能力。

Apache mod_rewrite解决方案,用于阻止对内联图像的访问

^$ RewriteCond %{HTTP_REFERER} !^ttp://www.example.com/~graphics/.*$ RewriteRule .*\.gif$ - [F] 

用于阻止访问内联图像的NetScaler解决方案

add patset pat1 bind patset pat1 .gif bind patset pat1 .jpeg add responder action act1 respondwith '"HTTP/1.1 403 Forbidden\r\n\r\n"' add responder policy pol1 '!HTTP. req . header ("Referer").EQ("") && !HTTP. req . header ("Referer"). startwith ("ttp://www.example.com/~graphics/")&& &&HTTP. req . url . endswith_any ("pat1")' act1 bind responder global pol1 100 

为了防止用户知道服务器端的应用程序或脚本细节,可以对用户隐藏文件扩展名。要做到这一点,您可能需要支持无扩展链接。您可以通过使用重写规则向所有请求添加扩展,或者有选择地向请求添加扩展来实现此行为。

下面的前两个示例演示向所有请求url添加扩展。在最后一个示例中,添加了两个文件扩展名中的一个。在最后一个示例中,mod_rewrite模块可以很容易地找到文件扩展名,因为该模块驻留在Web服务器上。相反,NetScaler必须调用HTTP调用来检查Web服务器上请求文件的扩展名。根据调出响应,NetScaler将.html或.php扩展名添加到请求URL中。

注意:

在第二个NetScaler示例中,使用HTTP调用来查询服务器上托管的名为file_check.cgi的脚本。这个脚本检查在调用中提供的参数是否是有效的文件名。

为所有请求添加.php扩展的Apache mod_rewrite解决方案

RewriteRule ^/ ([a-z]+)$ $1.php [L] 

NetScaler策略,用于向所有请求添加.php扩展名

添加重写动作act1 insert_after 'HTTP.REQ。URL' ' '".php"' add rewrite policy pol1 'HTTP.REQ.URL.PATH.REGEX_MATCH(re#^/([a-z]+)$#)' act1 bind rewrite global pol1 100 

Apache mod_rewrite解决方案,用于向请求添加.html或.php扩展

RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^/?([a-zA-Z0-9]+)$ $1.php [L] RewriteCond %{REQUEST_FILENAME}.html -f RewriteRule ^/?([a-zA-Z0-9]+)$ $1.html [L] 

NetScaler策略,用于向请求添加.html或.php扩展

add HTTPCallout Call_html add HTTPCallout Call_php设置策略HTTPCallout Call_html -IPAddress 10.102.59.101 -port 80 -hostExpr '"10.102.59.101"' -returnType BOOL -ResultExpr 'HTTP.RES.BODY(100).CONTAINS("True")' -urlStemExpr '"/cgi-bin/file_check.cgi"' -parameters query=http.req.url+".html"设置策略HTTPCallout Call_php -IPAddress 10.102.59.101 -port 80 -hostExpr '"10.102.59.101"' -returnType BOOL -ResultExpr 'HTTP.RES.BODY(100).CONTAINS("True")' -urlStemExpr '"/cgi-bin/file_check.cgi"'-parameters query=http.req.url+".php" add patset pat1 bind patset pat1 .html bind patset pat1 .php bind patset pat1 .asp bind patset pat1 .cgi add rewrite action act1 insert_after ' http.req.url。在“HTTP.REQ.URL”后添加重写动作act2 insert_HTTP.REQ.URL.CONTAINS_ANY("pat1") && SYS.HTTP_CALLOUT(Call_html)' act1 add rewrite policy pol2 '!HTTP.REQ.URL.CONTAINS_ANY("pat1") && SYS.HTTP_CALLOUT(Call_php)' act2 bind rewrite global pol1 100 END bind rewrite global pol2 101 END 

将工作URI重定向为新格式

假设你有一组类似于下面的工作url:

/ index . php ?id = nnnn < !——NeedCopy >

将这些url更改为/ nnnn并确保搜索引擎将它们的索引更新为新的URI格式,你需要做以下工作:

  • 将旧uri重定向到新uri,以便搜索引擎更新它们的索引。
  • 将新的URI改写为旧的URI,以便index . php脚本运行正确。

为此,可以在查询字符串中插入标记代码(确保用户看不到标记代码),然后删除index.php脚本的标记代码。

以下示例仅在查询字符串中没有出现标记时才从旧链接重定向到新格式。使用新格式的链接将被改写为旧格式,并向查询字符串添加一个标记。

Apache mod_rewrite的解决方案

RewriteCond %{QUERY_STRING} !marker RewriteCond %{QUERY_STRING} id=([a- za - z0 -9_+]+) RewriteRule ^/?index\.php$ %1?(R, L) RewriteRule ^ / ? ([-a-zA-Z0-9_ +] +) $ index . php ?记号id = 1美元[L] NetScaler解决方案添加响应器动作act_redirect重定向的HTTP.REQ.URL.PATH.BEFORE_STR (index . php) + HTTP.REQ.URL.QUERY.VALUE (" id ") -bypassSafetyCheck是的添加响应方政策pol_redirect ! HTTP.REQ.URL.QUERY.CONTAINS(“标记”)& & HTTP.REQ.URL.QUERY.VALUE (" id ") (-a-zA-Z0-9 .REGEX_MATCH (re / _+]+/) && HTTP.REQ.URL.PATH.CONTAINS (index . php)的act_redirect绑定响应器全球pol_redirect 100终端添加重写行动act1替换的HTTP.REQ.URL.PATH.SUFFIX (\ ' / \ ', 0) ''"index.phpmark &id="+HTTP.REQ.URL.PATH.SUFFIX(\'/\',0)' -bypassSafetyCheck yes add rewrite policy pol1 '!HTTP.REQ.URL.QUERY.CONTAINS("marker")' act1 bind rewrite global pol1 100 END 

确保所选页面使用安全服务器

为了确保所选网页只使用安全服务器,您可以使用以下Apache mod_rewrite代码或NetScaler Responder策略。

Apache mod_rewrite的解决方案

RewriteCond %{SERVER_PORT} !^443$ RewriteRule ^/?(page1|page2|page3|page4|page5)$ https://www.example.com/%1 [R,L] 

使用正则表达式的NetScaler解决方案

add responder action res_redirect redirect '"https://www.example.com"+HTTP.REQ.URL' -bypassSafetyCheck yes add responder policy pol_redirect '!CLIENT.TCP.DSTPORT.EQ(443)&&HTTP.REQ.URL.REGEX_MATCH(re/page[1-5]/)' res_redirect bind responder global pol_redirect 100 END 

使用模式集的NetScaler解决方案

add patset pat1 bind patset pat1 page1 bind patset pat1 page2 bind patset pat1 page3 bind patset pat1 page4 bind patset pat1 page5 add responder action res_redirect redirect '"https://www.example.com"+HTTP.REQ.URL' -bypassSafetyCheck yes add responder policy pol_redirect '!CLIENT.TCP.DSTPORT.EQ(443)&&HTTP.REQ.URL.CONTAINS_ANY("pat1")' res_redirect bind responder global pol_redirect 100 END 
将Apache mod_rewrite规则迁移到默认策略语法

在这篇文章中