nginx 浏览器缓存指令带来 404 not found

标签 nginx browser-cache

我想为图片实现浏览器缓存的网站之一 在 virtual.conf 上有这个特定的服务器 {} block

server {

    listen       80;
    server_name www.example.net  example.net;

    location / {
        root   /var/www/example.net/public_html;
        index  index.html index.htm index.php;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /var/www/example.net/public_html;
    }

    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /var/www/example.net/public_html;
    }

    location ~*  \.(jpg|jpeg|png|gif)$ {
       expires 180d;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        root           /var/www/example.net/public_html;
        error_log      /var/www/example.net/public_html/error.log error;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    } 
}

通常是这个指令

    location ~*  \.(jpg|jpeg|png|gif)$ {
       expires 180d;
    }

应该负责在用户端缓存图像。但是当我访问时 我网站上的图片我收到 404 未找到。我不确定我为什么会这样。

是的。添加指令后,我重新加载/重新启动了 nginx。

图像位于如下子文件夹中:
example.net/images/dir1/user_photos/photo.jpg
或者
example.net/images/dir1/user_photos/photo.jpg

如有任何帮助,我们将不胜感激。 谢谢。

最佳答案

根参数应该在位置指令之外。

将 root 放在内部位置不是最佳做法

改变

 location / {
        root   /var/www/html;
        index index.php  index.html index.htm;
        try_files $uri $uri/ /index.php?$args;
 } 

收件人:

 root   /var/www/html;
 location / {
        index index.php  index.html index.htm;
        try_files $uri $uri/ /index.php?$args;
 }

关于nginx 浏览器缓存指令带来 404 not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33456681/

相关文章:

html - CSS - 为什么我的浏览器不能反射(reflect)在服务器上所做的任何更改?

nginx - Kubernetes Nginx : How to have zero-downtime deployments?

Symfony 2 : 404 Not Found Error when tryes to open/app_dev. PHP

unix - 如何在不丢失任何请求的情况下升级Nginx?

nginx - 如何为 REST API 正确配置 Nginx 缓存?

caching - 如何在 Sitecore 中配置 Etag?

node.js - 将没有端口号的域名链接到 nginx 托管的 MEAN stack 服务器

firefox - 从十六进制格式解码 Firefox 浏览器缓存条目

http - 多个实体的 If-None-Match 有什么用?

html - 一小部分用户加载几天前的 HTML?