nginx正则表达式和rewrite用法.docxVIP

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
  4. 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
  5. 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们
  6. 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
  7. 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
nginx正则表达式和rewrite用法

一、首先介绍下Nginx的rewrite基本规则:1.正则表达式的匹配:~: 区分大小写匹配~*: 不区分大小写匹配!~ : 区分大小写写不匹配!~*:不区分大小写不匹配2.文件和目录的匹配:-f 和!-f用来判断是否存在文件-d和!-d用来判断是否存在目录-e和!-e用来判断是否存在文件或目录-x和!-x用来判断文件是否可执行3.rewrite模块基本语法:语法: rewrite regex replacement flag 作用域: server, location, if 这个指令根据表达式来更改URI,或者修改字符串。指令根据配置文件中的顺序来执行。 注意重写表达式只对相对路径有效。如果你想配对主机名,你应该使用if语句,如下: #rewrites http://www.mydomain.nl/foo = http://mydomain.nl/fooif ($host ~* www\.(.*)) { set $host_without_www $1; rewrite ^(.*)$ http://$host_without_www$1 permanent; # $1 contains /foo, not /foo}如果用于替换的字符串以 http:// 开头,则客户端被重定向,而且其他的rewrite指令将被终结以下是来自Nginx_Wiki对flags的解释:Flags can be any of the following:last - completes processing of rewrite directives, after which searches for corresponding URI and locationbreak - completes processing of rewrite directivesredirect - returns temporary redirect with code 302; it is used if the substituting line begins with http://permanent - returns permanent redirect with code 301对last和break的解释:Note that if an redirect is relative (has no host part), then when redirecting Nginx uses the Host headerif the header match name of server_name directive or the first name of server_name directive, if the headerdoes not match or is absent. If no server_name is set, then the local hostname is used. If you want Nginx toalways use the Host header, you can use a wildcard * server_name (but see the restrictions on doing so).示例:rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last;return 403;备注:But if we place these directives in location /download/, then it is necessary to replace flag last by break, otherwise nginx will hit the 10 cycle limit and return error 500: location /download/ { rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break; rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra break; return 403;}If in the line of replacement arguments are indicated, then the rest of the request arguments are appended to them. To avoid having the

文档评论(0)

yan698698 + 关注
实名认证
文档贡献者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档