我想为图片实现浏览器缓存的网站之一 在 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/