nginx 为什么不能反代图片? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
honmaple
V2EX    NGINX

nginx 为什么不能反代图片?

  •  
  •   honmaple 2017-03-14 12:44:11 +08:00 3702 次点击
    这是一个创建于 3216 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我的 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" 

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

    第 1 条附言    2017-03-14 16:23:51 +08:00
    谢谢大家的回答,找到原因了,原因在 google chrome ,不在 nginx,chrome 自动给 url 加了斜杠,我换成 firefox 后就行了,我原来的配置是可以的
    12 条回复    2017-03-14 21:37:57 +08:00
    arzusyume
        1
    arzusyume  
       2017-03-14 13:01:24 +08:00
    location /images {
    root /home/honmaple/storage/images/;
    honmaple
        2
    honmaple  
    OP
       2017-03-14 13:10:56 +08:00
    @arzusyume 这个我试过了,还是 404
    ryd994
        3
    ryd994  
       2017-03-14 13:26:19 +08:00 via Android   1
    求今后不要 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
        4
    yishanxin  
       2017-03-14 13:28:13 +08:00
    location /images/ {
    alias /home/honmaple/storage/images/;
    这么写的没问题
    yishanxin
        5
    yishanxin  
       2017-03-14 13:29:47 +08:00
    @ryd994 @honmaple 发完才看到楼上写乐,写的挺全的,忽略我的吧
    honmaple
        6
    honmaple  
    OP
       2017-03-14 13:39:13 +08:00
    @ryd994 好吧,以后用 example.com
    /home/honmaple/storage/images/admin/thumb/2017/03/148941736894194.png 文件是存在的,nginx 我用的是 root 账户,应该不存在权限的问题
    honmaple
        7
    honmaple  
    OP
       2017-03-14 13:41:17 +08:00
    148941736894194.png 是一个文件,不知道为什么会自动加斜杠
    mkdong
        8
    mkdong  
       2017-03-14 13:44:42 +08:00
    @ryd994 T_T 教室里点开吓死我了……
    gyorou
        9
    gyorou  
       2017-03-14 14:10:25 +08:00
    ```
    location ^~ /images/ {
    alias /home/honmaple/storage/images/;
    expires 1d;
    autoindex off;
    }
    ```
    honmaple
        10
    honmaple  
    OP
       2017-03-14 14:29:56 +08:00
    @gyorou 还是不行
    zerofiny
        11
    zerofiny  
       2017-03-14 15:12:52 +08:00
    location /images {
    alias /home/honmaple/storage/images;
    access_log /dev/null;
    expires 1d;
    }
    本地测试了下没问题
    BGYL
        12
    BGYL  
       2017-03-14 21:37:57 +08:00 via Android
    我的哥 搞事情 刚刚跟同学手机看了下 V2EX 一堆人围着。。。。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2385 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 05:34 PVG 13:34 LAX 21:34 JFK 00:34
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86