当前位置:网站首页>openresty 重定向
openresty 重定向
2022-07-04 13:22:00 【o_瓜田李下_o】
openresty 重定向
重定向
重定向:避免网站路径、域名修改后,网站中原有链接失效
命令格式:rewrite old_url new_url [flag]
old_url:旧的url路径,可为正则表达式
new_url:新的跳转的url,匹配正则表达式后,跳转到该地址
# flag:进一步处理的标识
last:终止rewrite,服务端跳转到指定路径
break:终止rewrite,不再继续匹配
redirect:临时重定向,返回响应码302
permanent:永久重定向,返回响应码301
示例
default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/local/openresty/nginx/html;
index index.html index.htm;
}
#location 1
location /break {
root /usr/share/nginx/html2;
if ( !-e $request_filename ){ #root目录下查找文件/default/info
rewrite ^/break/(.*) /default/info break;
}
}
#location 2
location /break2 {
root /usr/share/nginx/html2;
if ( !-e $request_filename ){ #root目录下查找文件/default/info,
#随后继续执行下面语句,echo "break"
rewrite ^/break2/(.*) /default/info break;
echo "break";
}
}
#location 3
location /last {
if ( !-e $request_filename ){ #匹配后,跳转到/test/$1路径
rewrite ^/last/(.*) /test/$1 last;
echo "last";
}
}
#location 4
location /test {
echo "test";
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/openresty/nginx/html;
}
}
本地文件
[email protected] redirect % pwd
/Users/huli/lua/openresty/redirect
[email protected] redirect % ls html
default
[email protected] redirect % ls html/default
info
[email protected] redirect % cat html/default/info
break info
创建容器
docker run -it -d -p 1000:80 \
-v /Users/huli/lua/openresty/redirect/html:/usr/local/openresty/nginx/html2 \
-v /Users/huli/lua/openresty/redirect/default.conf:/etc/nginx/conf.d/default.conf \
--name open2 lihu12344/openresty
使用测试
[email protected] redirect % curl localhost:1000/break/1
break info
[email protected] redirect % curl localhost:1000/break2/1
break
[email protected] redirect % curl localhost:1000/last
last
[email protected] redirect % curl localhost:1000/last/1
test
[email protected] redirect % curl localhost:1000/test
test
示例 2
default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /redirect {
if ( !-e $request_filename ) {
rewrite ^/redirect/(.*) /test/$1 redirect;
}
}
location /permanent {
if ( !-e $request_filename ) {
rewrite ^/permanent/(.*) /test/$1 permanent;
}
}
location /test {
echo "test";
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
创建容器
docker run -it -d -p 2000:80 \
-v /Users/huli/lua/openresty/redirect/default2.conf:/etc/nginx/conf.d/default.conf \
--name open3 lihu12344/openresty
使用测试
[email protected] redirect % curl -I localhost:2000/redirect/1
HTTP/1.1 302 Moved Temporarily
Server: openresty/1.21.4.1
Date: Mon, 04 Jul 2022 04:50:08 GMT
Content-Type: text/html
Content-Length: 151
Location: http://localhost/test/1
Connection: keep-alive
[email protected] redirect % curl -I localhost:2000/permanent/1
HTTP/1.1 301 Moved Permanently
Server: openresty/1.21.4.1
Date: Mon, 04 Jul 2022 04:50:17 GMT
Content-Type: text/html
Content-Length: 175
Location: http://localhost/test/1
Connection: keep-alive
边栏推荐
- 利用Shap值进行异常值检测
- 《opencv学习笔记》-- 线性滤波:方框滤波、均值滤波、高斯滤波
- 炒股网上开户安全吗?会不会被骗。
- Query optimizer for SQL optimization
- The failure rate is as high as 80%. What are the challenges on the way of enterprise digital transformation?
- Count the running time of PHP program and set the maximum running time of PHP
- LVGL 8.2 LED
- Leetcode 61: 旋转链表
- C language personal address book management system
- 一文概览2D人体姿态估计
猜你喜欢

Detailed explanation of visual studio debugging methods

Leetcode 61: 旋转链表

NowCoder 反转链表

韩国AI团队抄袭震动学界!1个导师带51个学生,还是抄袭惯犯
![[information retrieval] experiment of classification and clustering](/img/05/ee3b3bc4ab79d52b63cdc34305aa57.png)
[information retrieval] experiment of classification and clustering

Docker compose public network deployment redis sentinel mode

Digi XBee 3 RF: 4个协议,3种封装,10个大功能

Scratch Castle Adventure Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022

STM32F1与STM32CubeIDE编程实例-MAX7219驱动8位7段数码管(基于GPIO)

如何配和弦
随机推荐
软件测试之测试评估
關於miui12.5 紅米k20pro用au或者povo2出現問題的解决辦法
scratch古堡历险记 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
LVGL 8.2 Draw label with gradient color
RK1126平台OSD的实现支持颜色半透明度多通道支持中文
STM32F1与STM32CubeIDE编程实例-MAX7219驱动8位7段数码管(基于GPIO)
Progress in architecture
[information retrieval] experiment of classification and clustering
Visual Studio调试方式详解
程序员自曝接私活:10个月时间接了30多个单子,纯收入40万
Some problems and ideas of data embedding point
es6模块化
LVGL 8.2 keyboard
LVGL 8.2 Line wrap, recoloring and scrolling
Expose Ali's salary and position level
Nowcoder reverse linked list
MySQL的存储过程练习题
LVGL 8.2 text shadow
ML之shap:基于boston波士顿房价回归预测数据集利用Shap值对LiR线性回归模型实现可解释性案例
Practical puzzle solving | how to extract irregular ROI regions in opencv