Replace Contents in NGINX
The content of a webpage contains URL or other information need to be replaced, especially when domain name changed in URL.
sub_filter
Use sub_filter module in NGINX to replace content.
For example,
server {
```
location / {
sub_filter 'www.example.com' 'www.xx.example.com';
sub_filter_once on;
proxy_pass http://www.example.com;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_buffering 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;
}
}
Note: Place sub_filter at beginning of the location session
If required, also need to change host to allow server decide which page requested if it is also a NGINX server and displays page depending on the host as well.
proxy_set_header Host www.example.com;
References
Module ngx_http_sub_module
http_sub_module / sub_filter of nginx and reverse proxy not working