V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
xiaoz
1.03D
V2EX  ›  NGINX

nginx 反向代理替换图片的问题

  •  
  •   xiaoz ·
    helloxz · May 28, 2016 · 7952 views
    This topic created in 3623 days ago, the information mentioned may be changed or developed.

    a.com 中包含一张图片 b.com ,我用 c.com 去反代 a.com ,但是想把反代后的结果调用的图片 b.com 替换为其它图片,使用subs_filter b.com/123.jpg c.com/456.jpg;不起作用,请问应该怎样替换 a.com 域名中的图片地址 b.com

    Supplement 1  ·  May 28, 2016

    完整配置文件如下,需求是将http://search.xiaoz.me中包含的图片http://pic.bsdev.cn替换为HTTPS:https://pic.bsdev.cn

    server
    {
            listen 443;
            server_name so.xiaoz.me;
            ssl on;
            ssl_certificate /etc/letsencrypt/live/so.xiaoz.me/fullchain.pem;
            ssl_certificate_key /etc/letsencrypt/live/so.xiaoz.me/privkey.pem;
            location / {
                    proxy_redirect off;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_set_header X-Forwarded-Proto https;
                    proxy_pass http://search.xiaoz.me;
                    subs_filter_types text/html text/css text/xml;
                    subs_filter http://pic.bsdev.cn https://pic.bsdev.cn;
            }
    }
    server
    {
            listen 80;
            server_name so.xiaoz.me;
            rewrite ^(.*) https://so.xiaoz.me$1 permanent;
    }
    
    Supplement 2  ·  May 28, 2016

    此贴终结,无法使用subs_filter替换的原因是开启了Gzip导致,6楼回复正解。在location中加入proxy_set_header Accept-Encoding "";,重启nginx后完美解决。被坑了几个小时

    17 replies    2016-05-29 20:01:31 +08:00
    SeeMeTomorrow
        1
    SeeMeTomorrow  
       May 28, 2016 via iPhone
    建议把完整配置文件贴上来
    lslqtz
        2
    lslqtz  
       May 28, 2016
    subs?不是 sub 嘛?
    xiaoz
        3
    xiaoz  
    OP
       May 28, 2016
    @SeeMeTomorrow 您好,完整配置文件已经贴上来了,谢谢。
    xiaoz
        4
    xiaoz  
    OP
       May 28, 2016
    @lslqtz 不知道新版本中是否将 sub 改成了 subs ,因为我使用 sub 的时候报语法错误,根据官方的帮助文档: https://www.nginx.com/resources/wiki/modules/substitutions/?highlight=subs_filter 来看的话也是使用的 subs
    lslqtz2
        5
    lslqtz2  
       May 28, 2016
    @xiaoz 应该是我们模块不同?不清楚,我这边保存的文档说是 sub 测试有效
    http://www.ttlsa.com/linux/nginx-modules-ngx_http_sub_module/
    tammy
        6
    tammy  
       May 28, 2016   ❤️ 2
    ```
    proxy_set_header Accept-Encoding "";#关闭 Gzip 是 subs_filter 能正常工作
    ```

    来自: http://blog.iplayloli.com/nginx-reserve-proxy-google-gravatar.html

    话说怎么高亮代码?
    Tink
        7
    Tink  
    PRO
       May 28, 2016 via iPhone
    我感觉没什么问题
    xiaoz
        8
    xiaoz  
    OP
       May 28, 2016
    @Tink 但实际没有替换成功
    xiaoz
        9
    xiaoz  
    OP
       May 28, 2016
    @tammy V2 的回复不支持 markdown ,所以高亮不了,发表主题的话是支持 markdown 的
    xiaoz
        10
    xiaoz  
    OP
       May 28, 2016
    @lslqtz2 你 nginx 哪个版本的呢?我是按照官方文档安装的那个模块
    xiaoz
        11
    xiaoz  
    OP
       May 28, 2016
    @tammy 感谢回复,终于排查到原因就是因 Gzip 影响导致 subs_filter 不起作用,关闭 Gzip 后终于正常了,非常感谢。
    lslqtz
        12
    lslqtz  
       May 28, 2016
    @xiaoz 最新。
    lslqtz
        13
    lslqtz  
       May 28, 2016
    @tammy 忘了 gzip ,我也来感谢一下。
    @xiaoz 我表示不关 gzip 也能用,所以就没有在意。
    fakefish
        14
    fakefish  
       May 29, 2016
    图片就不要 gzip 了嘛 没用
    xiaoz
        15
    xiaoz  
    OP
       May 29, 2016 via iPhone
    @fakefish 不是图片 gzip 是整个网页中用了,导致 subs_filter 对整个网页不起作用。
    Daniel65536
        16
    Daniel65536  
       May 29, 2016   ❤️ 1
    proxy_set_header Accept-Encoding ""浪费流量。
    应该上 ngx_http_gunzip_module ,用 proxy_pass 来中转下就好,可以大幅度节约流量开支。
    xiaoz
        17
    xiaoz  
    OP
       May 29, 2016 via iPhone
    @Daniel65536 好的,多谢指点
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   972 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 55ms · UTC 20:09 · PVG 04:09 · LAX 13:09 · JFK 16:09
    ♥ Do have faith in what you're doing.