Nginx地址重定向 return rewrite if

return指令
该指令用于结束规则的执行并返回状态码给客户端。
示例:如果访问的URL以".sh"或".bash"结尾,则返回403状态码
location ~ .*.(sh|bash)?$
{
return 403;
}

rewrite 指令
使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向

rewrite 语法格式
rewrite regex(正则) replacement flag (rewrite 源地址 目标地址)
flag: break last redirect permanent
last: last一般写在server和if中 停止执行其他重写规则,根据URI继续搜索其他location
break:break一般使用在location url重写后,直接使用当前资源,不再执行location里余下的语句,完成本次请求
redirect:302临时重定向,地址栏改变,爬虫不更新URI
permanent:301永久重定向。地址栏改变,爬虫更新URI

总结:
使用last会对server标签重新发起请求,而break就直接使用当前的location中的数据源来访问,要视情况加以使用。一般在非根的location中配置rewrite,都是用的break;而根的location使用last比较好,因为如果配置了fastcgi或代理访问jsp文件的话,在根location下用break是访问不到。测试到rewrite有问题的时候,也不妨把这两者换换试试。

正则表达式匹配模式如下(操作符 模式匹配):
区分大小写匹配: ~
不区分大小写匹配: ~*
区分大小写不匹配 : !~
不区分大小写不匹配 : !~*

location表达式类型
~* 表示执行一个正则匹配,不区分大小写
^~ 表示普通字符匹配。使用前缀匹配。如果匹配成功,则不再匹配其他location。
= 进行普通字符精确匹配。也就是完全匹配。
@ 它定义一个命名的 location,使用在内部定向时,例如 error_page, try_files
@例
server {
listen 9090;
server_name localhost;
location / {
root html;
index index.html index.htm;
allow all;
}
#error_page 404 http://www.baidu.com # 直接这样是不允许的
error_page 404 = @fallback;
location @fallback {
proxy_pass http://www.baidu.com;
}
}
上述配置文件的意思是:如果请求的 URI 存在,则本 nginx 返回对应的页面;如果不存在,则把请求代理到baidu.com 上去做个弥补(注: nginx 当发现 URI 对应的页面不存在, HTTP_StatusCode 会是 404 ,此时error_page 404 指令能捕获它)。

location匹配优先级
精确匹配,只对匹配的目录或文件生效,对目录下的子文件不生效: = 优先级最高
不使用正则表达式匹配: ^~ 优先级第二
正则匹配第三 ~
路径匹配第四
默认匹配第五 location /

rewirte if 语句
if 语句最好放在location中使用
格式: if (判断条件) {
}

双目测试:
匹配为真
!~ 不匹配为真
= 等于为真
!= 不等于为真
~* 不区分大小写,匹配为真
!~* 不区分大小写,不匹配为真

基于首页文件跳转
server {
listen 80;
server_name www.aaa.com;
location / {
index index.html index.htm;
rewrite /index.html /b.html break; #/index.html现有网站首页 /b.html 跳转到的网页
}

基于域名的地址重定向
输入aaa.com跳转到www.aaa.com
server {
listen 80;
server_name 9qifu.com;
rewrite ^/(.*)$ http://www.aaa.com/$1 permanent;
access_log off;
}

根据客户端浏览器进行重定向
server {
listen 80;
server_name www.aaa.com;
location / {
root html;
index index.html index.htm;
if ($http_user_agent ~ Firefox) { # h t t p u s e r a g e n t   r e w r i t e ( . ) http_user_agent是客户端浏览器信息 ~ 匹配为真 rewrite ^(.*) /firefox/$1 break; #($1引用的变量是(.*) 必须要写break,不然死循环)
}
}
}
mkdir /application/nginx/html/firefox
vim /application/nginx/html/index.html
tail -f /application/nginx/logs/access.log #查看访问nginx信息其中有客户端浏览器信息

手机和pc端的分离
location / {
if ( h t t p u s e r a g e n t   " a n d r o i d " ) p r o x y p a s s h t t p : / / a n d i r o d ; i f ( http_user_agent ~* "android") { proxy_pass http://andirod; } if ( http_user_agent ~* “iphone”)
{
proxy_pass http://iphone;
}
proxy_pass http://pc;
}

改变音乐文件存储路径
server {
listen 80;
server_name www.aaa.com;
location / {
rewrite ^/(download/.)/media/(.)…*$ /$1/mp3/$2.mp3 break;
}
}

当访问地址不存在时地址重定向
server {
listen 80;
server_name www.aaa.com;
location / {
root html
index index.html index.htm;
if (!-e $request_filename){ #如果请求文件不存在
rewrite ^/ http://www.aaa.com/none.html
}
}
}

方法2
在location外面加入
server {
listen 80;
server_name www.aaa.com;
location / {
root html
index index.html index.htm;
}
error_page 403 /403.html; #跳转到指定页面
error_page 403 http://baidu.com; #跳转到指定页面
}

nginx限制http请求方法
r e q u e s t m e t h o d i f ( request_method 请求方法 if ( request_method !~ ^(GET|HEAD|POST)$) {
return 501
}

猜你喜欢

转载自blog.csdn.net/bjgaocp/article/details/87867925
今日推荐