nginx-rewrite地址重写

Rewrite使⽤场景

1.URL访问跳转: ⽀持开发设计, ⾯跳转, 兼容性⽀持, 展示效果

2.SEO优化: 依赖于url路径,以便⽀持搜索引擎录⼊

3.维护: 后台维护, 流量转发等

4.安全: 伪静态,

1.Rewrite标记Flag

Flag
last停止rewrite检测-  重新将rewrite后的地址在server标签中执行
break停止rewrite检测-将rewrite后的地址在当前location标签中执行
redirect返回302临时定向值,地址栏会显示跳转后的地址
permanent返回301永久重定向,地址栏会显示跳转后的地址

(1)对比last与break的区别:

    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location ~ ^/break {
                rewrite ^/break /test/ break;    ##访问www.benet.com/break就不再向下访问
        }
        location ~ ^/last {
                rewrite ^/last /test/ last;      ##访问到www.benet.com/last就继续访问
        }
        location ~ /test/{
                default_type application/json;    ##www.benet.com/test
                return 200 '{"status":"success"}';
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

(2)写完之后重启:就会发现last访问成功,但是break访问失败报错403

break:返回404, rewrite后在本location没找到test匹配规则,所以404,符合  "rewrite后不会跳出location 作用域。它的生命也在这个location中终结"

last:返回 test page   符合:rewrite 后会跳出location 作用域,重新开始再走一次刚刚的行为

1.2 对⽐flagredirect permanent

(1)redirect的使用跟permanent很像,但区别在于redirect在关闭服务后就无法进行跳转了

location ~ /ni {
                rewrite ^(.*)$ https://www.bilibili.com/ redirect;
        }
##当输入www.benet.com/ni时就会跳转到bilibli

(2)permanent使用优点在于关闭服务器后,浏览器还会有一定的缓存,在关闭浏览器之后无法访问

location ~ /ni {
                rewrite ^(.*)$ https://www.bilibili.com/ permanent;
        }

2.对nginx的conf文件进行修改使nginx可以向手机端开启手机版的网页

实验需要:在html下创建三个文件:Chrome;Firefox;shouji,分别写一个文件:index.html

(1)在服务端的conf文件写入:

location / {
            if ( $http_user_agent ~ "(Mobile)|(MIDP)|(SAMSUNG)|(iPone)|(ZTE)|(PHILIPS)|(HAIER)|(java)|(curl)|(Android)|(LENOVO)") {     #使用手机访问跳转到mobile文件
            root   html/mobile;
            }
            if ($http_user_agent ~ Firefox) {    #使用火狐访问跳转到firefox文件
                root /usr/local/nginx/html/firefox;
            }
            if ($http_user_agent ~ Chrome) {     #使用谷歌访问跳转到Chrome文件
                root /usr/local/nginx/html/chrome;
            }
           index  index.html index.htm;
        }

(2)重启服务器后在不同浏览器进行访问会访问到不同页面

(3)如何模拟手机访问?

(3.1)按F12进入开发者工具

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

摚张

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值