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 

Nginxconfiguration:

 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; } } 

以下应该显示设置在同一位置块中的过期标题…

 try_files $uri $uri/ /$1/$2 =404; 

根据Nginx wiki, try_files应该以URI或状态码结束。 如果您在Nginx中启用debugging,您可能会得到更多关于未设置过期的信息。

更新

想法为什么到期的标题不被传递?

我启用了debugging来弄清楚这个有趣的情况。 这是我发现的…

以下是之前链接的try_files wiki文章的直接引用。

如果找不到任何文件,则将内部redirect到最后一个参数中指定的uri。

所以,当你使用下面的代码…

 try_files $uri $uri/ /$1/$2; 

nginx找不到$uri$uri/ (在你的情况下/css/v1/afile.css和/css/v1/afile.css/),内部redirect到在最后一个参数中指定的URI。 所以,在内部redirect后,find位置/$1/$2 (在你的情况/css/afile.css), 在另一个位置块 。 由于它是由另一个没有任何过期的位置块执行的,所以你没有看到过期。