Nginx Proxy如何使用 sub_filter 模块替换 POST 请求中响应的 data 数据 中的字符串?

代理服务器:阿里云;
代理目标(网站存放):AWS EC2 和 S3,地址都在美东;
由于网站里显示的图片资源类都是s3给的url链接,因此在代理过程中需要使用sub_filter 模块来替换url。

以下是我的代理Nginx配置:

proxy_cache_path /var/nginx/cache/aws/eladeanode levels=2:2:2 use_temp_path=off keys_zone=aws_2:500m inactive=30d max_size=10g;

server {
    listen 80;
    server_name eladeanode.trueniu.com;

    location ~*/wp-content/uploads/(.*)\.(jpg|jpeg|png|gif)$ {

        set $s3_bucket          'adwords.yeahmobi.com.s3.amazonaws.com';
    
         add_header         x-by "aws";
        proxy_http_version    1.1;
        proxy_set_header    Host $s3_bucket;
        proxy_set_header    Authorization '';
        proxy_hide_header    x-amz-id-2;
        proxy_hide_header    x-amz-request-id;
        proxy_hide_header    Set-Cookie;
        proxy_ignore_headers    "Set-Cookie";
        proxy_buffering        off;
        proxy_intercept_errors    on;
        proxy_read_timeout     600s;
    
        # config cache
        proxy_cache             aws_2;
        proxy_cache_valid       any 1m;
        proxy_cache_valid       200 302 7d;
        add_header              X-Cached $upstream_cache_status;
        proxy_cache_lock on;
        proxy_buffer_size 128k;
        proxy_buffers 200 128k;

        resolver                8.8.8.8 valid=300s;
        resolver_timeout        10s;
    
        proxy_pass         http://$s3_bucket$request_uri;

    }

    location ~*/wp-content/themes/(.*)\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        add_header              x-by "aws";
        proxy_pass              http://google-adwords-1346230333.us-east-1.elb.amazonaws.com;
        proxy_redirect          off;
        proxy_set_header        Host $http_host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header         X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header         Accept-Encoding "";
        proxy_connect_timeout   60;
        proxy_read_timeout      600;
        proxy_send_timeout      600;

        add_header Pragma public;
        add_header Cache-Control "public";
        log_not_found off;
    
        # config cache
        proxy_cache             aws_2;
        proxy_cache_valid       any 1m;
        proxy_cache_valid       200 302 7d;
        add_header              X-Cached $upstream_cache_status;
        proxy_cache_lock on;
        proxy_buffer_size 128k;
        proxy_buffers 200 128k;

        resolver                8.8.8.8 valid=300s;
        resolver_timeout        10s;
    }

    location / {
        proxy_pass              http://google-adwords-1342430333.us-east-1.elb.amazonaws.com;
        proxy_redirect          off;
        proxy_set_header        Host $http_host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        Accept-Encoding "";
        proxy_connect_timeout   60;
        proxy_read_timeout      600;
        proxy_send_timeout      600;
    
        add_header Pragma public;
        add_header Cache-Control "public";
        log_not_found off;

        #filter img url
        sub_filter s3.amazonaws.com/adwords.yeahmobi.com 'eladeanode.trueniu.com';
        sub_filter_once off;
    }
}

在最后一个location里我使用sub_filter模块替换了所以的s3链接前缀域名为自定义域名;

但是有一个请求无法成功使用sub_filter模块,以下是该请求的所以信息:

General:
    Request URL:http://eladeanode.trueniu.com/wp-admin/admin-ajax.php
    Request Method:POST
    Status Code:200 OK
    Remote Address:104.18.51.80:80
    Referrer Policy:no-referrer-when-downgrade

Response Headers:
    Access-Control-Allow-Credentials:true
    Access-Control-Allow-Origin:http://eladeanode.trueniu.com
    Cache-Control:public
    Cache-Control:no-cache, must-revalidate, max-age=0
    CF-RAY:3b3a034676e81956-HKG
    Connection:keep-alive
    Content-Encoding:gzip
    Content-Type:application/json; charset=UTF-8
    Date:Thu, 26 Oct 2017 02:33:50 GMT
    Expires:Wed, 11 Jan 1984 05:00:00 GMT
    Pragma:public
    Server:cloudflare-nginx
    Transfer-Encoding:chunked
    X-Content-Type-Options:nosniff
    X-Frame-Options:SAMEORIGIN
    X-Powered-By:PHP/7.1.4
    X-Robots-Tag:noindex

