nginx 为什么不能反代图片?

2017-03-14 12:44:11 +08:00
 honmaple

我的 nginx 配置

    location ^~ /images/ {
        alias /home/honmaple/storage/images/;
        expires 1d;
    }
    location ~* ^/admin/(.*)/static/ {
        alias /home/honmaple/.virtualenvs/blog/lib/python3.4/site-packages/flask_admin/static/;
        access_log off;
        expires 1d;
    }
    location ~ ^/(api|admin)/ {
        proxy_pass http://127.0.0.1:8001;
        proxy_redirect     off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location / {
        return 403;
    }

图片路径类似这样 xxx.com/images/admin/thumb/2017/03/148941736894194.png

但是直接访问 url 会变成 xxx.com/images/admin/thumb/2017/03/148941736894194.png /, 多了一个斜杠,并且报错 404,
查看日志,发现是这样

2017/03/14 00:26:05 [error] 2288#2288: *15061 "/home/honmaple/storage/images/admin/thumb/2017/03/148941736894194.png/index.html" is not found (2: No such file or directory), client: 1.1.1.1, server: xxx.com, request: "GET /images/admin/thumb/2017/03/148941736894194.png/ HTTP/1.1", host: "xxx.com"

请教一下这是哪里的配置有问题

3292 次点击
所在节点    NGINX
12 条回复
arzusyume
2017-03-14 13:01:24 +08:00
location /images {
root /home/honmaple/storage/images/;
honmaple
2017-03-14 13:10:56 +08:00
@arzusyume 这个我试过了,还是 404
ryd994
2017-03-14 13:26:19 +08:00
求今后不要 xxx ,用 example 多好。建议大家找个人多的地方点开(逃

检查 /home/honmaple/storage/images/admin/thumb/2017/03/148941736894194.png 是否存在
然后 sudo -u nginx cat 一下看看 nginx 有没有权限
检查所有父目录, nginx 必须有 r 和 x 权限

先找找是谁发的跳转。 Nginx 只有当有对应路径的文件夹时才会自动发加斜杠 302

@arzusyume 你这就错了,楼主的 alias 写法没错,或者
location /images/ {
root /home/honmaple/storage/;
yishanxin
2017-03-14 13:28:13 +08:00
location /images/ {
alias /home/honmaple/storage/images/;
这么写的没问题
yishanxin
2017-03-14 13:29:47 +08:00
@ryd994 @honmaple 发完才看到楼上写乐,写的挺全的,忽略我的吧
honmaple
2017-03-14 13:39:13 +08:00
@ryd994 好吧,以后用 example.com
/home/honmaple/storage/images/admin/thumb/2017/03/148941736894194.png 文件是存在的,nginx 我用的是 root 账户,应该不存在权限的问题
honmaple
2017-03-14 13:41:17 +08:00
148941736894194.png 是一个文件,不知道为什么会自动加斜杠
mkdong
2017-03-14 13:44:42 +08:00
@ryd994 T_T 教室里点开吓死我了……
gyorou
2017-03-14 14:10:25 +08:00
```
location ^~ /images/ {
alias /home/honmaple/storage/images/;
expires 1d;
autoindex off;
}
```
honmaple
2017-03-14 14:29:56 +08:00
@gyorou 还是不行
hasdream
2017-03-14 15:12:52 +08:00
location /images {
alias /home/honmaple/storage/images;
access_log /dev/null;
expires 1d;
}
本地测试了下没问题
BGYL
2017-03-14 21:37:57 +08:00
我的哥 搞事情 刚刚跟同学手机看了下 V2EX 一堆人围着。。。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/347337

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX