php图片 nginx 缓存时间设置,Nginx设置缓存expires后返回404找不到文件的原因

Nginx服务器想设置一些文件(图片、js、css等)的浏览器缓存日期,但一旦在配置文件nginx.conf里加上expires的语句,图片就不能显示。expires语句的写法应该没错,因为其他博主使用是有效的。这是怎么回事?

下面是nginx.conf代码:

server {

listen 80;

server_name example.com

location / {

root    /data/file/static/blogourl;      index   index.html index.htm index.php;

}

location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {      expires 30d;

add_header Pragma public;

add_header Cache-Control "public";

}

location ~* \.php$ {  ssi on;

root /data/file/static;

fastcgi_param HTTP_USER_AGENT  $http_user_agent;

fastcgi_index   index.php;

#fastcgi_pass    127.0.0.1:9000;

#fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;

fastcgi_pass   unix:/var/run/php-fpm.sock;

include         fastcgi_params;

fastcgi_param   SCRIPT_FILENAME    /data/file/static/blogourl$fastcgi_script_name;

fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;

}

}

有网友认为,这不是语法错误,而是逻辑错误。Nginx的location是单独的,所以上例里静态文件没有root指向,这个时候root会被认为是默认的路径(/etc/nginx/html或其他),这样,当然就没有这些文件了,图片当然也不会有显示了。

通过这样解释,上面代码改为:

root    /data/file/static/blogourl;  location / {

index   index.html index.htm index.php;

}

location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {

expires 30d;

add_header Pragma public;

add_header Cache-Control "public";

}

location ~* \.php$ {

ssi on;

root /data/file/static;

fastcgi_param HTTP_USER_AGENT  $http_user_agent;

fastcgi_index   index.php;

#fastcgi_pass    127.0.0.1:9000;

#fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;

fastcgi_pass   unix:/var/run/php-fpm.sock;

include         fastcgi_params;

fastcgi_param   SCRIPT_FILENAME    /data/file/static/blogourl$fastcgi_script_name;

fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;

}

这样,就不会有问题了。

相关文章

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值