当前位置: 首页 > news >正文

Nginx rewrite项目练习

Nginx rewrite练习

1、访问ip/xcz,返回400状态码,要求用rewrite匹配/xcz

a、访问/xcz返回400
b、访问/hello时正常访问xcz.html页面
	  server {listen 192.168.99.137:80;server_name 192.168.99.137;charset utf-8;root /var/www/html;location / {root /var/www/html;rewrite ^/xcz$ /q.html last;rewrite ^/hello$ /xcz.html last;index index.html index.htm index.php;}location = /q.html {return 400;}
}

在这里插入图片描述
在这里插入图片描述

2、访问http://kgc.com/ 时跳转至 http://jd.com

server {listen 192.168.99.137:80;server_name kgc.com;charset utf-8;root /var/www/html;location / {root /var/www/html;if ($host ~* kgc.com ) {rewrite .* http://jd.com permanent;}index index.html index.htm index.php;}}

windows hosts文件添加

192.168.99.137 kgc.com

在这里插入图片描述

3、访问http://kgc.com/a/1.html时跳转至http://jd.com/a/1.html

 server {listen 192.168.99.137:80;server_name kgc.com;charset utf-8;root /var/www/html;location / {root /var/www/html;if ($host ~* kgc.com ) {rewrite /a/1.html http://jd.com/a/1.html permanent;}index index.html index.htm index.php;}}

在这里插入图片描述

4、通过http://kgc.com访问nginx根目录下的index.html

通过http://alice.kgc.com访问http://kgc.com/alice
通过http://jack.kgc.com访问http://kgc.com/jack

cd /var/www/html
mkdir jack alice
echo jack.... > jack/index.html
echo alice... > alice/index.html

windows hosts文件添加

192.168.99.137 kgc.com
192.168.99.137 jack.kgc.com
192.168.99.137 alice.kgc.com

ngnix.conf配置

 server {listen 192.168.99.137:80;server_name kgc.com;charset utf-8;root /var/www/html;location / {root /var/www/html;if ($host = kgc.com ) {break;}if ( $host ~* "^(.*)\.kgc\.com$" ) {set $user $1;rewrite .* http://kgc.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;}}

通过http://alice.kgc.com访问http://kgc.com/alice
在这里插入图片描述

访问jack.kgc.com
在这里插入图片描述

5、将所有URL 重定向到加上 .html 后缀的 URL,例:aa.com/a==>aa.com/a.html。

server {listen 192.168.99.137:80;server_name aa.com;charset utf-8;root /var/www/html;location / {root /var/www/html;if ($request_uri !~* "\.html$") {rewrite ^/(.*)$ /$1.html break;}}}

在这里插入图片描述

6、将所有 .html 结尾的 URL 重定向到去掉 .html 后缀的 URL,例:aa.com/a.html==>aa.com/a。

 if ($request_uri ~* "\.html$") {rewrite ^/(.*)\.html$ /$1 break;}

在这里插入图片描述

7、将所有 /blog/post/<id> 的请求重定向到 /blog/article/<id>,id为数字。

server {listen 192.168.0.116:80;server_name www.clean.com;charset utf-8;root /var/www/html/clean;location ~ ^/blog/post/(\d+)/$ {root /var/www/html/clean;rewrite ^/blog/post/(\d+)/$ /blog/article/$1 last;index index.html index.htm index.php;}location /article {root /var/www/html/clean/blog;index index.html index.htm index.php;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}

访问192.168.0.116/post/1/
在这里插入图片描述

脚本:

#!/usr/bin/bashread -p "测试题目号码:" num#检查nginx配置文件是否正确,并根据结果重启nginx服务。
function nginx_test {nginx -t &> /dev/nullif [ $? -eq 0 ]; thensystemctl restart nginxecho 'nginx配置文件正确'elseecho "nginx配置文件有误"exit $numfi}case $num in
1)echo '访问ip/xcz,返回400状态码,要求用rewrite匹配/xcz,要求:a、访问/xcz返回400
b、访问/hello时正常访问xcz.html页面'# 生成Nginx配置,重定向/xcz到/q.html返回400,/hello到/xcz.htmlcat >/etc/nginx/conf.d/test0506.conf <<EOFserver {listen 192.168.99.137:80;server_name 192.168.99.137;charset utf-8;root /var/www/html;location / {root /var/www/html;rewrite ^/xcz$ /q.html last;rewrite ^/hello$ /xcz.html last;index index.html index.htm index.php;}location = /q.html {return 400;}
}
EOF# 创建xcz.html页面内容cat >/var/www/html/xcz.html <<EOF
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><h1>xcz</h1>
</body>
</html>
EOF
nginx_test ;;
2)echo '访问http://kgc.com/ 时跳转至 http://jd.com'cat >/etc/nginx/conf.d/test0506.conf <<EOFserver {listen 192.168.99.137:80;server_name kgc.com;charset utf-8;root /var/www/html;location / {root /var/www/html;if ( \$host ~ kgc.com ) {rewrite .* http://jd.com permanent;}index index.html index.htm index.php;}}
EOF
nginx_test ;;
3)echo '访问http://kgc.com/a/1.html时跳转至http://jd.com/a/1.html'cat >/etc/nginx/conf.d/test0506.conf <<EOFserver {listen 192.168.99.137:80;server_name kgc.com;charset utf-8;root /var/www/html;location / {root /var/www/html;if ( \$host ~ kgc.com ) {rewrite /a/1.html http://jd.com/a/1.html permanent;}index index.html index.htm index.php;}}
EOF
nginx_test ;;
4)echo '通过http://kgc.com访问nginx根目录下的index.html
通过http://alice.kgc.com访问http://kgc.com/alice
通过http://jack.kgc.com访问http://kgc.com/jack'cd /var/www/htmlmkdir jack aliceecho jack.... >jack/index.htmlecho alice... >alice/index.html# 配置基于域名的根目录变更,测试并重启Nginxcat >/etc/nginx/conf.d/test0506.conf <<EOFserver {listen 192.168.99.137:80;server_name kgc.com;charset utf-8;root /var/www/html;location / {root /var/www/html;if ( \$host = kgc.com ) {break;}if ( \$host ~* "^(.*)\.kgc\.com$" ) {set \$user \$1;rewrite .* http://kgc.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;}}
EOF
nginx_test ;;
5)# 题目5:处理URL添加.html后缀的重定向规则echo '将所有URL 重定向到加上 .html 后缀的 URL,例:aa.com/a==>aa.com/a.html。'cat >/etc/nginx/conf.d/test0506.conf <<EOFserver {listen 192.168.99.137:80;server_name aa.com;charset utf-8;root /var/www/html;location / {root /var/www/html;if (\$request_uri !~* "\.html$") {rewrite ^/(.*)$ /\$1.html break;}}}
EOF# 创建示例页面jkl.html,进行配置、测试、重启cat >/var/www/html/jkl.html <<EOF
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><h1>jkl</h1>
</body>
</html>
EOF
nginx_test ;;
6)# 题目6:处理URL移除.html后缀的重定向规则echo '将所有 .html 结尾的 URL 重定向到去掉 .html 后缀的 URL,例:aa.com/a.html==>aa.com/a'cat >/etc/nginx/conf.d/test0506.conf <<EOFserver {listen 192.168.99.137:80;server_name aa.com;charset utf-8;root /var/www/html;location / {root /var/www/html;rewrite ^/(.*)\.html$ /\$1 permanent;}location /jk {root /var/www/html;index index.html index.htm index.php;}}
EOFmkdir -p /var/www/html/jktouch /var/www/html/jk/index.html cat >/var/www/html/jk/index.html <<EOF
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><h1>jk666</h1>
</body>
</html>
EOF
nginx_test ;;
7)echo '将所有 /blog/post/<id> 的请求重定向到 /blog/article/<id>,id为数字。'cat >/etc/nginx/conf.d/test0506.conf <<EOFserver {listen 192.168.99.137:80;server_name 192.168.99.137;charset utf-8;root /var/www/html;location ~ ^/blog/post/(\d+)/$ {root /var/www/html;rewrite ^/blog/post/(\d+)/$ /blog/article/\$1 last;index index.html index.htm index.php;}location /post {root /var/www/html/blog;index index.html index.htm index.php;}location /article {root /var/www/html/blog;index index.html index.htm index.php;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}
EOF
# 创建必要的目录结构和索引文件,配置重定向规则,测试并重启mkdir -p /var/www/html/blog/article/1touch /var/www/html/blog/article/1/index.htmlmkdir -p /var/www/html/blog/post/1touch /var/www/html/blog/post/1/index.htmlcat >/var/www/html/blog/article/1/index.html <<EOF
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><h1>1</h1>
</body>
</html>
EOF
nginx_test ;;
*)# 如果输入的不是1-7中的任意一个数字,显示错误信息并退出echo "一共7题,输入错误"exit 8;;
esac

相关文章:

Nginx rewrite项目练习

Nginx rewrite练习 1、访问ip/xcz&#xff0c;返回400状态码&#xff0c;要求用rewrite匹配/xcz a、访问/xcz返回400 b、访问/hello时正常访问xcz.html页面server {listen 192.168.99.137:80;server_name 192.168.99.137;charset utf-8;root /var/www/html;location / {root …...

2024,AI手机“元年”? | 最新快讯

文 | 伯虎财经&#xff0c;作者 | 铁观音 2024年&#xff0c;小米、荣耀、vivo、一加、努比亚等品牌的AI手机新品如雨后春笋般涌现。因此&#xff0c;这一年也被业界广泛视为AI手机的“元年” 试想&#xff0c;当你轻触屏幕&#xff0c;你的手机不仅响应你的指令&#xff0c;更…...

5月9(信息差)

&#x1f30d; 可再生能源发电量首次占全球电力供应的三成 &#x1f384;马斯克脑机接口公司 Neuralink 计划将 Link 功能扩展至现实世界&#xff0c;实现控制机械臂、轮椅等 马斯克脑机接口公司 Neuralink 计划将 Link 功能扩展至现实世界&#xff0c;实现控制机械臂、轮椅等…...

leetcode203-Remove Linked List Elements

题目 给你一个链表的头节点 head 和一个整数 val &#xff0c;请你删除链表中所有满足 Node.val val 的节点&#xff0c;并返回 新的头节点 。 示例 1&#xff1a; 输入&#xff1a;head [1,2,6,3,4,5,6], val 6 输出&#xff1a;[1,2,3,4,5] 示例 2&#xff1a; 输入&…...

2024付费进群系统,源码及搭建变现视频课程(教程+源码)

自从我做资源站项目盈利稳定后&#xff0c;我越来越对网站类项目感兴趣了&#xff0c;毕竟很多网站类项目还是需要一定技术门槛的&#xff0c;可以过滤掉一些人&#xff0c;很多新人做项目就只盯着短视频&#xff0c;所以网站类项目也就没那么的卷。 这个付费进群系统&#xf…...

深入理解Django:中间件与信号处理的艺术

title: 深入理解Django&#xff1a;中间件与信号处理的艺术 date: 2024/5/9 18:41:21 updated: 2024/5/9 18:41:21 categories: 后端开发 tags: Django中间件信号异步性能缓存多语言 引言 在当今的Web开发领域&#xff0c;Django以其强大的功能、简洁的代码结构和高度的可扩…...

rk3588局域网推流

最近无意间看见在网上有使用MediaMtx插件配合ffmpeg在Windows来进行推流&#xff0c;然后在使用其他软件进行拉流显示数据图像的&#xff0c;既然windows都可以使用 &#xff0c;我想linux应该也可以&#xff0c;正好手上也有一块RK3588的开发板&#xff0c;就测试了一下&#…...

Android虚拟机机制

目录 一、Android 虚拟机 dalvik/art&#xff08;6版本后&#xff09;二、Android dex、odex、oat、vdex、art区别 一、Android 虚拟机 dalvik/art&#xff08;6版本后&#xff09; 每个应用都在其自己的进程中运行&#xff0c;都有自己的虚拟机实例。ART通过执行DEX文件可在设…...

【触摸案例-手势解锁案例-按钮高亮 Objective-C语言】

一、我们来说这个self.btns,这个问题啊,为什么不用_btns, 1.我们说,在懒加载里边儿,经常是写下划线啊,_btns,为什么不写,首先啊,这个layoutSubviews:我们第一次,肯定会去执行这个layoutSubviews: 然后呢,去懒加载这个数组, 然后呢,接下来啊,走这一句话, 第一次…...

ChatPPT开启高效办公新时代,AI赋能PPT创作

目录 一、前言二、ChatPPT的几种用法1、通过在线生成2、通过插件生成演讲者模式最终成品遇到问题改进建议 三、ChatPPT其他功能 一、前言 想想以前啊&#xff0c;为了做个PPT&#xff0c;我得去网上找各种模板&#xff0c;有时候还得在某宝上花钱买。结果一做PPT&#xff0c;经…...

【C语言项目】贪吃蛇(上)

个人主页 ~ gitee仓库~ 欢迎大家来到C语言系列的最后一个篇章–贪吃蛇游戏的实现&#xff0c;当我们实现了贪吃蛇之后&#xff0c;我们的C语言就算是登堂入室了&#xff0c;基本会使用了&#xff0c;当然&#xff0c;想要更加熟练地使用还需要多多练习 贪吃蛇 一、目标二、需要…...

LeNet-5上手敲代码

LeNet-5 LeNet-5由Yann LeCun在1998年提出&#xff0c;旨在解决手写数字识别问题&#xff0c;被认为是卷积神经网络的开创性工作之一。该网络是第一个被广泛应用于数字图像识别的神经网络之一&#xff0c;也是深度学习领域的里程碑之一。 LeNet-5的整体架构&#xff1a; 总体…...

javaWeb入门(自用)

1. vue学习 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Title</title><script src"https://unpkg.com/vue2"></script> </head> <body><div id"…...

web3风格的网页怎么设计?分享几个,找找感觉。

web3风格的网站是指基于区块链技术和去中心化理念的网站设计风格。这种设计风格强调开放性、透明性和用户自治&#xff0c;体现了Web3的核心价值观。 以下是一些常见的Web3风格网站设计元素&#xff1a; 去中心化标志&#xff1a;在网站的设计中使用去中心化的标志&#xff0…...

ASP.NET MVC(-)表单的提交、获取表单数据

FromCollection 方式...

[AIGC] 《MyBatis-Plus 结合 Spring Boot 的动态数据源介绍及 Demo 演示》

在现代的 Web 应用开发中&#xff0c;Spring Boot 已经成为了一种流行的框架选择。而 MyBatis-Plus 则为 MyBatis 框架提供了更强大的功能和便利。当它们结合使用时&#xff0c;动态数据源的运用变得更加简单和高效。 动态数据源的概念允许我们在运行时根据不同的条件或需求选…...

【华为OD机试C卷D卷】部门人力分配(C++/Java/Python)

【华为OD机试】-(A卷+B卷+C卷+D卷)-2024真题合集目录 【华为OD机试】-(C卷+D卷)-2024最新真题目录 题目描述 部门在进行需求开发时需要进行人力安排。 当前部门需要完成 N 个需求,需求用 requirements 表述,requirements[i] 表示第 i 个需求的工作量大小,单位:人月。 这部…...

毕业设计:《基于 Prometheus 和 ELK 的基础平台监控系统设计与实现》

前言 《基于 Prometheus 和 ELK 的基础平台监控系统设计与实现》&#xff0c;这是我在本科阶段的毕业设计&#xff0c;通过引入 Prometheus 和 ELK 架构实现企业对指标与日志的全方位监控。并且基于云原生&#xff0c;使用容器化持续集成部署的开发方式&#xff0c;通过 Sprin…...

docker私有仓库部署与管理

一、搭建本地公有仓库 1.1 首先下载registry镜像 docker pull registry 1.2 在daemon.json文件中添加私有镜像仓库地址并重新启动docker服务 vim /etc/docker/daemon.json 1.3 运行registry容器 docker run -itd -v /data/registry:/var/lib/registry -p 5000:5000 --restartal…...

2024第六届济南国际大健康产业博会将于5月27日如期开幕

由山东省城市经济学会、山东省科学养生协会主办的第六届中国&#xff08;济南&#xff09;国际大健康产业博览会&#xff0c;将于5月27-29日&#xff0c;在济南黄河国际会展中心盛大举办。 近年来&#xff0c;健康越来越受到大众的重视&#xff0c;在我国经济重要的转型阶段成…...

wordpress后台更新后 前端没变化的解决方法

使用siteground主机的wordpress网站&#xff0c;会出现更新了网站内容和修改了php模板文件、js文件、css文件、图片文件后&#xff0c;网站没有变化的情况。 不熟悉siteground主机的新手&#xff0c;遇到这个问题&#xff0c;就很抓狂&#xff0c;明明是哪都没操作错误&#x…...

conda相比python好处

Conda 作为 Python 的环境和包管理工具&#xff0c;相比原生 Python 生态&#xff08;如 pip 虚拟环境&#xff09;有许多独特优势&#xff0c;尤其在多项目管理、依赖处理和跨平台兼容性等方面表现更优。以下是 Conda 的核心好处&#xff1a; 一、一站式环境管理&#xff1a…...

内存分配函数malloc kmalloc vmalloc

内存分配函数malloc kmalloc vmalloc malloc实现步骤: 1)请求大小调整:首先,malloc 需要调整用户请求的大小,以适应内部数据结构(例如,可能需要存储额外的元数据)。通常,这包括对齐调整,确保分配的内存地址满足特定硬件要求(如对齐到8字节或16字节边界)。 2)空闲…...

【Linux】shell脚本忽略错误继续执行

在 shell 脚本中&#xff0c;可以使用 set -e 命令来设置脚本在遇到错误时退出执行。如果你希望脚本忽略错误并继续执行&#xff0c;可以在脚本开头添加 set e 命令来取消该设置。 举例1 #!/bin/bash# 取消 set -e 的设置 set e# 执行命令&#xff0c;并忽略错误 rm somefile…...

云启出海,智联未来|阿里云网络「企业出海」系列客户沙龙上海站圆满落地

借阿里云中企出海大会的东风&#xff0c;以**「云启出海&#xff0c;智联未来&#xff5c;打造安全可靠的出海云网络引擎」为主题的阿里云企业出海客户沙龙云网络&安全专场于5.28日下午在上海顺利举办&#xff0c;现场吸引了来自携程、小红书、米哈游、哔哩哔哩、波克城市、…...

【第二十一章 SDIO接口(SDIO)】

第二十一章 SDIO接口 目录 第二十一章 SDIO接口(SDIO) 1 SDIO 主要功能 2 SDIO 总线拓扑 3 SDIO 功能描述 3.1 SDIO 适配器 3.2 SDIOAHB 接口 4 卡功能描述 4.1 卡识别模式 4.2 卡复位 4.3 操作电压范围确认 4.4 卡识别过程 4.5 写数据块 4.6 读数据块 4.7 数据流…...

在四层代理中还原真实客户端ngx_stream_realip_module

一、模块原理与价值 PROXY Protocol 回溯 第三方负载均衡&#xff08;如 HAProxy、AWS NLB、阿里 SLB&#xff09;发起上游连接时&#xff0c;将真实客户端 IP/Port 写入 PROXY Protocol v1/v2 头。Stream 层接收到头部后&#xff0c;ngx_stream_realip_module 从中提取原始信息…...

Spring Boot面试题精选汇总

&#x1f91f;致敬读者 &#x1f7e9;感谢阅读&#x1f7e6;笑口常开&#x1f7ea;生日快乐⬛早点睡觉 &#x1f4d8;博主相关 &#x1f7e7;博主信息&#x1f7e8;博客首页&#x1f7eb;专栏推荐&#x1f7e5;活动信息 文章目录 Spring Boot面试题精选汇总⚙️ **一、核心概…...

uniapp微信小程序视频实时流+pc端预览方案

方案类型技术实现是否免费优点缺点适用场景延迟范围开发复杂度​WebSocket图片帧​定时拍照Base64传输✅ 完全免费无需服务器 纯前端实现高延迟高流量 帧率极低个人demo测试 超低频监控500ms-2s⭐⭐​RTMP推流​TRTC/即构SDK推流❌ 付费方案 &#xff08;部分有免费额度&#x…...

力扣-35.搜索插入位置

题目描述 给定一个排序数组和一个目标值&#xff0c;在数组中找到目标值&#xff0c;并返回其索引。如果目标值不存在于数组中&#xff0c;返回它将会被按顺序插入的位置。 请必须使用时间复杂度为 O(log n) 的算法。 class Solution {public int searchInsert(int[] nums, …...