3

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:

Can you help? Thanx in advance for your answers.

Community
  • 1
  • 1
Regis May
  • 3,070
  • 2
  • 30
  • 51
  • The ngx_http_sub_module module is a filter that modifies a response by replacing one specified string by another. What you need is rewite ... ... break; and then proxy_pass http://nginx.org/ru/docs/http/ngx_http_proxy_module.html#proxy_pass http://serverfault.com/questions/379675/nginx-reverse-proxy-url-rewrite – Alexander Altshuler Aug 31 '15 at 14:04
  • According to your explanation the http_sub_module should do exactly what I would expect it to do: Modifying the content before delivery. But it does not: Regardles of what kind of RegEx I provide the content does not get modified. Nevertheless thank you for mentioning "rewrite": I did not come accross any configuration possibility like that in my google searches. I will try this option soon and then give feedback if "rewrite" solved this problem. – Regis May Sep 01 '15 at 23:43

1 Answers1

0

Hello!

Have you solved this issue? I think what you need, is the http_realip module.

From nginx.org documentation:

The ngx_http_realip_module module is used to change the client address to the one sent in the specified header field. This module is not built by default, it should be enabled with the --with-http_realip_module configuration parameter.

This module is needed when your NGINX server is behind a L7 load balancer or another device that passes the client's IP address in an HTTP header.

Probably this will help

algolejos
  • 129
  • 6