nginx 尝试文件,如果未找到,则回退到重写不起作用(重定向循环)

nginx 尝试文件,如果未找到,则回退到重写不起作用(重定向循环)

我有两种情况:

  • /media/cache/test.jpgstylesheet/theme/xxxx.css由应用程序生成。它不是文件系统上存在的静态文件。

  • /assets/test.jpg是主机上存在的静态文件

我希望它们两个都能被浏览器添加expires 6M标头缓存。

所以我做了:

location / {
    try_files $uri @rewriteapp;
}

location @rewriteapp {
     rewrite ^(.*)$ /index.php/$1 last;
}

location ~ ^/(index)\.php(/|$) {
    fastcgi_pass unix...;
    internal;
}

location ~ \.php$ {
    return 404;
}

# assets, media
location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif)$ {
    expires 6M;
    access_log off;

    # try accessing the file directly, and if not found 
    # it means the application has to generate it, 
    # so reroute to the @rewriteapp rule.
    try_files $uri @rewriteapp;
}

问题是 nginx 检测到循环:

*194369 rewrite or internal redirection cycle while redirect to named location "@rewriteapp", client: 127.0.0.1, server: , request: "GET /stylesheet/theme/3441-1484735061.css

请问我遗漏了什么?

答案1

好的,替换try_files $uri @rewriteapp;似乎try_files $uri /index.php$is_args$args;可以解决问题。

相关内容