NGINX_十二 nginx 地址重写 rewrite

news2024/10/5 23:30:28

十二 nginx 地址重写 rewrite

1 什么是Rewrite

Rewrite对称URL Rewrite,即URL重写,就是把传入Web的请求重定向到其他URL的过程。
  • URL Rewrite最常见的应用是URL伪静态化,是将动态页面显示为静态页面方式的一种技术。比如
    http://www.123.com/news/index.php?id=123 使用URLRewrite 转换后可以显示为 http://www.123 .com/news/123.html对于追求完美主义的网站设计师,就算是网页的地址也希望看起来尽量简洁明快。
    理论上,搜索引擎更喜欢静态页面形式的网页,搜索引擎对静态页面的评分一般要高于动态页面。所以,UrlRewrite可以让我们网站的网页更容易被搜索引擎所收录。
  • 从安全角度上讲,如果在URL中暴露太多的参数,无疑会造成一定量的信息泄漏,可能会被一些黑客
    利用,对你的系统造成一定的破坏,所以静态化的URL地址可以给我们带来更高的安全性。
  • 实现网站地址跳转,例如用户访问360buy.com,将其跳转到jd.com。例如当用户访问tianyun.com的
    80端口时,将其跳转到443端口。

2 Rewrite 相关指令

  • Nginx Rewrite 相关指令有 if 、rewrite、set、return

2.1、if 语句

应用环境

server,location

语法:

if (condition) {}
if 可以支持如下条件判断匹配符号
~ 					正则匹配 (区分大小写)
~* 				    正则匹配 (不区分大小写)
!~                  正则不匹配 (区分大小写)
!~*		            正则不匹配  (不区分大小写)
-f!-f 		    用来判断是否存在文件
-d!-d 		    用来判断是否存在目录
-e!-e 		    用来判断是否存在文件或目录
-x!-x 		    用来判断文件是否可执行

在匹配过程中可以引用一些Nginx的全局变量
$args				请求中的参数;
$document_root	    针对当前请求的根路径设置值;
$host				请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名;
$limit_rate			对连接速率的限制;
$request_method		请求的方法,比如"GET""POST";
$remote_addr		客户端地址;
$remote_port		客户端端口号;
$remote_user		客户端用户名,认证用;
$request_filename   当前请求的文件路径名(带网站的主目录/usr/local/nginx/html/images/a.jpg)
$request_uri		当前请求的文件路径名(不带网站的主目录/images/a.jpg)
$query_string$args相同;
$scheme				用的协议,比如http或者是https
$server_protocol	请求的协议版本,"HTTP/1.0""HTTP/1.1";
$server_addr 		服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费);
$server_name		请求到达的服务器名;
$document_uri$uri一样,URI地址;
$server_port 		请求到达的服务器端口号;

2.2、Rewrite flag

rewrite 指令根据表达式来重定向URI,或者修改字符串。

​ 可以应用于server,location, if环境下每行rewrite指令最后跟一个flag标记,支持的flag标记有:

last 			    相当于Apache里的[L]标记,表示完成rewrite。默认为last。
break 				本条规则匹配完成后,终止匹配,不再匹配后面的规则
redirect 			返回302临时重定向,浏览器地址会显示跳转后的URL地址
permanent 		    返回301永久重定向,浏览器地址会显示跳转后URL地址

​ redirect 和 permanent 区别则是返回的不同方式的重定向,对于客户端来说一般状态下是没有区别的。而对于搜索引擎,相对来说301的重定向更加友好,如果我们把一个地址采用301跳转方式跳转的话,搜索引擎会把老地址的相关信息带到新地址,同时在搜索引擎索引库中彻底废弃掉原先的老地址。使用302重定向时,搜索引擎(特别是google)有时会查看跳转前后哪个网址更直观,然后决定显示哪个,如果它觉的跳转前的URL更好的话,也许地址栏不会更改,那么很有可能出现URL劫持的现像。在做URI重写时,有时会发现URI中含有相关参数,如果需要将这些参数保存下来,并且在重写过程中重新引用,可以用到 () 和 $N 的方式来解决。

2.3、Rewrite匹配参考示例

# 本地解析host文件
例1:
# http://www.testpm.com/a/1.html ==> http://www.testpm.com/b/2.html
		
    location /a {
        root    /html;
        index   1.html index.htm;
        rewrite .* /b/2.html permanent;
        }
    location /b {
        root    /html;
        index   2.html index.htm;
        }
例2:
# http://www.testpm.com/2019/a/1.html ==> http://www.testpm.com/2018/a/1.html

     location /2019/a {
        root    /var/www/html;
        index   1.html index.hml;
        rewrite ^/2019/(.*)$ /2018/$1 permanent;
        }
     location /2018/a {
        root    /var/www/html;
        index   1.html index.htl;
        }
