小阿轩yx-Nginx Rewrite

2024-06-20 00:44
文章标签 nginx rewrite 小阿轩 yx

本文主要是介绍小阿轩yx-Nginx Rewrite,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

小阿轩yx-Nginx Rewrite

Nginx Rewrite 概述

  • 现在 Nginx 已经成为很多公司作为前端反向代理服务器的首选

实际工作中会遇到很多跳转(重写 URL)的需求

  • 更换域名后需要保持旧的域名能跳转到新的域名上
  • 某网页发生改变需要跳转到新的页面
  • 网站防盗链等等需求

后端使用的Nginx 服务器与Apache 服务器

区别

  • Apache 服务器虽然也能做跳转,规则库也很强大
  • 用 Nginx 跳转效率会更高(注:更有优势)

Rewrite 跳转场景包括

  • 可以调整用户浏览的 URL,看起来更规范,合乎开发及产品人员的需求。
  • 为了让搜索引擎搜录网站内容及用户体验更好,企业会将动态URL地址伪装成静态地址提供服务。
  • 网址换新域名后,让旧的访问跳转到新的域名上。例如,访问京东的 360buy.com 会跳转到 jd.com。
  • 根据特殊变量、目录、客户端的信息进行 URL调整等。

Rewrite 跳转实现

Nginx 是通过 ngx_http_rewrite_module 模块

  • 支持 url 重写
  • 支持if条件判断
  • 另外该模块需要 PCRE 支持
  • 应在编译 Nginx 时指定 PCRE 支持,默认已经安装

缺点

  • 不支持 else

根据相关变量重定向和选择不同的配置

从一个location 跳转到另一个location

  • 不过这样的循环最多可以执行 10 次
  • 超过后 Nginx 将返回 500 错误

同时,重写模块包含 set 指令,来创建新的变量并设其值,这在有些情景下非常有用的,如记录条件标识、传递参数到其他location、记录做了什么等等。

rewrite 功能就是使用 Nginx 提供的全局变量或自己设置的变量,结合正则表达式和标志位实现 ur 重写以及重定向。

Rewrite 实际应用场景

实际工作的应用中,Nginx跳转需求有三种方式可实现

  • 可以直接用rewrite 进行匹配跳转
  • 也可以使用if匹配全局变量后跳转
  • 另外,还可以使用location 匹配再跳转

所以rewrite 只能放在 server{}、f、location{}配置段中

Nginx 正则表达式

字符描述
^匹配输入字符串的起始位置
$匹配输入字符串的结束位置
 
*匹配前面的字符零次或多次。如“ol*”能匹配“o”及“ol”
+“ol!”匹配前面的字符一次或多次。如“ol+”能匹配“o!"及“ol!”、“ol!”,但不能匹配“o”
?匹配前面的字符零次或一次,例如“do(es)?"能匹配“do"或者“does”,"?”等效于”{0,1}”
.匹配除"\n”之外的任何单个字符,若要匹配包括“n"在内的任意字符,请使用诸如“[.\n]”之类的模式
\将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用。如“\n"匹配一个换行符,而“\$”则匹配“$”
\d匹配纯数字
{n}重复n次
{n,}重复n次或更多次
[c]匹配单个字符c
[a-z]匹配 a-z 小写字母的任意一个
[a-zA-Z]匹配 a-z 小写字母或 A-Z 大写字母的任意一个

Nginx Rewrite 基本操作

Rewrite 语法

  • regex:表示正则匹配规则
  • replacement:表示跳转后的内容
  • flag:表示 rewrite 支持的 flag 标记
rewrite<regex><replacement>[flag];

flag 标记说明

  • last:相当于 Apache 的[L]标记,表示完成 rewrite
  • break:本条规则匹配完成即终止,不再匹配后面的任何规则
  • redirect:返回 302临时重定向,浏览器地址会显示跳转后的 URL地址,爬虫不会更新url(因为是临时)
  • permanent:返回 301永久重定向,浏览器地址栏会显示跳转后的 URL地址,爬虫更新 url

(注:如果后面不跟 fag 标记,那么默认是 302 临时重定向

实际工作场景中,还有另一种 return 指定。因为 301 和 302不能简单的只返回状态码,还必须有重定向的 URL,这就是 return 指令无法返回 301 和 302 的原因)

last 和 break 区别

  • last 一般写在 server 和if中,而 break 一般使用在 location 中。
  • last 不终止重写后的 ur 匹配,即新的 ur 会再从 server 走一遍匹配流程,而 break 终止重写后的匹配

