V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
honmaple
V2EX  ›  NGINX

nginx 为什么不能反代图片?

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

    我的 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 还是不行
    hasdream
        11
    hasdream  
       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   ·   我们的愿景   ·   实用小工具   ·   3613 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 04:36 · PVG 12:36 · LAX 21:36 · JFK 00:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.