控制默认 Cache-Control:no-cache,私人 Nginx/php-fpm7.4 通过 Laravel Forge 服务安装

Controll over the default Cache-Control: no-cache, private Nginx/php-fpm7.4 installation via Laravel Forge service

是否有任何方法可以更改 Cache-Control header 的默认值,目前在所有 html 类型的文件上都设置为 no-cache, private

我曾尝试通过 nginx.conf 以及 Forge 面板更改此行为,但似乎对我没有任何作用。

除实际 html

之外,所有其他文件都按预期缓存
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/www.example.com/before/*;

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name www.example.com;
    server_tokens off;
    root /home/forge/www.example.com/public;

    # FORGE SSL (DO NOT REMOVE!)
    ssl_certificate /etc/nginx/ssl/www.example.com/824182/server.crt;
    ssl_certificate_key /etc/nginx/ssl/www.example.com/824182/server.key;

    ssl_protocols TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/dhparams.pem;
    
    add_header Last-Modified $date_gmt;
    if_modified_since off;
    etag off;
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";
    add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload' always;

    index index.html index.htm index.php;

    charset utf-8;

    # FORGE CONFIG (DO NOT REMOVE!)
    include forge-conf/www.example.com/server/*;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/www.example.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        try_files $query_string/index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
    
    # browser caching of static assets
    location ~* \.(ico|css|js|json|gif|jpeg|jpg|png|woff|ttf|otf|svg|woff2|eot)$ {   
        expires 30d;                                                                                            
        access_log off;
        add_header Pragma public;
        add_header Cache-Control "public, max-age=2592000";
    } 

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/www.example.com/after/*;

你能试试下面的配置吗?

map $uri $cache_control {
    ~\.html$  "public, max-age=2592000";
}

server {
    ...
    location / {
        add_header Cache-Control $cache_control;
        try_files $uri $uri/ /index.php?$query_string;
    }
    ...
    location ~ \.php$ {
        try_files $query_string/index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_hide_header Cache-Control;
        include fastcgi_params;
    }
    ...