Im am trying to set up a reverse proxy to provide a local http service via http-ssl (https). The configuration is set up like this:
- Backend server listens on 127.0.0.1:8081 and therefore only receives connections from localhost. It serves all web pages without (!) compression.
- Frontend server (= nginx) listens on port 83 and serves data it fetches from 127.0.0.1:8081
So far this work fine in nginx. Unfortunately the backend server provides some absolute URLs like "http://127.0.0.1:8081/....
" in the web pages it serves. And this behaviour can not (!) be changed. The nginx server therefore delivers the pages as is containing the invalid references to 127.0.0.1 by default - which is wrong by the clients point of view.
Using a sub_filter rule the web pages served should undergo a string replacement within the web page. As I can not modify the backend server the nginx server should replace http://127.0.0.1
with a more suitable URL. But this is not working. In fact it seems that no kind of string replacement is performed at all. What ever strings I try to replace - the web page delivered to the client remains unchanged.
My nginx configuration is this:
server {
listen 83 default_server;
location / {
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8081;
sub_filter Test TEST;
sub_filter_once on;
sub_filter_types text/xml text/css text/javascript;
}
}
Note: In this configuration I am trying to replace "Test" with "TEST" (but to no avail). "Test" is a randomly selected string found in the web page delivered by the backend web server. I use as test subject here. The real replacement should be "http://127.0.0.1:8081/
" to "/
" later, but even this simple test did not succeed.
Another note: In the final configuration the nginx server should serve the data using https. For tests the configuration tested and presented above does not include the SSL configuration.
nginx -V
tells me that nginx has been compiled with the relevant modules (--with-http_sub_module
) so everything should work, shouldn't it?
I probably do something wrong but unfortunately I can not figure out what it is exactly that I am doing wrong here. I found even two answers on stackoverflow, namely the following ones, but none of them worked:
- Any tool/utility to rewrite URLs in an html page
- http_sub_module / sub_filter of nginx and reverse proxy not working
Can you help? Thanx in advance for your answers.