当前位置:网站首页>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
边栏推荐
- Leetcode T48:旋转图像
- 如何搭建一支搞垮公司的技术团队?
- 开发中常见问题总结
- ML之shap:基于boston波士顿房价回归预测数据集利用shap值对XGBoost模型实现可解释性案例
- 实时数据仓库
- 失败率高达80%,企业数字化转型路上有哪些挑战?
- Data center concept
- Data Lake (13): spark and iceberg integrate DDL operations
- A keepalived high availability accident made me learn it again
- Stm32f1 and stm32subeide programming example -max7219 drives 8-bit 7-segment nixie tube (based on GPIO)
猜你喜欢
商业智能BI财务分析,狭义的财务分析和广义的财务分析有何不同?
scratch古堡历险记 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
Stm32f1 and stm32subeide programming example -max7219 drives 8-bit 7-segment nixie tube (based on GPIO)
Summary of common problems in development
No servers available for service: xxxx
Pandora IOT development board learning (RT thread) - Experiment 3 button experiment (learning notes)
Leetcode 61: rotating linked list
为什么国产手机用户换下一部手机时,都选择了iPhone?
Real time data warehouse
[MySQL from introduction to proficiency] [advanced chapter] (IV) MySQL permission management and control
随机推荐
Leetcode T47: 全排列II
《opencv学习笔记》-- 线性滤波:方框滤波、均值滤波、高斯滤波
LVGL 8.2 Draw label with gradient color
Leetcode t47: full arrangement II
C language set operation
Ranking list of databases in July: mongodb and Oracle scores fell the most
聊聊保证线程安全的 10 个小技巧
Node mongodb installation
Is it safe to open an account online for stock speculation? Will you be cheated.
Pandora IOT development board learning (RT thread) - Experiment 3 button experiment (learning notes)
Alcohol driving monitoring system based on stm32+ Huawei cloud IOT design
Digi XBee 3 RF: 4个协议,3种封装,10个大功能
scratch古堡历险记 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
毕业季-个人总结
Redis daily notes
C language course design questions
架构方面的进步
Nowcoder rearrange linked list
实战解惑 | OpenCV中如何提取不规则ROI区域
阿里被裁员工,找工作第N天,猎头又传来噩耗...