例3:
# http://www.lanfw1.com/a/1.html ==> http://jd.com

location /a {
        root    /html;
        if ($host ~* www.qf.com ) {
        rewrite .* http://jd.com permanent;
        }
        }
例4:
# http://www.lanfw1.com/a/1.html ==> http://jd.com/a/1.html
location /a {
        root /html;
        if ( $host ~* testpm.com ){
        rewrite .* http://jd.com$request_uri permanent;
        }
        }
例5: 在访问目录后添加/  (如果目录后已有/,则不加/)
# http://www.lanfw1.com/a/b/c
# $1: /a/b/
# $2: c
# http://$host$1$2$3/
location /a/b/c {
        if (-d $request_filename) {
        rewrite ^(.*)([^/])$ http://$host$1$2/ permanent;
        }
        }
例6:
# http://www.lanfw.com/login/lanfw.html ==>  http://www.lanfw.com/reg/login.html?user=lanfw

	location /login {
        root   /usr/share/nginx/html;
        rewrite ^/login/(.*)\.html$ http://$host/reg/login.html?user=$1;
        }
    location /reg {
        root /usr/share/nginx/html;
        index login.html;

        }
例7:
#http://www.lanfw.com/qf/11-22-33/1.html  ==>  http://www.lanfw.com/qf/11/22/33/1.html
location /qf {
            rewrite ^/qf/([0-9]+)-([0-9]+)-([0-9]+)(.*)$ /qf/$1/$2/$3$4 permanent;
        }

        location /qf/11/22/33 {
                root /html;
                index   1.html;
        }

2.4、set 指令

set 指令是用于定义一个变量,并且赋值

应用环境:

server,location,if

应用示例

例8:
#http://alice.testpm.com ==> http://www.testpm.com/alice
#http://jack.testpm.com ==> http://www.testpm.com/jack

[root@nginx-server conf.d]# cd /usr/share/nginx/html/
[root@nginx-server html]# mkdir jack alice
[root@nginx-server html]# echo "jack.." >> jack/index.html
[root@nginx-server html]# echo "alice.." >> alice/index.html

a. DNS实现泛解析
*   		IN      A			    网站IP
# 或者本地解析域名host文件
10.0.105.202 www.testpm.com
10.0.105.202 alice.testpm.com
10.0.105.202 jack.testpm.com

# 编辑配置文件:
server {
    listen       80;
    server_name  www.testpm.com;

    location / {
         root   /usr/share/nginx/html;
         index  index.html index.htm;
         if ( $host ~* ^www.testpm.com$) {
                break;
                }
         if ( $host ~* "^(.*)\.testpm\.com$" ) {
                set $user $1;
                rewrite .* http://www.testpm.com/$user permanent;
                }
        }
    location /jack {
         root /usr/share/nginx/html;
         index  index.html index.hml;
        }
    location /alice {
         root /usr/share/nginx/html;
         index index.html index.hml;
        }
}

2.5、return 指令

return 指令用于返回状态码给客户端

server,location,if

应用示例:

例9:如果访问的.sh结尾的文件则返回403操作拒绝错误
server {
    listen       80;
    server_name  www.testpm.cn;
    #access_log  /var/log/nginx/http_access.log  main;

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

    location ~* \.sh$ {
        return 403;
        }
}
例10:
<==80 == 443 == 80转443端口 ==> 
server {
    listen       80;
    server_name  www.testpm.cn;
    access_log  /var/log/nginx/http_access.log  main;
    return 301 https://www.testpm.cn$request_uri;
}

