Nginx的rewrite地址重写

一、Rewrite基本概述

  • Rewrite的作用:主要实现URL地址重写,以及地址重定向

  • Rewrite的使用场景:

  1. URL访问跳转:支持开发设计,页面跳转,兼容性支持,展示效果
  2. SEO优化:依赖于URL路径,以便于支持搜索引擎录入
  3. 维护:后台维护,流量转发等
  4. 安全:伪静态
  • Rewrite配置语法:

语法: rewrite regex replacement [flag];

可配置区域: server、location、if

例如:把所有请求都转发给/aaa/bbb.html

rewrite ^(.*)$ /aaa/bbb.html break;

二、Rewrite标记,Flag

(1)flag类型

Flag意义
last如果跳转的资源没有,那么就往下面的location需找资源,如果有就跳转,没有就停止跳转
break如果跳转的资源没有,那么会直接停止跳转
redirect返回302临时重定向,地址栏会显示跳转后的地址,只会在Nginx开启的时候生效
permanent返回301永久重定向,地址栏会显示跳转后的地址,Nginx关闭后也会生效

(2)对于Flag中的break和last

-实验环境

系统主机名ip地址nginx版本
Cetnos7.4rzy192.168.100.202Nginx-1.18.0

-实验步骤

******(1)先做基础配置
[root@Centos7 ~]# hostnamectl set-hostname rzy
[root@Centos7 ~]# su
[root@rzy ~]# systemctl stop firewalld
[root@rzy ~]# setenforce 0
setenforce: SELinux is disabled
[root@rzy ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
mount: /dev/sr0 已经挂载或 /mnt 忙
       /dev/sr0 已经挂载到 /mnt 上

******(2)上传Nginx源码包,进行安装
[root@rzy ~]# yum -y install zlib-devel pcre-devel
。。。。。。
完毕!
[root@rzy ~]# ll
总用量 1020
-rw-------. 1 root root    1264 112 18:27 anaconda-ks.cfg
-rw-r--r--  1 root root 1039530 419 10:03 nginx-1.18.0.tar.gz
[root@rzy ~]# tar xf nginx-1.18.0.tar.gz  -C /usr/src/
[root@rzy ~]# cd /usr/src/nginx-1.18.0/
[root@rzy nginx-1.18.0]# useradd -M -s /sbin/nologin nginx
[root@rzy nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install 
[root@rzy nginx-1.18.0]# ln -s /usr/local/nginx/sbin/nginx  /usr/local/sbin/
[root@rzy nginx-1.18.0]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
[root@rzy nginx-1.18.0]# cd /usr/local/nginx/conf/
[root@rzy conf]# cp nginx.conf nginx.conf.bak
[root@rzy conf]# sed -i '/#/d' nginx.conf
[root@rzy conf]# sed -i '/^$/d' nginx.conf

******(3)修改配置文件
[root@rzy conf]# vim nginx.conf
  1 worker_processes  1;
  2 events {
  3     worker_connections  1024;
  4 }
  5 http {
  6     include       mime.types;
  7     default_type  application/octet-stream;
  8     sendfile        on;
  9     keepalive_timeout  65;
 10     server {
 11         listen       80;
 12         server_name  localhost;
 13         root /aaa/bbb;  #网页目录
 14         location ~ ^/break {  #匹配break资源
 15             rewrite ^/break /ccc/ break; #跳转到/ccc/
 16         }
 17         location ~ ^/last { #匹配last资源
 18             rewrite ^/last /ccc/ last; #跳转到/ccc/
 19         }
 20         location ~ /ccc/ { #匹配/ccc/资源
 21             default_type application/json;
 22             return 200 '{"status":"success"}'; #返回200状态码,内容是{"status":"success"}的页面
 23         }
 24      
 25     }
 26 }
[root@rzy conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@rzy conf]# systemctl start nginx

使用浏览器进行访问
在这里插入图片描述

分别访问这三个location,发现break直接报错,而last和/ccc/可以访问成功,这就是因为在指定的网页目录找不到指定资源的情况下,break会直接停止跳转从而报错404找不到页面,而last会继续往下寻找跳转的资源,所以last可以访问成功

在这里插入图片描述

(2)对比Flag中的redirect和permanent

-实验环境

系统主机名ip地址nginx版本
Cetnos7.4rzy192.168.100.202Nginx-1.18.0

两个类型都可以进行跳转,都是last的检测方式,即找不到资源就往下一个location寻找

-实验步骤

******(1)继续上面的实验,修改配置文件
[root@rzy ~]# vim /usr/local/nginx/conf/nginx.conf
  1 worker_processes  1;
  2 events {
  3     worker_connections  1024;
  4 }
  5 http {
  6     include       mime.types;
  7     default_type  application/octet-stream;
  8     sendfile        on;
  9     keepalive_timeout  65;
 10     server {
 11         listen       80;
 12         server_name  localhost;
 13         root /aaa/bbb;
 14         location ~ ^/aaa {
 15             rewrite ^/aaa /ccc/ redirect;
 16         }
 17         location ~ ^/bbb{
 18             rewrite ^/bbb /ccc/ permanent;
 19         }
 20         location ~ /ccc/ {
 21             default_type application/json;
 22             return 200 '{"status":"success"}';
 23         }
 24 
 25     }
 26 }
[root@rzy ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@rzy ~]# systemctl restart nginx

在Nginx开启的时候,两种类型都可以进行重定向
在这里插入图片描述

关闭Nginx,再次进行访问

在这里插入图片描述

访问aaa无法跳转,访问bbb可以跳转到ccc

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值