Rewrite all links on reverse proxied page

2019-09-14 23:18发布

问题:

I'm reverse proxying to a IP restricted page like this:

server {
    server_name mypage.com;
    listen 80 ;

    location / {
       proxy_pass https://sandbox.otherpage.com/;
       proxy_bind $server_addr;
    }
}

This works good but all links on the page (including AJAX calls) links to https://sandbox.otherpage.com/

I am not sure whether I do something wrong or the the other web application I'm proxying to links to the absolute page.

How can I rewrite these links?

回答1:

You can use sub filters to do what you want

location / {
  proxy_pass https://sandbox.otherpage.com/;
  proxy_bind $server_addr;
  sub_filter "https://sandbox.otherpage.com/" "https://myserver.com/";
  sub_filter_once off;
  sub_filter_types *;
}

You can get rid of the sub_filter_types if you only need to match html files.

Also you might need to add proxy_set_header Accept-Encoding "".