2

Nginx架构篇(13)Rewrite - 替换url中一部分的内容

 1 year ago
source link: https://blog.51cto.com/u_15630500/5372681
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

例2 替换url中一部分的内容

利用正则中的”()和\1 “,
替换url中一部分的内容。
将http://192.168.19.100/2016/a/b/c/1.html
换http://192.168.19.100/2017/a/b/c/1.html

实验环境:

mkdir -p /usr/share/nginx/2016/a/b/c/ 创建源目录
mkdir -p /usr/share/nginx/2017/a/b/c/ 创建待测试目录

echo /a/b/a/1.html > /usr/share/nginx/2017/a/b/c/1.html 仅替换url
cp /usr/share/nginx/2016/a/b/c/1.html /usr/share/nginx/2017/a/b/c/1.html

配置地址重写:

vim /etc/nginx/conf.d/default.conf
location /2016 {
rewrite ^/2016(.*)$ /2017/$1 permanent;
#^/2016(.*)$: 以/2016开头任意字符结尾
}

访问旧页面进行测试;http://192.168.19.100/2016/a/b/c/1.html

Nginx架构篇(13)Rewrite - 替换url中一部分的内容_nginx

url因为permanent被重新改写 ,请求也变为2次。客户端重新申请了两次

cat /var/log/nginx/access.log

192.168.19.102 - - [10/Jun/2022:11:15:55 +0800] "GET /2016/a/b/c/1.html HTTP/1.1" 301 169 "-" "ELinks/0.12pre6 (textmode; Linux; 113x20-2)" "-"
192.168.19.102 - - [10/Jun/2022:11:15:55 +0800] "GET /2017//a/b/c/1.html HTTP/1.1" 200 14 "http://192.168.19.100/2016/a/b/c/1.html" "ELinks/0.12pre6 (textmode; Linux; 113x20-2)" "-"

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK