2013-05-10 126 views
0

我有一個腳本,每次部署我的網站時都會增加一個全局修訂號。然後將該數字映射到加載CSS,JavaScript和精靈資源的HTML中。這被用作緩存清除策略。nginx中的通配符請求/位置?

e.g <link rel="stylesheet" href="/css/screen_r123.css" type="text/css" />

在Apache中,我會重寫這些遞增的URL回這樣的實際資產:

RewriteRule ^css/screen_r(.*).css$ /css/screen_min.css [L] 

我怎麼會做同樣的nginx的?我不確定在哪裏放置正則表達式匹配邏輯。

注意:我不想將查詢?r=123附加到URI的末尾,因爲它將查詢傳遞給靜態資產感覺不正確,加上我在不包含查詢的Varnish代理後面在它的緩存哈希


這是我爲我的網站目前的nginx的conf:

server { 
    listen 8080; 
    server_name domain.com www.domain.com 
    port_in_redirect off; 

    root /usr/share/nginx/mydomain.com/public; 
    index index.html index.php; 

    #set long expiry for assets 
    location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ { 
    expires max; 
    log_not_found off; 
    } 

    location/{ 
    #FOLLOWING LINE DOES NOT WORK AS INTENDED 
    rewrite ^/css/screen_r(.*).css$ /css/screen.css last; 

    # Check if a file or directory index file exists, else route it to index.php. 
    try_files $uri $uri/ /index.php; 
    } 

    location ~* \.php$ {  
    fastcgi_pass unix:/tmp/php5-fpm.sock; 
    fastcgi_index index.php; 
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
    include  fastcgi_params; 
    fastcgi_param APPLICATION_ENV "production"; 
    fastcgi_split_path_info ^(.+.php)(.*)$; 
    } 

    add_header "X-UA-Compatible" "IE=Edge,chrome=1"; 
    add_header Cache-Control "public max-age=60"; 
} 

回答

0

解決。重寫指令需要在任何location塊之外。

這工作:

server { 
    listen 8080; 
    server_name domain.com www.domain.com; 
    port_in_redirect off; 

    rewrite ^/css/screen_r(.*).css$ /css/screen.css last; 
    ...