I'm trying to make the simplest proxy possible, for the following scenario:
1. upstream is behind Round Robin DNS
2. I'd need keepalive requests to upstream, even if client closes the connection
3. I need to modify the content of the JSONs returned.
I figured out 1. and 2. but 3. really took me a long time. First a sub_filter seemed to be working from curl, but from real browsers it was always broken. I figured out the reason is that it doesn't work on compressed responses.
My idea was then to add proxy_set_header Accept-Encoding '' for the application/json responses, but I cannot find a clean way to do this. Having this inside an if block would be great, but it's not allowed if I understand correctly.
At the end, I ended up copying and pasting the whole location block and listing every single location in a regex which can return a JSON.
My question:
1. Is there a way to make this much cleaner somehow? I mean I'd like to add proxy_set_header and gzip directives by detecting content-type, not by regexing all possible JSON locations.
2. Do I understand it correctly that I need to get it uncompressed, run sub_filter and then compress it back?
Here is my config:
upstream ofm {
server tiles.openfreemap.org:443;
keepalive 32;
}
server {
location / {
proxy_pass https://ofm;
proxy_set_header Host tiles.openfreemap.org;
proxy_http_version 1.1;
proxy_set_header Connection '';
}
# modify the json's content on the fly
location ~ ^/(planet(/[^/]*)?|monaco(/[^/]*)?|styles/[^/]+)$ {
proxy_pass https://ofm;
proxy_set_header Host tiles.openfreemap.org;
proxy_http_version 1.1;
proxy_set_header Connection '';
# get uncompressed
proxy_set_header Accept-Encoding '';
# search and replace
sub_filter 'tiles.openfreemap.org' 'ofm.maphub.net';
sub_filter_once off;
sub_filter_types application/json;
# compress it back
gzip on;
gzip_comp_level 1;
gzip_types application/json;
}
}