当前位置:网站首页>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
边栏推荐
猜你喜欢
阿里被裁员工,找工作第N天,猎头又传来噩耗...
Sqlserver functions, creation and use of stored procedures
Leetcode 61: 旋转链表
What is the difference between Bi financial analysis in a narrow sense and financial analysis in a broad sense?
leetcode:6110. The number of incremental paths in the grid graph [DFS + cache]
《opencv学习笔记》-- 线性滤波:方框滤波、均值滤波、高斯滤波
Pandora IOT development board learning (RT thread) - Experiment 3 button experiment (learning notes)
STM32F1与STM32CubeIDE编程实例-MAX7219驱动8位7段数码管(基于GPIO)
Test evaluation of software testing
[information retrieval] experiment of classification and clustering
随机推荐
LVGL 8.2 List
Digi重启XBee-Pro S2C生产,有些差别需要注意
LVGL 8.2 Sorting a List using up and down buttons
利用Shap值进行异常值检测
[information retrieval] experiment of classification and clustering
(1) The standard of performance tuning and the correct posture for tuning - if you have performance problems, go to the heapdump performance community!
Free, easy-to-use, powerful lightweight note taking software evaluation: drafts, apple memo, flomo, keep, flowus, agenda, sidenote, workflow
Intelligence d'affaires bi analyse financière, analyse financière au sens étroit et analyse financière au sens large sont - ils différents?
Explain of SQL optimization
第十七章 进程内存
One architecture to complete all tasks - transformer architecture is unifying the AI Jianghu on its own
软件测试之测试评估
Red envelope activity design in e-commerce system
Detailed analysis of pytorch's automatic derivation mechanism, pytorch's core magic
[MySQL from introduction to proficiency] [advanced chapter] (IV) MySQL permission management and control
实战解惑 | OpenCV中如何提取不规则ROI区域
PyTorch的自动求导机制详细解析,PyTorch的核心魔法
Programmers exposed that they took private jobs: they took more than 30 orders in 10 months, with a net income of 400000
LVGL 8.2 Menu
Progress in architecture