I want to replace the specific text in the response body of the text/html content type pages.
I am using NGINX Reverse Proxy setup for this. When i hit the NGINX base URL it is redirecting to the https://www.google.com but the response is not receiving via NGINX. So i am unable to replace the text in the response.
For Redirection i used Proxy_Pass, rewrite directives. For text replace i used sub_filter directive
When i tried this approach in my local machine which is running in the tomcat server it works. But when i try this on the https://www.google.com it didn't work.
I need help on how to redirect the https://www.google.com using my NGINX server and get the response back to the NGINX server.
This is my NGINX Reverse proxy configuration. In all the configuration the redirect works but the response is not received via NGINX server.
Configuration 1:
server {
listen 85;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
rewrite ^/(.*)$ https://www.google.com/$1 permanent;
sub_filter '</body>' '</body><div><p>Replace success</p></div>';
sub_filter_once off;
}
Configuration 2:
server {
listen 85;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass https://www.google.com;
sub_filter '</body>' '</body><div><p>test</p></div>';
sub_filter_once off;
}
Configuration 3:
server {
listen 85;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
add_header Access-Control-Allow-Origin $http_origin;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-NginX-Proxy true;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass https://www.google.com;
sub_filter '</body>' '</body><div><p>test</p></div>';
sub_filter_once off;
}
Configuration 4:
upstream test {
ip_hash;
server google.com:443;
}
server {
listen 85;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass https:test;
add_header Access-Control-Allow-Origin $http_origin;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-NginX-Proxy true;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
sub_filter '</body>' '</body><div><p>test</p></div>';
sub_filter_once off;
}
The reason might because the proxy_pass destination, for ex. google has gzip enabled. You can check that by various websites (https://www.giftofspeed.com/gzip-test/) or if have a look at the headers of the response. When gzip is enabled sub_filter can't find the search and replace strings and that's why nothing happens.
See: https://stackoverflow.com/a/36274259