nginx rewrite 重写

来源:互联网 发布:怎么消除马赛克软件 编辑:程序博客网 时间:2024/06/18 07:47

rewrite 重写

Nginx内置常用变量

$arg 请求中的参数; $content_length HTTP请求信息里的”Content-Length” $content_type 请求信息里的”Content-Type” $document_root 针对当前请求的根路径设置值 $fastcgi_script_name 脚本名称 $uri 请求的URI,可能和最初的值有不同,比如经过重定向之类的。 $document_uri 与$uri相同 $host 请求信息中的”Host”,如果请求中没有Host行,则等于设置的服务器名 $limit_rate 对连接速率的限制; $request_method 请求的方法,比如”GET”、”POST”等 $remote_addr 客户端地址 $remote_port 客户端端口号 $remote_user 客户端用户名,认证用 $request_filename 当前请求的文件路径名 $request_body_file 客户端请求主体信息的临时文件名 $request_uri 请求的URI,带参数 $args 这个变量等于GET请求中的参数 $query_string 与$args相同; $scheme 所用的协议,比如http或者是https,用法:rewrite ^(.+)scheme://example.com$1 redirect; $server_protocol 请求的协议版本,”HTTP/1.0″或”HTTP/1.1″ $server_addr 服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费) $server_name 请求到达的服务器名 $server_port 请求到达的服务器端口号 $nginx_version nginx 版本号,可修改、隐藏

重写中用到的指令

**if (条件) {} 设定条件,再进行重写
set #设置变量
return #返回状态码
break #跳出rewrite
rewrite #重写**

if位置 放在location下

If 语法格式

If 空格 (条件) {
重写模式
}

条件判断重写

1: “=”来判断相等, 用于字符串比较
2: “~” 用正则来匹配(此处的正则区分大小写)
~* 不区分大小写的正则
3: -f -d -e来判断是否为文件,为目录,是否存在.

例子:

if  ($remote_addr = 192.168.1.100) {return 403;} if ($http_user_agent ~ MSIE) { rewrite ^.*$ /ie.htm; break; #(不break会循环重定向) }if (!-e $document_root$fastcgi_script_name) {rewrite ^.*$ /404.html break;#注, 此处还要加break,}

以 xx.com/dsafsd.html这个不存在页面为例,
我们观察访问日志, 日志中显示的访问路径,依然是GET /dsafsd.html HTTP/1.1
提示: 服务器内部的rewrite和302跳转不一样.
跳转的话URL都变了,变成重新http请求404.html, 而内部rewrite, 上下文没变,
就是说 fastcgi_script_name 仍然是 dsafsd.html,因此 会循环重定向.
set 是设置变量用的, 可以用来达到多条件判断时作标志用.
达到apache下的 rewrite_condition的效果

如下: 判断IE并重写,且不用break; 我们用set变量来达到目的

if ($http_user_agent ~* msie) {                set $isie 1;            }            if ($fastcgi_script_name = ie.html) {                set $isie 0;            }            if ($isie 1) {                rewrite ^.*$ ie.html;            }

Rewrite语法
Rewrite 正则表达式 定向后的位置 模式

Goods-3.html —->Goods.php?goods_id=3
goods-([\d]+).html —> goods.php?goods_id =$1

location /ecshop {index index.php;rewrite goods-([\d]+)\.html$ /ecshop/goods.php?id=$1;rewrite article-([\d]+)\.html$ /ecshop/article.php?id=$1;rewrite category-(\d+)-b(\d+)\.html /ecshop/category.php?id=$1&brand=$2;rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+)\.html /ecshop/category.php?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5;rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d+\.])-(\d+)-([^-]+)-([^-]+)\.html /ecshop/category.php?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5&page=$6&sort=$7&order=$8;}

注意:用url重写时, 正则里如果有”{}”,正则要用双引号包起来

nginx隐藏index.php

location / {  try_files $uri $uri/ /index.php?$query_string;}

nginx防盗链

location  ~*  \.(gif|jpg|png|bmp|swf|flv)$  {     valid_referers none blocked *.ttlsa.com server_names  ~\.google\.  ~\.baidu\.;     if  ($invalid_referer)  {          return  403;          #rewrite ^/ http://www.ttlsa.com/403.jpg;     }}

第一行:gif|jpg|png|bmp|swf|flv
表示对gif|jpg|png|bmp|swf|flv后缀的文件实行防盗链
第二行: 表示对来源地址进行判断,以上所有来至ttlsa.com和域名中包含google和baidu的站点都可以访问到当前站点的图片,如果来源域名不在这个列表中,那么$invalid_referer等于1
if{}里面内容的意思是,如果来路不是指定来源地址就跳转到直接返回403或则rewrite跳转其他页面

0 0
原创粉丝点击