Location 分类

location 大致可以分为三类

  • location = patt {} [精准匹配]

  • location patt {} [一般匹配]

  • location ~ patt {} [正则匹配]

(注:精准匹配和一般匹配不需要做详细的说明,主要是正则匹配)

[root@localhost ~]# mkdir -p /var/www/html/aaa
[root@localhost ~]# mkdir -p /var/www/html/bbb
[root@localhost ~]# mkdir -p /var/www/html/ccc[root@localhost ~]# echo "aaa">/var/www/html/aaa/aaa.html
[root@localhost ~]# echo "bbb">/var/www/html/bbb/index.html
[root@localhost ~]# echo "ccc">/var/www/html/ccc/INDEX.html
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conflocation / {root   /var/www/html/aaa;index  index.html aaa.html;}location = /index.html {root   /var/www/html/bbb;index  index.html index.htm;}location ~ /INDEX.html {root   /var/www/html/ccc;index  index.html index.htm INDEX.html;}

(注:

http://192.168.10.101

访问到的是aaa

http://192.168.10.101/index.html

访问到的是bbb

http://192.168.10.101/INDEX.html

访问到的是ccc)

正则匹配的一些表达式

  • ~:表示执行一个正则匹配,区分大小写

  • ~*:表示执行一个正则匹配,不区分大小写

  • > !~:表示执行一个正则匹配,区分大小写不匹配

  • !~":表示执行一个正则匹配,不区分大小写不匹配

  • ^~:表示普通字符匹配。使用前缀匹配。如果匹配成功,则不再匹配其他 location

  • =:进行普通字符精确匹配,也就是完全匹配

  • @:它定义一个命名的 location,使用在内部定向时,例如 error_page, try _files

Location 优先级 

Nginx location 配置中 location 的顺序没有太大关系

匹配优先级和 location 表达式的类型有关

  • 相同类型的表达式

  • 字符串长的会优先匹配

按优先级排列说明

  • 等号类型(=)的优先级最高。一旦匹配成功,则不再查找其他匹配项

  • ^~类型表达式。一旦匹配成功,则不再查找其他匹配项

  • 正则表达式类型(~~*)的优先级次之

  • 常规字符串匹配类型。按前缀匹配

  • 通用匹配(/),如果没有其它匹配,任何请求都会匹配到

(注:= 严格匹配。如果这个查询匹配,那么将停止搜索并立即处理此请求)

从功能看 rewrite 和 location 似乎有点像,都能实现跳转

主要区别

  • rewrite 是在同一域名内更改获取资源的路径
  • 而location 是对一类路径做控制访问或反向代理
  • 还可以proxy_pass 到其他机器
  • 很多情况下rewrite 也会写在 location 里

执行顺序

  • 执行server块里面的 rewrite 指令
  • 执行location 匹配
  • 执行选定的 location 中的 rewrite 指令
#精确匹配 / ,主机名后面不能带任何字符串
location = / { [ configuration A ] 
}# 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求,但是正则和最长字符串会优先匹配
location / { [ configuration B ] 
}# 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续向下搜索,只有后面的正则表达式没有匹配到时,这一条才会起作用 
location /documents/ { [ configuration C ] 
}# 匹配任何以 /documents/abc 开头的地址,匹配符合以后,还要继续向下搜索,只有后面的正则表达式没有匹配到时,这一条才会起作用 
location ~ /documents/abc { [ configuration D ] 
}# 匹配任何以 /images/ 开头的地址,匹配符合以后,停止向下匹配 
location ^~ /images/ { [ configuration E ] 
}# 匹配所有以 gif,jpg 或 jpeg 结尾的请求,然而所有请求/images/下的图片会被 [ configuration E] 处理,因为^~的优先级更高 
location ~* \.(gif|jpg|jpeg)$ { [ configuration F ] 
}# 最长字符匹配到 /images/abc,优先级最低 
location /images/abc { [ configuration G ] 
}# 匹配以/images/abc 开头的,优先级次之 
location ~ /images/abc { [ configuration H ] 
}# 匹配/images/abc/1.html 文件,如果和正则~ /images/abc/1.html 相比,正则优先级更高
location /images/abc/1.html { [ configuration I ] 
}

(总结:

如果是匹配某个具体文件

(location = 完整路径) > (location ^~ 完整路径) > (location ~* 完整路径)> (location ~ 完整路径) > (location 完整路径) > (location /)

如果是用目录做匹配访问某个文件

(location = 目录) > (location ^~ 目录/) > (location ~ 目录)> (location ~* 目录) > (location 目录) > (location /))

