最近公司商城的图片严重影响到了网站的正常访问,为了尽快解决这个问题,采用 图片动态生成的处理方式。这里用到了nginx的 http_image_filter_module 模块,安装nginx的时候需要配置上 --with-http_image_filter_module
同时,把原来放在项目跟目录下的图片通过 rsync + inotify 配置 对图片进行同步,同步到图片服务器
nginx 配置:
server { listen 80; server_name static.xxxx.com; location ~* /uploads/(.*)_(d+)x(d+).(jpeg|jpg|png|gif)$ { root /home/wwwroot; set $h $2; set $w $3; if ($h = '0'){ rewrite /uploads/(.*)_(d+)x(d+).(jpeg|jpg|gif|png)$ /uploads/$1.$4 last; } if ($w = '0') { rewrite /uploads/(.*)_(d+)x(d+).(jpeg|jpg|gif|png)$ /uploads/$1.$4 last; } #生成缩略图 image_filter resize $h $w; image_filter_buffer 5M; error_page 415 /uploads/nopicture.png; try_files /uploads/$1.$4 /uploads/nopicture.jpg; } location ~* /uploads/(.*)$ { root /home/wwwroot; } # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } #access_log /home/wwwlogs/static.cwbbc.com.log access; }
错误解决:
我遇到的情况是,通过域名访问 uploads 目录下少数图片 出现 403错误,查看nginx error.log 如下
[error] 1326#0: *8 open() "/home/wwwroot/uploads/2018/0126/eeae48c7a3c6edd7085d75f63afc497b.jpg" failed (13: Permission denied), client: 125.80.130.229, server: static.cwbbc.com, request: "GET /uploads/2018/0126/eeae48c7a3c6edd7085d75f63afc497b.jpg HTTP/1.1", host: "static.xxxx.com"
貌似是权限的问题,百度之后 说是 nginx的启动用户和运行用户不一致导致的 权限错误。
修改 nginx.conf 里面的 user 配置,我的配置默认是关闭的,没有设置启动用户,如此打开就是了
#user nobody; user root;
重启nginx
原来是这个问题