如何在 Laravel 中强制缓存标头

问题描述 投票:0回答:1

我有一个文件,我通过这样的路线调用:

Route::group([
    'middleware' => ['cache.headers:public;max_age=600'],
], function () {
    Route::get('/my-file', function ($locale) {
        //some logic

        $contents = 'my file content';
        $response = \Response::make($contents, 200);

        $response->header('Cache-Control', 'public, max-age=600');
        $response->header('Expires', now()->addSeconds(600)->toRfc7231String());
        $response->header('Content-Type', 'application/javascript');
        $response->header('Pragma', '');

        return $response;
    });
});

如您所见,我正在应用中间件并传递标头,因为我需要此文件可在设备上缓存,而不是每次从服务器返回时都提供服务。

在我的本地主机上,这有效:

Cache-Control:
max-age=600, public
Connection:
close
Content-Type:
application/javascript
Date:
Thu, 08 Feb 2024 19:23:55 GMT, Thu, 08 Feb 2024 19:23:55 GMT
Expires:
Thu, 08 Feb 2024 19:33:55 GMT
Host:
127.0.0.1:8000
Pragma:

但是当我部署到在 nginx 服务器上运行的实时环境时,我得到:

Cache-Control:
no-store, no-cache, must-revalidate
Cache-Control:
max-age=600, public
Connection:
keep-alive
Content-Encoding:
gzip
Content-Type:
application/javascript; charset=utf-8
Date:
Thu, 08 Feb 2024 19:17:21 GMT
Expires:
Thu, 19 Nov 1981 08:52:00 GMT
Expires:
Thu, 08 Feb 2024 19:27:21 GMT
Pragma:
no-cache
Pragma:
Server:
nginx/1.14.0 (Ubuntu)

我尝试调整 nginxconf 中的位置以在那里设置标头,但无济于事,仍然是同样的事情,我不知道如何解决这个问题。任何帮助将不胜感激,谢谢。

laravel nginx http-headers browser-cache
1个回答
0
投票

看起来您有 2 个过期标头;一项由 Laravel 设置,一项由 nginx 设置。

您需要更改 nginx 配置以不设置过期标头。

© www.soinside.com 2019 - 2024. All rights reserved.