2

nginxでコンテンツタイプに依存するExpiresヘッダーを設定することは可能ですか? 私は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]+$

しかし、それは私のアプリケーションには当てはまりません。

4

3 に答える 3

0

やってみました

$content_type ~= application/javascript

また、必ずこれを読んでください: http://wiki.nginx.org/IfIsEvil

于 2012-07-20T16:13:44.137 に答える
0

lua モジュールがインストールされている場合は、次のようなことができます。

server {

    location / {...}

    header_filter_by_lua_block {

            local cct = {}  -- # cached content types

            cct["text/css"] = true
            cct["application/javascript"] = true
            cct["application/x-javascript"] = true
            cct["text/javascript"] = true
            cct["image/jpeg"] = true
            cct["image/png"] = true
            cct["application/vnd.ms-fontobject"] = true
            cct["application/font-woff"] = true
            cct["application/x-font-truetype"] = true
            cct["image/svg+xml"] = true
            cct["application/x-font-opentype"] = true

            if cct[ngx.header.content_type] ~= nil and ngx.header["expires"] == nil then
                    local now = os.time()
                    local expires = os.date("%a, %d-%b-%Y %H:%I:%S GMT", now+604800) -- # one week in seconds

                    ngx.header["expires"] = expires
            end
    }
}

指定された content-type を持つ応答は、expires-header を取得するようになりました。応答に既に expires-header がある場合、lua ブロックはヘッダーに触れません。

于 2018-06-21T20:21:46.190 に答える
0

私はコードがlocation / { }代わりにあるべきだと思うlocation ~ /files\.php$ { }...

于 2012-07-03T13:38:50.337 に答える