The magic that is nginx_reverse_proxy

I cannot count anymore how many times the reverse proxy function of nginx has saved my life - or sanity - in course of the last year.

Like every life-saving, close-to-magic technology, it needs mastery. Which I do not command, thus having to spend endless nights fiddling around for the little joy of "it works".

One pitfall is that you cannot seem to use sub_filter (which replaces a string with another string) on compressed resources. Which is a bit of a bummer but at least there is a workaround: set an empty accept-encoding header. I found it here.

Replace URLs with nginx sub_filter
Replacing URLs in content served behind nginx reverse proxy

But why am I so excited? Just a small example:

In my current scenario, I have a setup that generates a folder structure like this:
domain-like-folder/subfolders/of/content
with language automatically prepended, meaning
language/domain-like-folder/subfolders/of/content

What I need is
domain.tld/language/subfolders/of/content

I set up a server with the above domain folder structure on a server called origin.tld. Then I configure domain.tld like this (simplified):

 location /language/ {               
  proxy_pass https://origin.tld/language/domain-like-folder/;
  proxy_set_header Accept-Encoding "";
  sub_filter   /language/domain-like-folder/   /language/;
  sub_filter_once off;
}

The sub_filter directive is necessary (and also a big part of the magic), as the generated links on the origin domain would look like this: <a href="/language/domain-like-folder/other-subfolder"> which is what I'm trying to avoid. Of course, you can use sub_filter much more sophisticatedly than I do here.

You know better? Write to me! @gurkendoktor@mastodon.cloud

I haven't posted in a while. That happens. A few posts will follow this one in the near future. Not all of them will be about nginx. Some will be about Astro, 11ty, Headless CMS, or other random stuff.