【问题标题】:Nginx rule for redirecting all image files用于重定向所有图像文件的 Nginx 规则
【发布时间】:2015-12-07 18:24:02
【问题描述】:

我是 nginx 新手。我想将网站的假图像路径重定向到原始图像路径。例如,假设我在 example.com 主目录上有很多图像,例如 example.com/a.jpeg、example.com/b.png 和 example.com/c.jpg。如果有人调用 example.com/tests/a.jpeg,那么它应该重定向到 example.com/a.jpeg。

所以我想写一个重写规则,将图像文件重定向到原始位置。

【问题讨论】:

    标签: php nginx rewrite


    【解决方案1】:

    因此,层次结构的任何目录中的图像文件实际上应该位于文档根目录的顶部。所以URI/path/to/image.jpeg映射到物理位置$document_root/image.jpeg

    这很容易通过regex location 结合try_files 来实现:

    root /path/to/document/root;
    
    location ~* ([^/]+\.(?:png|jpeg|jpg))$ {
        try_files /$1 =404;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-12-03
      • 2017-02-04
      • 2011-10-15
      • 2015-12-22
      • 2017-08-28
      • 2011-08-24
      • 1970-01-01
      • 1970-01-01
      • 2019-10-23
      相关资源
      最近更新 更多