NginxRewrite
一、如是说RewriteRewrite依照nginx提供更多的全局函数或他们增设的函数,紧密结合程序语言和象征位同时实现url改写和者链接。
Rewrite和location类似于,都能实现链接,差别是rewrite是在同两个搜索引擎内更动url,而location是对同类别相匹配方向做掌控出访,或是proxypass全权到其它伺服器。Rewrite和location竭尽全力执行次序:竭尽全力执行server下的rewrite竭尽全力执行location相匹配竭尽全力执行location下的rewrite
二、句法和模块表明
rewrite句法文件格式rewrite ;URL程序语言替代的文本改写类别Rewrite:通常都是rewriteRegex:能是数组或是二阶来则表示想相匹配的最终目标URLReplacement:将二阶相匹配的文本换成replacementFlag:flag标注,改写类别:- last:前项准则相匹配顺利完成后,竭尽全力向上相匹配捷伊location URI准则;相等于Apache布兰德(L)记号,则表示顺利完成rewrite,应用程序网页URL门牌号维持不变;通常写在server和if中;- break:前项准则相匹配顺利完成后,中止相匹配,无须相匹配前面的准则,应用程序网页URL门牌号维持不变;通常采用在location中;- redirect:回到302临时性链接,应用程序门牌号会表明链接后的URL门牌号;- permanent:回到301永久性链接,应用程序网页会表明链接后的URL门牌号;server {# 出访/last.html 的这时候,网页文本改写到/index.html 中,并竭尽全力前面的相匹配,应用程序网页URL门牌号维持不变 rewrite /last.html /index.html last;# 出访/break.html 的这时候,网页文本改写到/index.html 中,并暂停先期的相匹配,应用程序网页URL门牌号维持不变; rewrite /break.html /index.html break;# 出访/redirect.html 的这时候,网页间接302或非到/index.html中,应用程序门牌号URL跳为index.html rewrite /redirect.html /index.html redirect;# 出访/permanent.html 的这时候,网页间接301或非到/index.html中,应用程序门牌号URL跳为index.html rewrite /permanent.html /index.html permanent;# 把/html/*.html =>/post/*.html ,301或非 rewrite ^/html/(.+?).html$/post/$1.html permanent;# 把/search/key =>/search.html?keyword=key rewrite ^/search\/([^\/]+?)(\/$)/search.html?keyword=$1 permanent;# 把现阶段搜索引擎的允诺,链接到新搜索引擎上,搜索引擎变动但方向维持不变 rewrite ^/(.*) http://www.jd.com/$1 permanent;}if (函数){}#当函数而已两个函数时,假如值空或任何人以0结尾的数组单厢当作false间接较为函数和文本时,采用=或!=~程序语言相匹配,~*不界定UTF的相匹配,!~界定UTF的不相匹配$args :那个函数等同于允诺行中的模块,同$querystring$contentlength :允诺头中的Content-length字段。$contenttype :允诺头中的Content-Type字段。$documentroot :现阶段允诺在root指令中指定的值。$host :允诺主机头字段,否则为伺服器名称。$httpuseragent :客户端agent信息$httpcookie :客户端cookie信息$limitrate :那个函数能限制连接速率。$requestmethod :客户端允诺的动作,通常为GET或POST。$remoteaddr :客户端的IP门牌号。$remoteport :客户端的端口。$remoteuser :已经经过Auth Basic Module验证的用户名。$requestfilename :现阶段允诺的文件方向,由root或alias指令与URI允诺生成。$scheme : HTTP方法(如http,https)。$serverprotocol :允诺采用的协议,通常是HTTP/1.0或HTTP/1.1。$serveraddr :伺服器门牌号,在顺利完成一次系统调用后能确定那个值。$servername :伺服器名称。$serverport :允诺到达伺服器的端口号。$requesturi :包含允诺模块的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。$uri :不带允诺模块的现阶段URI,$uri不包含主机名,如”/foo/bar.html”。$documenturi :与$uri相同。例子:URL:http://localhost:81/download/stat.php?id=1585378&webid=1585378ServerDir:/var/www/html$host:localhost$serverport:81$requesturi:/download/stat.php?id=1585378&webid=1585378$documenturi:/download/stat.php$documentroot:/var/www/html$requestfilename:/var/www/html/download/stat.php#假如文件不存在则回到400if (!-f $requestfilename){ return 400;}#假如host是www.360buy.com,则301到www.jd.com中if ($host !=”www.jd.com”){ rewrite ^/(.*)$ https://www.jd.com/$1 permanent;}#假如允诺类别是POST则回到405,return不能返回301,302if ($requestmethod = POST){ return 405;}#假如模块中有 a=1则301到指定搜索引擎if ($args ~ a=1){ rewrite ^ http://example.com/ permanent;}-文件名及模块改写 location =/index.html {# 修改默认值 set $name test;# 假如模块中有 name=xx 则采用该值 if ($args ~* name=(\w+?)(&$)){ set $name $1;} # permanent 301链接 rewrite ^/$name.html permanent;}-隐藏真实目录server { root /var/www/html;# 用/htmltest 来掩饰 html location /{ #采用break拿一旦相匹配成功则忽略先期location rewrite /htmltest /html break;} #出访真实门牌号间接报没权限 location /html { return 403;}}
-禁止指定IP出访 location /{ if ($remoteaddr =192.168.1.253){ return 403;} }-假如允诺的文件不存在,则反向全权到localhost 。这里的break也是暂停竭尽全力rewriteif (!-f $requestfilename){ break; proxypass http://127.0.0.1;}-对/images/bla500x400.jpg文件允诺,改写到/resizer/bla.jpg?width=500&height=400门牌号,并会竭尽全力尝试相匹配location。rewrite ^/images/(.*)(\d+)x(\d+)\.(pngjpggif)$/resizer/$1.$4?width=$2&height=$3? last;
ProxyPass
Proxypass反向全权,用的是nginx的Proxy模块。
第一种:location /proxy/{ proxypass http://127.0.0.1/;}全权到URL:http://127.0.0.1/test.html第二种:location /proxy/{ proxypass http://127.0.0.1;#少/}全权到URL:http://127.0.0.1/proxy/test.html第三种:location /proxy/{ proxypass http://127.0.0.1/aaa/;}全权到URL:http://127.0.0.1/aaa/test.html第四种(相对于第三种,最后少两个/ )location /proxy/{ proxypass http://127.0.0.1/aaa;}全权到URL:http://127.0.0.1/aaatest.html- proxysetheader Host $host;作用web伺服器上有多个站点时,用该模块heade。部分程序需要该功能。- Proxypass配合upstream同时实现负载均衡http { include mime.types; defaulttype application/octet-stream; sendfile on; upstream coretomcat { server 192.168.1.253:80 weight=5 maxfails=3 failtimeout=30; server 192.168.1.252:80 weight=1 maxfails=3 failtimeout=30; server 192.168.1.251:80 backup;} server { listen 80; servername www.jd.com; location /web { proxypass http://coretomcat; proxysetheader Host $host;} }}Nginx负载均衡的几种模式
轮询:每个允诺按时间次序逐一分配到不同的后端伺服器,假如后端伺服器down掉,就不在分配;upstream coretomcat { server 192.168.1.253:80 maxfails=3 failtimeout=30; server 192.168.1.252:80 maxfails=3 failtimeout=30;}权重轮询:依照后端伺服器性能不通配置轮询的权重比,权重越高出访的比重越高;upstream coretomcat { server 192.168.1.253:80 weight=2 maxfails=3 failtimeout=30; server 192.168.1.252:80 weight=8 maxfails=3 failtimeout=30;}#假如有十个允诺,八个会指向第二台伺服器,两个指向第一台;IPHash:依照允诺的ip门牌号hash结果进行分配,第一次分配到A伺服器,前面再允诺默认还是分配到A伺服器;能解决Session失效重新登录问题;upstream coretomcat { iphash; server 192.168.1.253:80 maxfails=3 failtimeout=30; server 192.168.1.252:80 maxfails=3 failtimeout=30;}Fair:按后端伺服器的响应时间来分配允诺,响应时间短的优先分配;upstream coretomcat { fair; server 192.168.1.253:80 maxfails=3 failtimeout=30; server 192.168.1.252:80 maxfails=3 failtimeout=30;}Urlhash:按出访url的hash结果来分配允诺,使每个url或非到同两个个后端伺服器,后端伺服器为缓存时较为有效;upstream coretomcat { hash $requesturi; server 192.168.1.253:80 maxfails=3 failtimeout=30; server 192.168.1.252:80 maxfails=3 failtimeout=30;}