繁体   English   中英

nginx execute.php as.gif文件改写规则

[英]nginx execute .php as .gif file rewrite rules

访问这个对我来说是一个很大的挑战:

mysite.com/cookie/image.php?perma=perma1

像这样;

mysite.com/cookie/perma1.gif

或者

mysite.com/cookie/image.php?perma=perma1

作为

mysite.com/cookie/perma2.gif

因此,每当我在后台请求perma1.gif or perma2.gif时,nginx 都会运行

image.php?perma=perma1

image.php?perma=perma2 

分别。

因此,对于您的问题,您的 nginx server.conf 的正确规则应该是:

location ~* ^/cookie/(.*)\.(gif)$ {
    try_files $uri $uri/ /cookie/image.php?perma=$1;
}

这条规则意味着:

  1. 只能在子文件夹 /cookie/ 中使用
  2. 仅由 gif 文件类型使用
  3. 尝试是否存在 gif
  4. 如果不重定向到 cookie/image.php
  5. 并将 gif 文件名作为 GET perma

...这应该可以解决您的问题。

因此,每当我在后台请求 perma1.gif 或 perma2.gif 时,nginx 都会运行

这意味着try_files是完全没有必要的,不仅@MatWer 的答案总是会尝试检查例如/cookie/perma1.gif的存在,而且还会检查目录/cookie/perma1.gif/的存在(这是两个不必要的系统stat调用)。

最有效的是放置在server上下文中的简单重写(在任何location之外):

rewrite ^/cookie/(.*)\.gif$ /cookie/image.php?perma=$1 last;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM