温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

nginx带参数跳转

发布时间:2020-02-27 15:25:55 来源:网络 阅读:1060 作者:doubf 栏目:系统运维

原链接:https://www.baidu.com/benefit_detail?slug=bankofchina-20170320
目标链接:https://www.test.cn/boc 
location ~ /benefit_detail {
        if ($args ~* "slug=bankofchina-20170320") {
            rewrite ^/benefit_detail /boc? permanent;
        }
        try_files $uri $uri/ /index.php?$query_string;
        }
常见跳转事例:
1,将www.myweb.com/connect 跳转到connect.myweb.com

rewrite ^/connect$ http://connect.myweb.com permanent;

rewrite ^/connect/(.*)$ http://connect.myweb.com/$1 permanent;

2,将connect.myweb.com 301跳转到www.myweb.com/connect/ 

if ($host = "connect.myweb.com"){

rewrite ^/(.*)$ http://www.myweb.com/connect/$1 permanent;

    }

3,myweb.com 跳转到www.myweb.com

if ($host != 'www.myweb.com' ) { 

rewrite ^/(.*)$ http://www.myweb.com/$1 permanent; 

    }

4,www.myweb.com/category/123.html 跳转为 category/?cd=123

rewrite "/category/(.*).html$" /category/?cd=$1 last;

5,www.myweb.com/admin/ 下跳转为www.myweb.com/admin/index.php?s=

if (!-e $request_filename){

rewrite ^/admin/(.*)$ /admin/index.php?s=/$1 last;

    }

6,在后面添加/index.php?s=

if (!-e $request_filename){

    rewrite ^/(.*)$ /index.php?s=/$1 last;

    }

7,www.myweb.com/xinwen/123.html  等xinwen下面数字+html的链接跳转为404

rewrite ^/xinwen/([0-9]+)\.html$ /404.html last;

8,http://www.myweb.com/news/radaier.html 301跳转 http://www.myweb.com/strategy/

rewrite ^/news/radaier.html http://www.myweb.com/strategy/ permanent;

9,重定向 链接为404页面

rewrite http://www.myweb.com/123/456.php /404.html last;

10, 禁止htaccess

location ~//.ht {

         deny all;

     }

11, 可以禁止/data/下多级目录下.log.txt等请求;

location ~ ^/data {

     deny all;

     }

12, 禁止单个文件

location ~ /www/log/123.log {

      deny all;

     }

13, http://www.myweb.com/news/activies/2014-08-26/123.html 跳转为 http://www.myweb.com/news/activies/123.html

rewrite ^/news/activies/2014\-([0-9]+)\-([0-9]+)/(.*)$ http://www.myweb.com/news/activies/$3 permanent;

14,nginx多条件重定向rewrite

如果需要打开带有play的链接就跳转到play,不过/admin/play这个不能跳转

        if ($request_filename ~ (.*)/play){ set $payvar '1';}
        if ($request_filename ~ (.*)/admin){ set $payvar '0';}
        if ($payvar ~ '1'){
                rewrite ^/ http://play.myweb.com/ break;
        }

15,http://www.myweb.com/?gid=6 跳转为http://www.myweb.com/123.html

 if ($request_uri ~ "/\?gid\=6"){return  http://www.myweb.com/123.html;}

正则表达式匹配,其中:

* ~ 为区分大小写匹配

* ~* 为不区分大小写匹配

* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配

文件及目录匹配,其中:

* -f和!-f用来判断是否存在文件

* -d和!-d用来判断是否存在目录

* -e和!-e用来判断是否存在文件或目录

* -x和!-x用来判断文件是否可执行

flag标记有:

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

* break 终止匹配, 不再匹配后面的规则

* redirect 返回302临时重定向 地址栏会显示跳转后的地址

* permanent 返回301永久重定向 地址栏会显示跳转后的地址
nginx各个内置变量含义请参考:
https://blog.csdn.net/wanglei_storage/article/details/66004933
【nginx try_files的理解】
以 try_files $uri $uri/ /index.php; 为例,当用户请求 http://servers.blog.ustc.edu.cn/example 时,
这里的 $uri 就是 /example。try_files 会到硬盘里尝试找这个文件。如果存在名为 /$root/example(其中 $root 是 WordPress 
的安装目录)的文件,就直接把这个文件的内容发送给用户。显然,目录中没有叫 example 的
文件。然后就看 $uri/,增加了一个 /,也就是看有没有名为 /$root/example/ 的目录。又找不到,
就会 fall back 到 try_files 的最后一个选项 /index.php,发起一个内部 “子请求”,也就是相当于
nginx 发起一个 HTTP 请求到 http://servers.blog.ustc.edu.cn/index.php。这个请求会被 location
~ \.php$ { ... } catch 住,也就是进入 FastCGI 的处理程序。而具体的 URI 及参数是在 REQUEST_URI 
中传递给 FastCGI 和 WordPress 程序的,因此不受 URI 变化的影响
[$request_uri解释]
$request_uri就是完整url中刨去最前面$host剩下的部分,比如http://www.baidu.com/pan/beta/test1?fid=3这个url,
去掉www.baidu.com剩下的就是了,日志里会看到打印出来的$request_uri其实是/pan/beta/test1?fid=3。
如果只访问www.baidu.com,$request_uri里也会有个/的。

if ($request_uri ~* "^/$") 表示url中只有域名,后面不跟任何东西,比如www.baidu.com。
if ($request_uri ~* "test") 表示域名后面那串儿只要包含test这个关键词,就可匹配成功,
比如www.baidu.com/pan/beta/test3

if ($request_uri ~* "^/$"){
               rewrite ^ http://kj.fnwtv.com/index.html permanent;
           }

if ($request_uri !~* "^/$") {
                rewrite ^ http://www.fnwtv.com/ permanent;
            }
【Nginx if 条件判断参考】
https://www.cnblogs.com/saneri/p/6257188.html
location proxy_pass 后面的url 加与不加/的区别参考:
https://blog.51cto.com/huangzp/1954575
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI