0

I'm trying to add different apps behind different locations of a reverse proxy.

I'm aware of the proxy_redirect directive which modifies the Location and Refresh headers, however I also need to modify custom headers like X-Ajax-Redirect.

I also tried my luck with the map directive but since this is even above the server level it doesn't work for different locations. I would need something like sub_filter that works for headers.

My current configuration:

worker_processes auto;

events { worker_connections 1024; }

http {

    sendfile on;

    upstream docker-firstapp{
        server firstapp:8080;
    }

    upstream docker-secondapp{
        server secondapp:8080;
    }

    server {
        listen 80;

        location /myFirstApp/ {
            proxy_pass         http://docker-firstapp/;
            proxy_redirect     / /myFirstApp/;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;

            sub_filter_once off;
            sub_filter ' href="/' ' href="/myFirstApp/';
            sub_filter ' src="/' ' src="/myFirstApp/';
            sub_filter ' action="/' ' action="/myFirstApp/';

            # sub_filter 'X-Ajax-Redirect: /' 'X-Ajax-Redirect: /myFirstApp/';
        }

        location /mySecondApp/ {
            proxy_pass         http://docker-secondapp/;
            proxy_redirect     / /mySecondApp/;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;

            sub_filter_once off;
            sub_filter ' href="/' ' href="/mySecondApp/';
            sub_filter ' src="/' ' src="/mySecondApp/';
            sub_filter ' action="/' ' action="/mySecondApp/';

            # sub_filter 'X-Ajax-Redirect: /' 'X-Ajax-Redirect: /mySecondApp/';
        }
    }
}

Any ideas on how to accomplish what I need?

1 Answer 1

0

Proxy server take headers that you set for it and back with a response with its own headers

I don't know whether you want to

  • send specific header for proxy {server > proxy}

or

  • filters proxy responds before announcing to client {server > client}

so let's say we need both


If server is communicating with proxy: you would use proxy_set_header

you could set what header you want and send to the proxy

set $my_header "/myFirstApp/$x_ajax_redirect";
proxy_set_header X-Ajax-Redirect $my_header

possible duplicate Change Host header in nginx reverse proxy


If server is communicating with client: nginx has the ability to add headers using add_header directive

But it has 2 Limitations

  1. It doesn't modify , means it only add headers.. so it could duplicate and conflict either nginx or browser
  2. Restricted headers: means it only work with headers that nginx knows

an alternative is headers-more-nginx-module

it allows for replacing, clearing default nginx headers and setting either default or custom header

directive will look like this more_set_headers 'X-Ajax-Redirect: ...';

Documentations refers to some limitations but no problem as it could be used along with add_header

The only real limitation is that it's not part of nginx mainline so you will need to compile it into nginx

Installation

  1. Get nginx source code
  2. Download the module from its file list
  3. Build nginx from source with the module
# download nginx source code
wget 'http://nginx.org/download/nginx-x.xx.x.tar.gz'
tar -xzvf nginx-x.xx.x.tar.gz
cd nginx-x.x.x/

# nginx installation dir: /opt/nginx/
# assuming you downdoaded the module
./configure --prefix=/opt/nginx \
    --add-module=/path/to/headers-more-nginx-module

# build nginx
make
make install

Using docker ? You could find some nginx containers with this module compiled like macbre/nginx-http3

I will not recommend this container as it's built for expermental http3 features

better to build from source or loading the module as a dynamic one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.