以 = 开头,表示精确匹配;如只匹配根目录结尾的请求,后面不能带任何字符串。
以^~ 开头,表示uri以某个常规字符串开头,若是匹配到,则不继续往下匹配。不是正则匹配
以~ 开头,表示区分大小写的正则匹配;
以~* 开头,表示不区分大小写的正则匹配
以/ 开头,通用匹配, 若是没有其它匹配,任何请求都会匹配到
*注意 location xxx {} 其中xxx与括号之间不少时候须要空格,最好都加上正则表达式
(location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)json
rewrite只能放在 server{}, location{}, if{}中,而且只能对域名后的文件路径起做用。api
rewrite regex replacement [flag];app
这儿分为server级和location级,其中if可写在server和location中,分别对应server级和location级。同级别中执行顺序看书写顺序。
last : 再也不执行同级rewrite,写在location中从新循环匹配location.
break : 再也不执行同级rewrite,只日后继续执行,不从新匹配location。
redirect : 返回302临时重定向,地址栏会显示跳转后的地址
permanent : 返回301永久重定向,地址栏会显示跳转后的地址
当不写flag时,再次循环同级匹配代理
当表达式只是一个变量时,若是值为空或任何以0开头的字符串都会当作false
直接比较变量和内容时,使用=或!=
~ 正则表达式匹配
~* 不区分大小写的匹配
!~ 区分大小写的不匹配
-f和!-f 用来判断是否存在文件
-d和!-d 用来判断是否存在目录
-e和!-e 用来判断是否存在文件或目录
-x和!-x 用来判断文件是否可执行调试
default_type application/json; 在locaiton中 return 200 '$uri xxx'
set $a "1" if ($a = "1") { return 302 }
proxy_pass http://127.0.0.1:8008/;
这里只讨论在location中的proxy_pass;code
一、 location 使用非正则匹配server
location /api { # 1. proxy_pass http://127.0.0.1:8008/a; # 2. proxy_pass http://127.0.0.1:8008; }
此处要注意,并非uri最后是否有"/",是端口后面是否有"/"字符串
二、location 使用正则匹配域名
location ~ /api/ { proxy_pass http://127.0.0.1:8008; }
三、若是proxy_pass后面有变量,直接去的那个地址,跳出1 2条规则。
server { listen 80; rewrite /a(.*) /b$1; rewrite /b(.*) /c$1 last; rewrite /c(.*) /d$1 break; location / { if ($uri ~ /d/) { rewrite /d/(.*) /api/$1 last; } return 200 '$uri'; } location /api { proxy_pass http://127.0.0.1:8000/test; #端口号后面有/,代理删掉/api。 } location ~ /t/(.*)/t { proxy_pass http://127.0.0.1:8000/test1/$1; #proxy_pass http://127.0.0.1:8000/test1; 写法报错,必须使用变量,使用第3条代理规则。 } }
http://127.0.0.1:8000/test/dd
http://127.0.0.1:8000/test1/test2