server {
    listen 443 ssl;
    server_name www.testpm.cn;
    access_log  /var/log/nginx/https_access.log  main;
    #ssl on;
    ssl_certificate   /etc/nginx/cert/2447549_www.testpm.cn.pem;
    ssl_certificate_key  /etc/nginx/cert/2447549_www.testpm.cn.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    ssl_prefer_server_ciphers on;

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

[root@nginx-server ~]# curl -I http://www.testpm.cn
HTTP/1.1 301 Moved Permanently
Server: nginx/1.16.0
Date: Wed, 03 Jul 2019 13:52:30 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: https://www.testpm.cn/

3 last break详解

d17a4709c5bfc4e1cc4c8fa2d6ee005a2435d49f

[root@localhost test]# cat /etc/nginx/conf.d/last_break.conf 
server {
    listen       80;
    server_name  localhost;
    access_log  /var/log/nginx/last.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    location /break/ {
        root /usr/share/nginx/html;
        rewrite .* /test/break.html break;
    }
    location /last/ {
        root /usr/share/nginx/html;
        rewrite .* /test/last.html last;
    }
    location /test/ {
        root /usr/share/nginx/html;
        rewrite .* /test/test.html break;
    }

}
[root@localhost conf.d]# cd /usr/share/nginx/html/
[root@localhost html]# mkdir test
[root@localhost html]# echo "last" > test/last.html
[root@localhost html]# echo "break" > test/break.html
[root@localhost html]# echo "test" > test/test.html

http://10.0.105.196/break/break.html
http://10.0.105.196/last/last.html

注意:

  • last 标记在本条 rewrite 规则执行完后,会对其所在的 server { … } 标签重新发起请求;
  • break 标记则在本条规则匹配完成后,停止匹配,不再做后续的匹配;
  • 使用 alias 指令时,必须使用 last;
  • 使用 proxy_pass 指令时,则必须使用break。

4 Nginx 的 https ( rewrite )

server {
        listen       80;
        server_name  *.vip9999.top vip9999.top;

        if ($host ~* "^www.vip9999.top$|^vip9999.top$" ) {
                return 301 https://www.vip9999.top$request_uri;
        }

        if ($host ~* "^(.*).vip9999.top$" ) {
                set $user $1;
                return 301 https://www.vip9999.top/$user;
        }

    }

    # Settings for a TLS enabled server.
    server {
        listen       443 ssl;
        server_name  www.vip9999.top;

        location / {
                root      /usr/share/nginx/html;
                index     index.php index.html;
        }

        #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            root           /usr/share/nginx/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
        ssl on;
        ssl_certificate cert/214025315060640.pem;
        ssl_certificate_key cert/214025315060640.key;
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;
        }

5 Apache 的 https ( rewrite )

[root@localhost ~]# yum -y install httpd mod_ssl
[root@localhost ~]# vim /etc/httpd/conf.d/vip9999.conf

1561898727918

例4

image-20240619201432502

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1842300.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

odoo 入库发票三大凭证

注意对应科目 库存计价凭证 原材料 成品 供应商账单凭证 银行账单凭证

分布式系列之限流组件

概述 在高并发场景下&#xff0c;请求量瞬间到达&#xff0c;后端服务器即使有缓存、集群主备、分库分表、容错降级等措施&#xff0c;也有可能扛不住这请求量&#xff0c;因此可考虑引入限流组件。限流的目的&#xff1a;防止恶意请求流量或流量超出系统承载。 应用场景&…

1.搭建SpringBoot项目三种方式

目录 1.使用Spring Initializr 1.1访问Spring Initializr: 1.2填写项目基本信息 1.3配置项目元数据: 1.4添加依赖: 1.5生成项目: 1.6下载项目: 1.7解压项目: 1.8导入项目到IDE: 1.9运行项目: 1.10创建控制器: 1.11访问应用 2.使用IDE&#xff08;集成开发环境&…

秒杀技术方案

参考文档&#xff1a; 面试必考&#xff1a;秒杀系统要如何设计&#xff1f;

gorm 一对多

type Author struct {AID int gorm:"primary_key;AUTO_INCREMENT"Name stringAge stringSex string//关联关系Article []Article gorm:"ForeignKey:Auid;AssociationForeignKey:AID" } type Article struct {ArId int gorm:"primary_key;AUTO_I…

MySQL Server和Server启动程序(二)

mysql.server — MySQL Server启动脚本 Unix和类Unix系统上的MySQL发行版包括一个名为MySQL.server的脚本&#xff0c;该脚本使用mysqld_safe启动MySQL服务器。它可以在Linux和Solaris等系统上使用&#xff0c;这些系统使用System V风格的运行目录来启动和停止系统服务。它也被…

基础算法---滑动窗口

文章目录 什么是滑动窗口1.长度最小的子数组2.无重复字符的最长子串3.最大连续1的个数4.将x减到0的最小操作数5.最小覆盖子串总结 什么是滑动窗口 滑动窗口&#xff08;Sliding Window&#xff09;是一种在计算机科学中用于解决各种子数组或子字符串问题的技术。滑动窗口技术通…

如何建立私域流量?私域流量怎么运营,一文读懂

当全网都在讨论私域流量&#xff0c;你是不是也有很多问号呢&#xff1f; 互联网高速发达&#xff0c;消费形式日新月异&#xff0c;跟不上时代就会被时代淘汰&#xff0c;接下来&#xff0c;我们就从3个层面深度讨论下私域流量究竟是什么&#xff1f;为什么要玩转私域流量&am…

详细解析Ubuntu22 部署Kaldi大模型

一、下载Kaldi模型 下载地址&#xff1a;GitHub - kaldi-asr/kaldi: kaldi-asr/kaldi is the official location of the Kaldi project. 下载文件解释&#xff1a; 一般常用的是src、tools和egs包 src&#xff08;源代码&#xff09;包&#xff1a; 包含Kaldi的核心源代码&…

Flutter调用本地web

前言: 在目前Flutter 环境中&#xff0c;使用在线 webview 是一种很常见的行为 而在 app 环境中&#xff0c;离线使用则更有必要 1.环境准备 将依赖导入 2.引入前端代码 前端代码有两种情况 一种是使用打包工具 build 而来的前端代码 另一种情况是直接使用 HTML 文件 …

Quantlab整合Alpha158因子集,为机器学习大类资产配置策略做准备(代码+数据)

原创文章第565篇&#xff0c;专注“AI量化投资、世界运行的规律、个人成长与财富自由"。 我们的研报得现工作&#xff0c;用了两篇文章讲数据准备&#xff1a; 【研报复现】年化16.19%&#xff0c;人工智能多因子大类资产配置策略 【研报复现】年化27.1%&#xff0c;人…

vmware workstation下centos7屏幕切换及大小调整

虚拟机版本&#xff1a;vmware workstation15.5.2 操作系统版本&#xff1a;centos 7.9.2009 一 图形界面和命令行界面切换方法 在CentOS 7中&#xff0c;可以使用以下方法切换界面&#xff1a; 1 使用快捷键切换&#xff1a;按下Ctrl Alt F2&#xff08;或F3&#xff0…

0.5 逐行扫描(Progressive scan)简介

0.5 逐行扫描简介 逐行扫描&#xff08;Progressive scan&#xff09;是一种将图像显示在扫描式的显示设备上的方法。 逐行扫描常被用在计算机显示器上。 逐行扫描按照从左到右&#xff0c;从上到下的顺序扫描图像的所有行。如下图&#xff1a; 下图粗略的将逐行扫描与隔行…

LORA、UNB无线网关物联网锁助力人才公寓智慧化管理

吸引和留住青年人才是城市持续发展的关键&#xff0c;解决青年人才住房问题又是其中重要一环。“人才跟着产业走、公寓跟着人才建”已成为全国各地新建及改造人才公寓的目标&#xff0c;“引才聚才”离不开人才公寓行业布局与发展。 人才公寓不同于普通的长短租公寓&#xff0c…

[HGAME 2022 week1]Matryoshka(古典密码混合)

题目&#xff1a; 直接说方法&#xff1a; 首先这是一段盲文&#xff0c;要先将盲文反转&#xff0c; 然后再用摩斯密码转换 将得到的字符串去掉“,”后&#xff0c;base16解码 在尝试维吉尼亚密码 再用base64解码 然后用凯撒密码 最后栅栏密码&#xff08;22栏&#xff09;

专家观点∣企企通采购供应链数字化总监于海生:如何利用数字化技术重构采购流程,推动企业降本增效?

摘要 数字化转型现已成为企业提升竞争力、实现降本增效的必由之路。企业应主动参与到数字经济的建设中&#xff0c;以数据资源为关键要素&#xff0c;以现代信息网络为主要载体&#xff0c;以信息通信技术的有效使用作为效率提升和经济结构优化的重要推动力的一系列经济活动&a…

基于STC12C5A60S2系列1T 8051单片机接收串口调试助手发送的固定长度字符串控制单片机的功能

基于STC12C5A60S2系列1T 8051单片机接收串口调试助手发送的固定长度字符串控制单片机的功能 STC12C5A60S2系列1T 8051单片机管脚图STC12C5A60S2系列1T 8051单片机串口通信介绍STC12C5A60S2系列1T 8051单片机串口通信的结构基于STC12C5A60S2系列1T 8051单片机串口通信的特殊功能…

广州化工厂可燃气体报警器检定检验:安全生产新举措显成效

随着科技的不断发展&#xff0c;可燃气体报警器的检定检验技术也在不断进步。 广州的一些化工厂开始采用先进的智能检测系统和数据分析技术&#xff0c;对报警器的性能进行更加精准和全面的评估。 这些新技术不仅能够提高检定检验的效率和准确性&#xff0c;还能够为化工厂的…

第11章 测试代码

第11章 测试代码 11.1 测试函数11.1.1 单元测试和测试用例11.1.2 可通过的测试11.1.3 未通过的测试11.1.4 测试未通过时怎么办11.1.5 添加新测试 11.2 测试类11.2.1 各种断言方法11.2.2 一个要测试的类11.2.3 测试 AnonymousSurvey 类11.2.4 11.1 测试函数 11.1.1 单元测试和测…

Rancher注册已有k8s集群

Rancher安装后注册K8s集群操作 1.Rancher安装 编辑docker—compose文件 version: 3.8services:rancher:image: registry.cn-hangzhou.aliyuncs.com/rancher-images/rancher:v2.8.5container_name: rancherprivileged: truerestart: unless-stoppedports:- "18080:80&qu…