Linux
NGINX 沒有一致地設置 Cache-Control 標頭
我無法讓 NGINX 根據 mime 類型正確發送記憶體控制標頭。它適用於某些類型,但不適用於其他類型。我錯過了什麼?
在我的
site.conf
文件中,我有:map $sent_http_content_type $expires { default off; text/html epoch; text/css 1h; text/javascript 1h; application/javascript 1h; # ~image/ 1h; image/webp 1h; image/png 2h; ~font/ 1h; } server { server_name ...; ... root /var/www/site; index index.html; expires $expires; location / { proxy_pass http://127.0.0.1:5001; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } ... }
使用圖像效果很好。它也適用於
~image/
模式。curl -I https://site.tld/images/image.webp HTTP/2 200 server: nginx/1.18.0 (Ubuntu) date: Thu, 22 Sep 2022 14:37:16 GMT content-type: image/webp content-length: 9488 accept-ranges: bytes expires: Thu, 22 Sep 2022 15:37:16 GMT cache-control: max-age=3600 strict-transport-security: max-age=2628000; includeSubDomains $ curl -I https://site.tld/images/image.png HTTP/2 200 server: nginx/1.18.0 (Ubuntu) date: Thu, 22 Sep 2022 14:37:25 GMT content-type: image/png content-length: 53676 accept-ranges: bytes expires: Thu, 22 Sep 2022 16:37:25 GMT cache-control: max-age=7200 strict-transport-security: max-age=2628000; includeSubDomains
但它拒絕使用 CSS 或 javascript…
$ curl -I https://site.tld/css/bootstrap.min.css HTTP/2 200 server: nginx/1.18.0 (Ubuntu) date: Thu, 22 Sep 2022 14:43:39 GMT content-type: text/css; charset=utf-8 content-length: 162264 vary: Accept-Encoding accept-ranges: bytes strict-transport-security: max-age=2628000; includeSubDomains $ curl -I https://site.tld/js/bootstrap.min.js HTTP/2 200 server: nginx/1.18.0 (Ubuntu) date: Thu, 22 Sep 2022 14:43:53 GMT content-type: text/javascript; charset=utf-8 content-length: 62563 vary: Accept-Encoding accept-ranges: bytes strict-transport-security: max-age=2628000; includeSubDomains
該應用程序是一個已編譯的 Go 應用程序,其中包含編譯並通過代理提供的圖像資產。但是所有的資源都被編譯進去了,圖片和 CSS 等等。那麼為什麼它對某些類型有效,而對其他類型無效呢?
您嘗試設置記憶體的內容類型具有字元集。這也應該是規則的一部分:
map $sent_http_content_type $expires { default off; text/html epoch; text/css 1h; text/javascript 1h; application/javascript 1h; # ~image/ 1h; image/webp 1h; image/png 2h; ~font/ 1h; "text/ccs; charset=utf-8" 1h; "text/javascript; charset=utf-8" 1h; }
否則它將落入
default
設置之下。