Rewrite 常见示例

案例环境

  • 192.168.10.101 www.benet.com 

  • 192.168.10.102 www.accp.com

设置好客户端和服务器端的hosts文件

浏览器访问www.benet.com,跳转到www.accp.com

安装 Nginx 服务

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

修改 Nginx 默认站点配置文件

[root@localhost ~l# vim /etc/nginx/conf.d/default.conf
server {listen    80:server name ww.kgc.com;#charset koi8-r;
access log /var/log/nginx/www.kgc.com-access.logmain:
location /{
root    /usr/share/nginx/html;
index index.html index.htm;
#error_page 404    /404.html;
…./省略部分
[root@localhost ~]# systemctl start nginx

Rewrite 示例

(注:在做每一步场景测试之前请确定 www.kgc.com 域名可以正常解析,每做下一个场景之前请删除上一个场景的配置。另外,要清除浏览器缓存)

flag 标记说明

  • last:相当于 Apache 的[L]标记,表示完成 rewrite。
  • break:本条规则匹配完成即终止,不再匹配后面的任何规则。
  • redirect:返回 302 临时重定向,浏览器地址会显示跳转后的 URL 地址,爬虫不会更新url(因为是临时)。
  • permanent:返回 301 永久重定向,浏览器地址栏会显示跳转后的 URL 地址,爬虫更新 url

基于域名的跳转

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.confserver {listen       80;server_name  www.benet.com;charset utf-8;#access_log  logs/host.access.log  main;location / {root   html;index  index.html index.htm;if ($host = 'www.benet.com') {rewrite ^/(.*)$ http://www.accp.com/$1 permanent;}}
}
[root@localhost ~]# systemctl restart nginx

 域名直接跳转

  • F12 可以清楚的看到从旧域名 www.benet.com 跳转到了新域名 www.accp.com 上,状态码是 301 永久重定向

  • b:域名后面加参数跳转

基于客户端 IP 访问跳转

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.confserver {listen       80;server_name  www.benet.com;#access_log  /var/log/nginx/host.access.log  main;set $rewrite true;if ($remote_addr = "192.168.10.51") {set $rewrite false;}if ($rewrite = true) {rewrite (.+) /maintenance.html;}location / {root   html;index  index.html index.htm;#	if ($host = 'www.benet.com') {
#	  rewrite ^/(.*)$ http://www.accp.com/$1 permanent; 
#	}}
}
  • (.+)  匹配整个URL,匹配上以后,就在URL后面加上/maintenance.html

  • .+  匹配任意字符1次或多次

  • .*  匹配任意字符0次或多次

[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# echo "Website is Maintaining,Please visit later."> /usr/local/nginx/html/maintenance.html

(注:

$host = 'www.benet.com'  判断获取到得请求URL是否为www.benet.com     另外,=号两边有空格

permanent:返回 301 永久重定向,浏览器地址栏会显示跳转后的 URL 地址,爬虫更新 url,没有则返回302)

^/(.*)$

  • ^  指的是以任何字符或字符串开始的域名都会匹配上

  • /  指得是url后跟的/,比如www.benet.com/index.html

  • .*  匹配任意多个字符,注意这里的(),括号里的内容会用后面的$1调用

  • $   结尾

(注:Rewrite 语法rewrite<regex><replacement>[flag];

基于旧域名跳转到新域名后面加目录

[root@localhost html]# cd /usr/local/nginx/html/
[root@localhost html]# mkdir -p bbs/post
[root@localhost html]# cd bbs/post
[root@localhost post]# echo "i am bbs">index.html
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server {listen       80;server_name  bbs.benet.com;#access_log  /var/log/nginx/host.access.log  main;location /post {rewrite (.+) http://www.benet.com/bbs$1 permanent;}location / {root   html;index  index.html index.htm;}
}
[root@localhost ~]# systemctl restart nginx

客户端添加域名解析

  • 192.168.10.101 bbs.benet.com

基于参数匹配的跳转

root@localhost post]# vim /usr/local/nginx/conf/nginx.conf
server {listen       80;server_name  bbs.benet.com;#access_log  /var/log/nginx/host.access.log  main;if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {rewrite (.*) http://www.benet.com permanent;}location / {root   html;index  index.html index.htm;}
}
  • \d+  匹配任意个数字

[root@localhost ~]# systemctl restart nginx

基于目录下所有 php 结尾的文件跳转

[root@localhost post]# vim /usr/local/nginx/conf/nginx.conf
server {listen       80;server_name  bbs.benet.com;#access_log  /var/log/nginx/host.access.log  main;location ~* /upload/.*\.php$ {rewrite (.+) http://www.benet.com permanent;}location / {root   html;index  index.html index.htm;}
}

注:

  • ~*  不区分大小写

  • ~   区分大小写

  • .*\.php  一个文件的名字   名字.后缀   任意名字.php

[root@localhost ~]# systemctl restart nginx

浏览器访问 http://www.benet.com/upload/1.php会跳转到http://www.benet.com

基于最普通一条 url 请求的跳转

[root@localhost post]# vim /usr/local/nginx/conf/nginx.confserver {listen       80;server_name  bbs.benet.com;#access_log  /var/log/nginx/host.access.log  main;location ~* ^/1/test.html {rewrite (.+) http://www.benet.com permanent; }location / {root   html;index  index.html index.htm;}
}

浏览器访问http://www.benet.com/1/test.html 跳转到http://www.benet.com

小阿轩yx-Nginx Rewrite

这篇关于小阿轩yx-Nginx Rewrite的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1076624

相关文章

关于Nginx跨域问题及解决方案(CORS)

《关于Nginx跨域问题及解决方案(CORS)》文章主要介绍了跨域资源共享(CORS)机制及其在现代Web开发中的重要性,通过Nginx,可以简单地解决跨域问题,适合新手学习和应用,文章详细讲解了CO... 目录一、概述二、什么是 CORS?三、常见的跨域场景四、Nginx 如何解决 CORS 问题?五、基

Nginx实现动态封禁IP的步骤指南

《Nginx实现动态封禁IP的步骤指南》在日常的生产环境中,网站可能会遭遇恶意请求、DDoS攻击或其他有害的访问行为,为了应对这些情况,动态封禁IP是一项十分重要的安全策略,本篇博客将介绍如何通过NG... 目录1、简述2、实现方式3、使用 fail2ban 动态封禁3.1 安装 fail2ban3.2 配

Nginx启动失败:端口80被占用问题的解决方案

《Nginx启动失败:端口80被占用问题的解决方案》在Linux服务器上部署Nginx时,可能会遇到Nginx启动失败的情况,尤其是错误提示bind()to0.0.0.0:80failed,这种问题通... 目录引言问题描述问题分析解决方案1. 检查占用端口 80 的进程使用 netstat 命令使用 ss

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接

nginx部署https网站的实现步骤(亲测)

《nginx部署https网站的实现步骤(亲测)》本文详细介绍了使用Nginx在保持与http服务兼容的情况下部署HTTPS,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录步骤 1:安装 Nginx步骤 2:获取 SSL 证书步骤 3:手动配置 Nginx步骤 4:测

Windows设置nginx启动端口的方法

《Windows设置nginx启动端口的方法》在服务器配置与开发过程中,nginx作为一款高效的HTTP和反向代理服务器,被广泛应用,而在Windows系统中,合理设置nginx的启动端口,是确保其正... 目录一、为什么要设置 nginx 启动端口二、设置步骤三、常见问题及解决一、为什么要设置 nginx

nginx-rtmp-module模块实现视频点播的示例代码

《nginx-rtmp-module模块实现视频点播的示例代码》本文主要介绍了nginx-rtmp-module模块实现视频点播,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录预置条件Nginx点播基本配置点播远程文件指定多个播放位置参考预置条件配置点播服务器 192.

nginx-rtmp-module构建流媒体直播服务器实战指南

《nginx-rtmp-module构建流媒体直播服务器实战指南》本文主要介绍了nginx-rtmp-module构建流媒体直播服务器实战指南,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有... 目录1. RTMP协议介绍与应用RTMP协议的原理RTMP协议的应用RTMP与现代流媒体技术的关系2

解决systemctl reload nginx重启Nginx服务报错:Job for nginx.service invalid问题

《解决systemctlreloadnginx重启Nginx服务报错:Jobfornginx.serviceinvalid问题》文章描述了通过`systemctlstatusnginx.se... 目录systemctl reload nginx重启Nginx服务报错:Job for nginx.javas

centos7基于keepalived+nginx部署k8s1.26.0高可用集群

《centos7基于keepalived+nginx部署k8s1.26.0高可用集群》Kubernetes是一个开源的容器编排平台,用于自动化地部署、扩展和管理容器化应用程序,在生产环境中,为了确保集... 目录一、初始化(所有节点都执行)二、安装containerd(所有节点都执行)三、安装docker-