nginx rewrite 学习

#nginx rewrite 学习

来源文档 http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

break语法

跳出rewrite

Syntax:	break;
Default:	—
Context:	server, location, if

if (condition){
    limit_rate 10k;
    break;
}

if 语法

1.空字符串或者0被判为false.
2.= 或者 !=判断相等不等, 用于字符串比较
3.~ 区分大小写正则来匹配,!~不匹配
~* 不区分大小写的正则,!~* 不匹配
如果表达式用有}和;整个表达式应该用单引号或者双引号引起来
Regular expressions can contain captures that are made available for later reuse in the $1…$9 variables
4.-f -d -e -x判断是否为文件,为目录,是否存在,是否可执行.不存在前面加!

Syntax:	if (condition) { ... }
Default:	—
Context:	server, location

if ($http_user_agent ~ MSIE) {
    rewrite ^(.*)$ /msie/$1 break;
}

if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
    set $id $1;
}

if ($request_method = POST) {
    return 405;
}

if ($slow) {
    limit_rate 10k;
}

if ($invalid_referer) {
    return 403;
}

return语法

作用返回状态码

Syntax:	return code [text];
        return code URL;
        return URL;
Default:	—
Context:	server, location, if



rewrite语法

重写
last
stops processing the current set of ngx_http_rewrite_module directives and starts a search for a new location matching the changed URI;
break
stops processing the current set of ngx_http_rewrite_module directives as with the break directive;
redirect
returns a temporary redirect with the 302 code; used if a replacement string does not start with “http://”, “https://”, or “$scheme”;
permanent
returns a permanent redirect with the 301 code.

Syntax:	rewrite regex replacement [flag];
Default:	—
Context:	server, location, if

server {
    ...
    rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;
    rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra  last;
    return  403;
    ...
}

location /download/ {
    rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;
    rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra  break;
    return  403;
}

rewrite_log语法

Syntax:	rewrite_log on | off;
Default:	
rewrite_log off;
Context:	http, server, location, if

set语法

设置变量

Syntax:	set $variable value;
Default:	—
Context:	server, location, if


发布了30 篇原创文章 · 获赞 0 · 访问量 1009

猜你喜欢

转载自blog.csdn.net/l4oli/article/details/102460776
今日推荐