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 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 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
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_headerpossible 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
- It doesn't modify , means it only add headers.. so it could duplicate and conflict either nginx or browser
- 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
- Get nginx source code
- Download the module from its file list
- Build nginx from source with the module
# download nginx source code
wget '
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 installUsing 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