Nginx Rewrite 的语法规则,语法: rewrit


Nginx rewrite只是会改写路径部分的东西,不会改动用户的输入参数,因此这里的if规则里面,你无需关心用户在浏览器里输入的参数,rewrite后会自动添加的,因此,我们只是加上了一个?号和后面我们想要的一个小小的参数 ***https=1就可以了。

语法: rewrite regex replacement flag
默认: none
作用域: server, location, if

This directive changes URI in accordance with the regular expression and the replacement string. Directives are carried out in order of appearance in the configuration file.
这个指令根据表达式来更改URI,或者修改字符串。指令根据配置文件中的顺序来执行。

Be aware that the rewrite regex only matches the relative path instead of the absolute URL. If you want to match the hostname, you should use an if condition, like so:
注意重写表达式只对相对路径有效。如果你想配对主机名,你应该使用if语句。

nginx的rewrite语法规则参考:

文件及目录匹配

-f和!-f      用来判断是否存在文件-d和!-d    用来判断是否存在目录-e和!-e    用来判断是否存在文件或目录-x和!-x    用来判断文件是否可执行

正则表达式匹配

~     为区分大小写匹配~*   为不区分大小写匹配!~    为区分大小写不匹配!~*  为不区分大小写不匹配

Nginx Rewrite 基本标记(flags)

last              相当于Apache里的[L]标记,表示完成rewrite,不再匹配后面的规则break           中止Rewirte,不再继续匹配redirect        返回302临时重定向,地址栏会显示跳转后的地址permanent   返回301永久重定向,地址栏会显示跳转后的地址

Nginx 的一些可用的全局变量,可用做条件判断

$args$content_length$content_type$document_root$document_uri$host$http_user_agent$http_cookie$limit_rate$request_body_file$request_method$remote_addr$remote_port$remote_user$request_filename$request_uri$query_string$scheme$server_protocol$server_addr$server_name$server_port$uri

相关内容