Nginx:过期标头不起作用

iDe*_*247 2 nginx

我的expires标题字段有问题。以下 nginx 规则给了我这个标题(没有过期标题)。为什么不通过过期标头的想法?

头文件:/css/v1/afile.css

HTTP/1.1 200 OK
Server: nginx
Date: Sat, 14 Sep 2013 07:29:59 GMT
Content-Type: text/css
Content-Length: 12548
Last-Modified: Sat, 11 May 2013 11:05:51 GMT
Connection: keep-alive
Accept-Ranges: bytes
Run Code Online (Sandbox Code Playgroud)

Nginx 配置:

server {
    listen 80 default_server;
    server_name _;
    root    /var/www/apps/myapp/public/app/webroot;
    index   index.php index.html index.htm;

    server_tokens off;

    access_log  /var/www/apps/myapp/logs/access.log;
    error_log   /var/www/apps/myapp/logs/error.log;

    client_max_body_size 20M;

    rewrite_log on;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ /(js|css)/v[0-9]+/(.*) {
        access_log off;
        expires 7d;
        add_header Cache-Control public;
        try_files $uri $uri/ /$1/$2;
    }

    # Pass the PHP scripts to FastCGI server
    location ~ \.php$ {
        fastcgi_pass   backend;
        fastcgi_index  index.php;
        fastcgi_intercept_errors on; # to support 404s for PHP files not found
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
Run Code Online (Sandbox Code Playgroud)

Pot*_*thu 5

以下应该可以显示在同一位置块中设置的 expires 标头...

try_files $uri $uri/ /$1/$2 =404;
Run Code Online (Sandbox Code Playgroud)

根据 Nginx wiki,try_files应该以 URI 或状态代码结尾。如果您在 Nginx 中启用调试,您可能会获得有关未设置过期原因的更多信息。

更新

为什么不通过过期标头的想法?

我启用了调试以找出这个有趣的情况。这是我发现的...

这是之前链接的 try_files wiki 文章的直接引述...

如果没有找到任何文件,则内部重定向到最后一个参数中指定的 uri。

因此,当您使用以下代码时...

try_files $uri $uri/ /$1/$2;
Run Code Online (Sandbox Code Playgroud)

nginx 找不到$uri并且$uri/(在您的情况下为 /css/v1/afile.css 和 /css/v1/afile.css/),会进行到最后一个参数中指定的 URI 的内部重定向。因此,在内部重定向之后/$1/$2在另一个 location 块中找到了该位置(在您的情况下为 /css/afile.css)。因为它是由另一个没有任何过期的位置块执行的,所以您没有看到过期。