2012-07-03 60 views
2

是否可以在nginx中設置內容類型相關的過期標題?我對nginx很陌生,並嘗試以下方法:內容類型相關的nginx過期標題

location ~ /files\.php$ { 
    ... 
      if ($content_type = "text/css") { 
        add_header X-TEST1 123; 
        expires 7d; 
      } 
      if ($content_type = "image/png") { 
        add_header X-TEST2 123; 
        expires 30d; 
      } 
      if ($content_type = "application/javascript") { 
        add_header X-TEST3 123; 
        expires 1d; 
      } 
      #testing 
      if ($content_type != "text/css") { 
        add_header X-TEST4 abc; 
      } 
      #testing 
      if ($content_type = text/css) { 
        add_header X-TEST5 123; 
      } 
    } 

但是,所有請求中添加的唯一頭是「X-TEST4」。 我知道其他使用解決方案文件擴展名:

location ~* \.(ico|css|js|gif|jp?g|png)\?[0-9]+$ 

但它並不適用於我的應用程序。

回答

0

我認爲的代碼應在location/{ }代替location ~ /files\.php$ { } ...

+0

沒有,這不物。請參閱http://wiki.nginx.org/HttpCoreModule#location – dompie

0

你試過

$content_type ~= application/javascript 

此外,請務必閱讀本: http://wiki.nginx.org/IfIsEvil

+0

在/etc/nginx/sites-enabled/a.localhost:49中重新啓動nginx:nginx:[emerg]意外的「〜=」 –