Nginx 基础模块 rewrite

Rewite 规则作用

Rewrite规则可以实现对url的重写,以及重定向

作用场景:

URL访问跳转,支持开发设计,如页面跳转,兼容性支持,展示效果等
SEO优化
维护:后台维护、流量转发等
安全

rewrite实用场景

Nginx跳转需求的实现方式
使用 rewrite进行匹配跳转
使用if匹配全局变量后跳转
使用 location匹配再跳转
rewrite放在 server{},if{}, location{} 段中
对域名或参数字符串
使用if全局变量匹配
使用 proxy_pass反向代理
在这里插入图片描述

常用正则表达式元字符

Nginx rewrite模块支持正则表达式,正则表达式在之前的文章里有说,心情好可以回头翻一下
在这里插入图片描述

Rewrite

rewrite <regex正则> <replacement跳转后的内容> [flag rewrite所支持的flag标记]

flag标记说明图

在这里插入图片描述

last对比break图在这里插入图片描述

Location

location 常见匹配格式

location = 条件 {} [精准匹配]
ocation 条件 {} [一般匹配 ]
location ~ 条件 {} [正则匹配]

常见表达式
在这里插入图片描述

location优先级

若表达式类型相同,那字多的优先匹配

类型优先级排列如下
= 类型
^= 类型
~ 和 ~* 类型
常规字符串匹配类型
通用匹配 /

匹配某个具体文件 以这个为主
( location = 完整路径)>( location ^~ 完整路径)>( location ~* 完整路径)>( location ~ 完整路径)>( location完整路径)>( location /)

用目录做匹配访问某个文件
( location = 目录)>( location ^~ 目录)>( location ~ 目录)>
( location ~* 目录)>( location 目录)>( location /)

location = / {	##精确匹配 /,主机名后面不能带任何字符串'
    [configuraion A ]	
}

location / {	##所有的地址都以/开头,这条规则将匹配到所有请求,但正则和最长字符串会优先匹配'
    [configuraion B ]
}

location /documents/ {		##匹配任何以/documents/开头的地址,当后面的正则表达式没有匹配到时,才起作用'
    [configuraion C ]
}

location ~ /documents/abc {		##匹配任何以/documents/abc开头的地址,当后面的正则表达式没有匹配到时,才会起作用'
    [configuraion D ]
}

location ^~ /images/ {	##以/images/开头的地址,匹配符合后,停止往下匹配'
    [configuraion E ]
}

location ~*\.(gif|jpg|gpeg)$ {	##匹配所有以 gif, jpg或jpeg结尾的请求, Images/下的图片会被 [configuration E]处理,因为^~的优先级更高'
    [configuraion F ]
}

location /images/abc {	##最长字符匹配到 /images/abc,优先级最低'
    [configuraion G ]
}

location ~ /images/abc {	##以/ Images/abc开头的,优先级次之'
    [configuraion H ]
}

