5

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

 2 years 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.
neoserver,ios ssh client

例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)" "-"

Recommend

  • 43
    • www.qikqiak.com 5 years ago
    • Cache

    ingress-nginx 中 Rewrite 的使用

    由于 nginx 的优秀性能表现,所以很多企业在Kubernetes 中选择 Ingress Controller 的时候依然会选择基于 nginx 的 ingress-nginx ,前面文章中我们更多的是介绍更加云原生配...

  • 40

    rewrite会改变浏览器的URL链接,把原来的URL转发到新的URL上 try_file会转发URL到nginx代理的新的URL上,转发的同时,会以新的URL对应的location中的response为准,不会改变浏览器的URL proxy_pass会转发URL到upstream对应...

  • 8

    Mod rewrite to delete the file name of the url advertisements If I have a file, let's say rec-view.php, then I can call it by this URL...

  • 6
    • www.liurongxing.com 3 years ago
    • Cache

    nginx 301跳转到带www域名方法rewrite

    nginx 301跳转到带www域名方法rewrite 作者: JavasBoy 分类: Nginx 发布时间: 2012-04-2...

  • 5

    nginx 利用 rewrite 屏蔽IE浏览器 浏览:1716次  出处信息    搞过前端的估计都碰到最头疼的问题就是浏览器兼...

  • 7

    风之优雅z ...

  • 4
    • mmenozzi.github.io 2 years ago
    • Cache

    Crazy Magento URL rewrite generation

    Crazy Magento URL rewrite generation A couple of weeks ago I experienced a Magento URL rewrite behaviour which I wasn’t aware of. Considering that I work with Magento since 2010,...

  • 7
    • blog.gslin.org 2 years ago
    • Cache

    Kagi 的 url rewrite

    Kagi 的 url rewrite Kagi 在上個禮拜推出了 url rewrite 的功能,可以把搜尋結果裡面的網址換掉:(在「

  • 6
    • gist.github.com 2 years ago
    • Cache

    URL rewrite on ExpressJS 4.0

    URL rewrite on ExpressJS 4.0 · GitHub Instantly share code, notes, and snippets. URL rewrite on ExpressJS 4.0 ...

  • 5

    Rewrite is a Go package that lets you rewrite URL paths, subdomains or hosts based on regular expressions. It is inspired by Apache's mod_rewrite module and can be used as a middleware for net/...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK