美文网首页
Nginx总结之Rewirte

Nginx总结之Rewirte

作者: 夜醉梦紅尘 | 来源:发表于2019-10-11 20:41 被阅读0次

Rewrite对称URL Rewrite,即URL重写,就是把传入Web的请求重定向到其他URL的过程。

Rewrite 语法

server {
 
rewrite {规则} {定向路径} {重写类型} ;
}
例1:rewrite ^/(.*) http://www.test.com/$1 permanent;
说明:                                        
rewrite为固定关键字,表示开始进行rewrite匹配规则
regex部分是 ^/(.*) ,这是一个正则表达式,匹配完整的域名和后面的路径地址
replacement部分是http://www.test.com/$1,$1是取自regex部分()里的内容。匹配成功后跳转到的
URL。
flag部分 permanent表示永久301重定向标记,即跳转到新的 http://www.test.com/$1 地址上

规则:可以是字符串或者正则来表示想匹配的目标url 2、定向路径:表示匹配到规则后要定向的路
径,如果规则里有正则,则可以使用$index来表示正则里的捕获分组 3、重写类型:
rewrite 指令根据表达式来重定向URI,或者修改字符串。可以应用于 server,location, if环境下每行rewrite 指令最后跟一个 flag 标记,支持的 flag 标记有:

last 相当于Apache里的[L]标记,表示完成 rewrite

break 本条规则匹配完成后,终止匹配,不再匹配后面的规则

redirect返回302临时重定向,浏览器地址会显示跳转后的 URL 地址

permanent返回301永久重定向,浏览器地址会显示跳转后URL地址
示例:

server {
 
rewrite /last.html /index.html last;
   # 访问 /last.html 的时候,页面内容重写到 /index.html 中
 
rewrite /break.html /index.html break;
   # 访问 /break.html 的时候,页面内容重写到 /index.html 中,并停止后续的匹配
 
rewrite /redirect.html /index.html redirect;
   # 访问 /redirect.html 的时候,页面直接302定向到 /index.html中
 
rewrite /permanent.html /index.html permanent;
   # 访问 /permanent.html 的时候,页面直接301定向到 /index.html中
 
rewrite ^/html/(.+?).html$ /post/$1.html permanent;
   # 把 /html/*.html => /post/*.html ,301定向
 
rewrite ^/search\/([^\/]+?)(\/|$) /search.html?keyword=$1 permanent;
   # 把 /search/key => /search.html?keyword=key
}

Rewirte参考示例

例1:访问1.html重定向到2.html
#http://www.tianyun.com/abc/a/1.html ==> http://www.tianyun.com/ccc/bbb/2.html
server {
  listen       80;
  server_name
localhost;
   #charset koi8-r;
   #access_log
 
/var/log/nginx/host.access.log
main;
location / {
      root   /usr/share/nginx/html;
      index
index.html index.htm;
  }
  location /abc {
  rewrite .* /ccc/bbb/2.html permanent;
  }
}
location = /abc {
 
rewrite .* /ccc/bbb/2.html permanent;
   #return 301 /ccc/bbb/2.html;
}
location ~ /abc {
 
rewrite .* /ccc/bbb/2.html permanent;
   #return 301 /ccc/bbb/2.html;
}
location ^~ /abc {
 
rewrite .* /ccc/bbb/2.html permanent;
   #return 301 /ccc/bbb/2.html;
}

例2:只替换其中一个参数
#http://www.tianyun.com/2015/ccc/bbb/2.html ==>
http://www.tianyun.com/2014/ccc/bbb/2.html
server {
  listen       80;
  server_name
localhost;
   #charset koi8-r;
   #access_log
 
/var/log/nginx/host.access.log
location / {
      root   /usr/share/nginx/html;
      index
index.html index.htm;
  }
  location /2015 {
    rewrite ^/2015/(.*)$ /2014/$1 permanent;    #$1得到(.*)
 
}
}
location /2015 {
    rewrite ^/2015/(.*)$ /2014/$1 permanent;
}

