因为预算有限,只有一台服务器,想要玩的东西很多,因此这个台服务器上会提供多重服务,所以涉及到的nginx转发就必有重要了php
由nginx作请求代理,提供多种服务html
本片配置笔记中,主要集中如下几个内容java
location [=|~|~*|^~|@] /uri/ { ... }
从上面的语法出发,能够了解到location能够区分为三个部分,接下来一个一个的研究一下nginx
[=|~|~*|^~|@]
=
: 表示精确匹配后面的url~
: 表示正则匹配,可是区分大小写~*
: 正则匹配,不区分大小写^~
: 表示普通字符匹配,若是该选项匹配,只匹配该选项,不匹配别的选项,通常用来匹配目录@
: "@" 定义一个命名的 location,使用在内部定向时,例如 error_page上面定义了几个不一样的符号,表示不一样的匹配规则,那么前后顺序呢?web
直接看这个可能不太好理解,写几个case实际测试一下正则表达式
测试case1:spring
location = /world { return 600; } location = /hello { return 600; } location ~ /hellowo { return 602; } location ^~ /hello { return 601; }
- 请求 localhost/world 返回600 - 请求 localhost/world2 localhost/test/world 返回其余 - 请求 localhost/hello 返回600 - 请求 localhost/hello/123 返回601 - 请求 localhost/hellow 返回601 - 请求 localhost/hellowo 返回601 - 请求 localhost/test/hellowo 返回602 - 请求 localhost/test/hello 返回其余
所以能够知道tomcat
=
是精确完整匹配, 且优秀最高~
和 ^~
同时匹配规则,则 ^~
优先^~
这个不会匹配请求url中后面的路径, 如上面的 /test/hello
没有匹配上^~
不支持正则,和=
相比,范围更广, hellowo
是能够被^~
匹配,可是 =
不会匹配~
路径中只要包含就能够匹配,如上面的 /test/hellowo
返回了602测试case2:服务器
location ~ /hello { return 602; } location ~ /helloworld { return 601; }
- 请求 localhost/world/helloworld 返回 602 - 请求 localhost/helloworld 返回 602
调整一下上面的顺序以后hexo
location ~ /helloworld { return 601; } location ~ /hello { return 602; }
- 请求 localhost/helloworld 返回601 - 请求 localhost/world/helloworld 返回601 - 请求 localhost/helloWorld 返回602
因此同时正则匹配时
~*
测试case3:
location ^~ /hello/ { return 601; } location /hello/world { return 602; }
这种场景中,存在一个没有符号的路由规则,那么实际的测试是怎样呢?
- http://localhost/hello/wor 返回601 - http://localhost/hello/world 返回602 - http://localhost/hello/world23 返回602 - http://localhost/hello/world/123 返回602
从上面case能够看出
这里主要填的就是须要匹配的path路径,根据前面的符号,这里能够填写精确的path路径,也能够填正则表达式,下面则主要针对正则进行说明
. : 匹配除换行符之外的任意字符 ? : 重复0次或1次 + : 重复1次或更屡次 * : 重复0次或更屡次 \d :匹配数字 ^ : 匹配字符串的开始 $ : 匹配字符串的介绍 {n} : 重复n次 {n,} : 重复n次或更屡次 [c] : 匹配单个字符c [a-z] : 匹配a-z小写字母的任意一个 小括号()之间匹配的内容,能够在后面经过$1来引用,$2表示的是前面第二个()里的内容。正则里面容易让人困惑的是\转义特殊字符。
匹配完毕以后内部定义一些列的处理动做,这个涉及到的点比较多,这里不详细展开,后面有空单独捞出
请求path匹配只是第一步,匹配完了以后,如何将请求转发给其余的web服务呢?
一般可见的一种使用姿式就是使用nginx,代理请求,转发到内部的tomact服务上
主要是经过 proxy_pass 这个来实现
location ^~ /webs { proxy_pass http://127.0.0.1:8080/webs; }
将全部以 webs开头的请求,转发给8080端口的tomcat服务上
上面是直接写死转发到一个ip上,若是是多个机器提供服务呢?能够这么玩
## 下面放在http的括号内,做为第一层 upstream test.online { server 120.11.11.11:8080 weight=1; server 120.11.11.12:8080 weight=1; } location ^~ /webs { proxy_pass http://test.online; proxy_redirect default; }
rewrite功能就是,使用nginx提供的全局变量或本身设置的变量,结合正则表达式和标志位实现url重写以及重定向。
rewrite只能放在server{},location{},if{}中,
而且只能对域名后边的除去传递的参数外的字符串起做用, 如
http://zbang.online/a/we/index.php?id=1&u=str 只对/a/we/index.php重写。
语法rewrite regex replacement [flag];
一个case,经过rewrite实现对url的重写,将下面的
location ^~ /hexo { root '/Users/yihui/GitHub/'; } location ~ /hello { rewrite ^(/hello).*$ /hexo/public/index.html last; return 603; }
将hello开头的,所有转发到/hexo/public/index.html
将全部以blog开头的请求,所有转发到某个地方
location ^~ /blog { root '/var/www/html/blog'; }
=
: 表示精确匹配后面的url~
: 表示正则匹配,可是区分大小写~*
: 正则匹配,不区分大小写^~
: 表示普通字符匹配,若是该选项匹配,只匹配该选项,不匹配别的选项,通常用来匹配目录@
: "@" 定义一个命名的 location,使用在内部定向时,例如 error_page匹配顺序以下:
尽信书则不如,已上内容,纯属一家之言,因本人能力通常,见识有限,若有问题,请不吝指正,感激