通过 Nginx 从 Mongo GridFS 提供动态内容,不会出现 404 错误

发布于 2024-12-21 19:27:29 字数 834 浏览 2 评论 0原文

我在 CodeIgniter 中有一个动态内容控制器,可以从 GridFS 中提取图像。服务器正在运行 nginx,我尝试在 nginx 配置中设置缓存控制标头,以缓存此动态内容控制器提供的图像 7 天。我在 nginx 配置中正确设置了配置,但我从 nginx 收到 404 标头,因为这些文件实际上并不存在于服务器上。

我的缓存控制指令如下:

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires 7d;
    log_not_found off;
}

log_not_found 帮助防止 nginx 记录 404 错误,但发送到浏览器的标头仍然是 404 错误。我尝试通过 php 的“header”函数手动设置标头,但是因为 nginx 使用 php-fpm,所以它做了一些奇怪的事情。

任何人都可以为我指明如何针对这种情况正确设置缓存控制标头的正确方向吗?谢谢大家 =)

更新:

我更改了 nginxconf,为所有静态文件和动态控制器指定了一个特殊位置。

location ~ ^/(dres|js|css|art)/ {
    access_log off;
    expires 7d;
    add_header Cache-Control public;
    try_files $uri $uri/ /index.php?$args;
}

Nginx 正在静态文件上设置正确的过期标头,但我一生都无法让 fastcgi 和 nginx 为动态输出图像输出过期标头。我的 fastcgi 配置中一定缺少一些内容,以便在提供 php 文件时允许过期标头。

I have a dynamic content controller in CodeIgniter that pulls images from GridFS. The server is running nginx and I am trying to set the cache control headers in my nginx config to cache the images served by this dynamic content controller for 7 days. I have the config set correctly in my nginx config, but I am getting 404 headers from nginx because the files do not physically exist on the server.

My cache control directive is as follows:

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires 7d;
    log_not_found off;
}

log_not_found helps keep nginx from logging the 404 error, but the headers that are sent to the browser are still 404 errors. I tried setting the headers manually via php's "header" function, but because nginx is using php-fpm, it was doing some weird stuff.

Can anyone point me in the correct direction on how to get my cache control headers set up properly for this situation? Thanks everyone =)

UPDATE:

I changed my nginx conf with a special location for all my static files and my dynamic controller.

location ~ ^/(dres|js|css|art)/ {
    access_log off;
    expires 7d;
    add_header Cache-Control public;
    try_files $uri $uri/ /index.php?$args;
}

Nginx is setting the correct expires headers on the static files, but I cannot for the life of me get fastcgi and nginx to output the expires headers for the dynamically output images. I must be missing something in my fastcgi config to allow expiration headers when serving php files.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

年少掌心 2024-12-28 19:27:29

你不是应该为此设置 fastcgi_cache 吗?

Aren't you supposed to set fastcgi_cache for that?

我爱人 2024-12-28 19:27:29

解决了大部分。意识到使用 php 的“header”功能是有效的,但还有其他问题让我认为它不起作用。我刚刚将其添加到我的动态图像控制器中:

// seconds, minutes, hours, days
$expires = (60*60*24*7);
header("Pragma: public");
header("Cache-Control: maxage=".$expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');       

现在至少到期时间可以像我想要的动态图像一样工作。我还没有弄清楚如何指定静态文件的过期时间而不在​​这些动态图像上得到 404,但目前这更好。

Solved for the most part. Realized that using php's "header" function was working, there were other issues which were making me think it was not. I just added this to my dynamic image controller:

// seconds, minutes, hours, days
$expires = (60*60*24*7);
header("Pragma: public");
header("Cache-Control: maxage=".$expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');       

Now at least the expiration is working like I want for the dynamic images. I haven't figured out how to specify expiration for static files without getting a 404 on these dynamic images, but this is better for now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文