例3:连接到外网
#http://www.360buy.com/aaa/1.html ==> http://jd.com
server {
  listen       80;
  server_name
localhost;
   #charset koi8-r;
   #access_log
 
/var/log/nginx/host.access.log
location / {
      root   /usr/share/nginx/html;
      index
 
main;
index.html index.htm;
}
   if ( $host ~* 360buy.com ) {
 
rewrite .*
http://jd.com permanent;
   # return 301 http://jd.com;
 
}
}
if ( $host ~* 360buy.com ) {
rewrite .*
https://jd.com permanent;
}

例子5:连接到京东某个具体商品

server {
listen 80;
server_name www.app1.com;
root /usr/share/nginx;
access_log /var/log/www.mytest1.com.log main;
error_log /var/log/www.mytest1.com.error.log;

if ( host ~* 360buy.com ) { rewrite .* https://item.jd.com/request_uri permanent; }
}
访问时候找到某个商品的url(即www.jd.com/后面这串)匹配上 request_uri,在www.360buy.com/100006254092.html
就可以到达具体的商品
经常用的www.360buy.com最好去/etc/hosts注册一下

例子6:正则的运用

#http://www.360buy.com/qf/11-22-33/1.html
==>
http://www.360buy.com/qf/11/22/33/1.html
server {
  listen       80;
  server_name
localhost;
   #charset koi8-r;
   #access_log
 
/var/log/nginx/host.access.log
main;
location / {
      root   /usr/share/nginx/html;
      index
index.html index.htm;
  }
  location /qf {
      root   /usr/share/nginx;
      rewrite ^/qf/([0-9]+)-([0-9]+)-([0-9]+)(.*)$ /qf/$1/$2/$3$4 permanent;
      }
}

常用正则

3、rewrite 常用正则表达式说明
字符 描述
. 匹配除换行符以外的任意字符
? 匹配前面的字符零次或一次
+ 匹配前面的字符一次或多次
* 匹配前面的字符0次或多次
\d 匹配一个数字字符。等价于[0-9]
\ 将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用。如“\n”匹配一个换行符,而“$”则匹配“$”
^ 匹配字符串的开始
$ 匹配字符串的结尾
{n} 匹配前面的字符n次
{n,} 匹配前面的字符n次或更多次
[c] 匹配单个字符c
[a-z] 匹配a-z小写字母的任意一个

小括号()之间匹配的内容,可以在后面通过1来引用,2表示的是前面第二个()里的内容。正则里面容易让人困惑的是\转义特殊字符。

对给定的条件进行判断。如果为真,大括号内的rewrite指令将被执行,if条件可以是如下任何内容:当表达式只是一个变量时,如果值为空或任何以0开头的字符串都会当做false

=或!= 直接比较变量和内容
~ 区分大小写正则表达式匹配
~* 不区分大小写的正则表达式匹配
!~ 区分大小写的正则表达式不匹配
-f和!-f 用来判断文件是否存在
-d和!-d 用来判断目录是否存在
-e和!-e 用来判断文件或目录是否存在
-x和!-x 用来判断文件是否可执行
(2)if判断可使用的全局变量
变量名称 变量说明
$args 这个变量等于请求行中的参数,同$query_string
$content_length 请求头中的Content-length字段
$content_type 请求头中的Content-Type字段
$document_root 当前请求在root指令中指定的值
$host 请求主机头字段,否则为服务器名称
$http_user_agent 客户端agent信息
$http_cookie 客户端cookie信息
$limit_rate 这个变量可以限制连接速率
$request_method 客户端请求的动作,通常为GET或POST
$remote_addr 客户端的IP地址
$remote_port 客户端的端口
$remote_user 已经经过Auth Basic Module验证的用户名
$request_filename 当前请求的文件路径,由root或alias指令与URI请求生成
$scheme HTTP方法(如http,https)
$server_protocol 请求使用的协议,通常是HTTP/1.0或HTTP/1.1
$server_addr 服务器地址,在完成一次系统调用后可以确定这个值
$server_name 服务器名称
$server_port 请求到达服务器的端口号
$request_uri 包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”
$uri 不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”
$document_uri 与$uri相同

例:

http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:/var/www/html
$request_filename:/var/www/html/test1/test2/test.php

相关文章

网友评论

      本文标题:Nginx总结之Rewirte

      本文链接:https://www.haomeiwen.com/subject/itsapctx.html