Request Headers:
    Accept:*/*
    Accept-Encoding:gzip, deflate
    Accept-Language:zh-CN,zh;q=0.8
    Connection:keep-alive
    Content-Length:147
    Content-Type:application/x-www-form-urlencoded; charset=UTF-8   
    Cookie:wordpress_2a35dac80f8d2dba9071cc5f31db9e70=trueniu%7C1509156799%7CIByKRdqs6Hf4HRnMi7SrwpmK6Col6bo2nQU0SwXybjC%7Cbd178d88b137f748ea52662805ef67e818b9b588f82f79a769387f231968bfd1; _ga=GA1.2.785188978.1508929014; _gid=GA1.2.1878943149.1508929014; __cfduid=d92b187ddb69100aa75e9e42f6c6909d21508983775; wordpress_test_cookie=WP+Cookie+check; wordpress_logged_in_2a35dac80f8d2dba9071cc5f31db9e70=trueniu%7C1509156799%7CIByKRdqs6Hf4HRnMi7SrwpmK6Col6bo2nQU0SwXybjC%7C7b1cf8d43a5c1e7eab6c6de37af6f973ba92a19f505d72540b587a56a0772a73; wp-settings-1=libraryContent%3Dbrowse%26editor%3Dtinymce%26hidetb%3D0%26advImgDetails%3Dshow; wp-settings-time-1=1508984000
    Host:eladeanode.trueniu.com
    Origin:http://eladeanode.trueniu.com
    Referer:http://eladeanode.trueniu.com/wp-admin/upload.php
    User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
    X-Requested-With:XMLHttpRequest
   
Form Data:
    pll_ajax_backend:1
    action:query-attachments
    post_id:0
    query[orderby]:date
    query[order]:DESC
    query[posts_per_page]:40
    query[paged]:1
    
这是响应数据中的一部分:
{"success":true,"data":[{"id":864,"title":"ISO9001quality-certificate","filename":"ISO9001quality-certificate.jpg","url":"http:\/\/s3.amazonaws.com\/adwords.yeahmobi.com\/wp-content\/uploads\/sites\/7\/2017\/10\/26095151\/ISO9001quality-certificate.jpg","link":"http:\/\/eladeanode.trueniu.com\/%e6%98%93%e8%8e%b1%e5%be%b7%e7%a7%91%e6%8a%80%e9%a1%ba%e5%88%a9%e9%80%9a%e8%bf%872015%e7%89%88iso9001%e8%b4%a8%e9%87%8f%e7%ae%a1%e7%90%86%e4%bd%93%e7%b3%bb%e8%ae%a4%e8%af%81\/iso9001quality-certificate-2\/","alt":"ISO9001\u8d28\u91cf\u7ba1\u7406\u4f53\u7cfb\u8ba4\u8bc1\u8bc1\u4e66","author":"4","description":"","caption":"","name":"iso9001quality-certificate-2","status":"inherit","uploadedTo":863,"date":1508982686000,"modified":1508982738000,"menuOrder":0,"mime":"image\/jpeg","type":"image","subtype":"jpeg","icon":"http:\/\/eladeanode.trueniu.com\/wp-includes\/images\/media\/default.png","dateFormatted":"2017\u5e7410\u670826\u65e5","nonces":{"update":"f74aefdb1f","delete":"767bf73074","edit":"723e30b896"},"editLink":"http:\/\/eladeanode.trueniu.com\/wp-admin\/post.php?post=864&action=edit","meta":false,"authorName":"eladeanode","uploadedToLink":"http:\/\/eladeanode.trueniu.com\/wp-admin\/post.php?post=863&action=edit","uploadedToTitle":"\u6613\u83b1\u5fb7\u79d1\u6280\u987a\u5229\u901a\u8fc72015\u7248ISO9001\u8d28\u91cf\u7ba1\u7406\u4f53\u7cfb\u8ba4\u8bc1","filesizeInBytes":94258,"filesizeHumanReadable":"92 KB","height":768,"width":1024,"orientation":"landscape","sizes":{"thumbnail":{"height":150,"width":150,"url":"http:\/\/s3.amazonaws.com\/adwords.yeahmobi.com\/wp-content\/uploads\/sites\/7\/2017\/10\/26095151\/ISO9001quality-certificate-150x150.jpg","orientation":"landscape"},"medium":{"height":300,"width":400,"url":"http:\/\/s3.amazonaws.com\/adwords.yeahmobi.com\/wp-content\/uploads\/sites\/7\/2017\/10\/26095151\/ISO9001quality-certificate-400x300.jpg","orientation":"landscape"},"large":{"height":480,"width":640,"url":"http:\/\/s3.amazonaws.com\/adwords.yeahmobi.com\/wp-content\/uploads\/sites\/7\/2017\/10\/26095151\/ISO9001quality-certificate-1024x768.jpg","orientation":"landscape"},"full":{"url":"http:\/\/s3.amazonaws.com\/adwords.yeahmobi.com\/wp-content\/uploads\/sites\/7\/2017\/10\/26095151\/ISO9001quality-certificate.jpg","height":768,"width":1024,"orientation":"landscape"}},"compat":{"item":"","meta":""}}]}

可以发现在响应数据中图片的url没有经过sub_filter模块的过滤,而直接返回了s3的链接,,而在WP的后台媒体库里就是通过这种方式来获取所以图片的,所以导致显示这块的图片无法代理成功。

猜想:难道是Nginx proxy中sub_filter无法过滤POST请求中响应的数据?

阅读 9.9k
2 个回答

你S3和阿里云都绑定同一个域名,然后用阿里云的OSS镜像S3的系统自带域名,这样你用DNS的分区域解析,访问到的内容是同一个

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题