location /images/abc/1.html {	##如果和正则 ~ images/abc/1.htm相比,正则优先级更高'
    [configuraion I ]

rewrite 与 location 区别

相同点

都可以实现跳转

不同点

rewrite是在同一域名内更改获取资源的路径
location是对一类路径做控制访问或反向代理,还可以 proxy_pass到其他机器

rewrite敲在location里,执行顺序

执行 server块里面的 rewrite指令
执行 location匹配
执行选定的 location中的 rewrite指令

来吧!展示!!

nginx在之前我们一直使用的是源码编译安装,这次我们用 大黄狗 安装

[root@5centos ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@5centos ~]# yum -y install nginx

根据域名实现跳转功能

比如公司旧域名www.ora.com突然不想用了,需要使用新域名www.nge.com代替,但是旧域名不能废除,需要跳转到新域名上,而且后面的参数保持不变
安装 DNS 服务
设置两个域名 www.ora.com 和 www.nge.com
DNS安装及配置在之前博客有写:DNS服务配置

[root@5centos named]# echo "nameserver 20.0.0.5" > /etc/resolv.conf
[root@5centos named]# nslookup www.ora.com
Server:		20.0.0.5
Address:	20.0.0.5#53

Name:	www.ora.com
Address: 20.0.0.5

[root@5centos named]# nslookup www.nge.com
Server:		20.0.0.5
Address:	20.0.0.5#53

Name:	www.nge.com
Address: 20.0.0.5

查看 nginx 的配置文件位置

[root@5centos /]# rpm -qc nginx
/etc/logrotate.d/nginx
/etc/nginx/conf.d/default.conf     ##默认主配置文件
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug

对主配置文件进行修改

[root@5centos /]# vim /etc/nginx/conf.d/default.conf
  server {
    listen       80;
    server_name  www.ora.com;
  location / {   ##找到这行
        if ($host = "www.ora.com"){
          rewrite ^/(.*)$ http://www.nge.com/$1 permanent;
        }
        root   /usr/share/nginx/html; ##在这行上面添加
        index  index.html index.html
    }
    ……省略部分……
[root@5centos /]# cd /usr/share/nginx/html/
[root@5centos html]# ls
50x.html  index.html
[root@5centos html]# cp -p index.html index.html.back
[root@5centos html]# vim index.html
<h1>ORA</h1>
~                                 
[root@5centos /]# systemctl start nginx
[root@5centos /]# setenforce 0
[root@5centos /]# iptables -F

在这里插入图片描述这就是rewrite 和 permanent 的功能

根据客户端 IP 访问进行跳转

在公司网站初上线时,有时候需要让用户访问一个界面,让开发人员访问正常上线界面

[root@5centos html]# vim /etc/nginx/conf.d/default.conf 
server {
    listen       80;
    server_name  www.ora.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
    set $rewrite false;     ##设置变量为 false
    if ($remote_addr = "20.0.0.5"){   ##如果访问IP 为本机 IP
                set $rewrite ture;    ##设置变量为 ture
        }
    if ($rewrite = false){            ##若上面一条不成立的情况,如果变量还为 false 
                rewrite (.+) /fangwen.html;
        }
    location = /fangwen.html {        ##匹配上一条的标记
                root /usr/share/nginx/html;   ##目录位置
                index  fangwen.html;          ##主页名称
        }
    location / {                      ##在 变量为 ture 正常匹配
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

使用本机进行访问测试,页面正常

在这里插入图片描述使用客户机进行访问
在这里插入图片描述

根据域名跳转并添加网页路径

访问 www.nge.com/post ,自动跳转到 www.ora.com/bbs/post

[root@5centos post]# vim index.html
[root@5centos post]# pwd
/usr/share/nginx/html/bbs/post
[root@5centos post]# 
server {
    listen       80;
    server_name  www.nge.com;

    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;
    location /post {
        rewrite (.+) http://www.ora.com/bbs$1 permanent;
        }
        location / {

        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

在这里插入图片描述

根据参数匹配的跳转

访问 www.ngecom/500-100或200-50,自动跳转到ora网站主页

[root@5centos /]# vim /etc/nginx/conf.d/default.conf  
server {
    listen       80;
    server_name  www.nge.com;

    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;
    if ($request_uri ~ ^/500-(100|200)-(\d+).html$){
        rewrite (.*) http://www.ora.com permanent;
        }
[root@5centos /]# systemctl restart nginx.service

在这里插入图片描述

访问 PHP 跳转到首页

访问任何 nge站点下的 upload 目录里的 PHP 文件,都访问到ora主页

server {
    listen       80;
    server_name  www.nge.com;

    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;
        local ~* /upload/.*\.php$ {
                rewrite (.+) http://www.ora.com permanent;
        }

普通 URL 请求跳转

访问 www.nge.com/1/test.com,跳转到 www.ora.com

server {
    listen       80;
    server_name  www.nge.com;

    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;
        location ~* ^/1/test.html {
        rewrite (.+) http://www.ora.com permanent;
        }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值