TIL #3 - Nginx docker container rewrite rules
Let’s look a little bit under the hood
I chose Hugo to generate the page since I always wanted to try out one of these fancy site builder thingies. The config seemed pretty straightforward and I was up and running fairly quickly. I played around some with the Minimal Bootstrap Hugo Theme but was not satisfied with it so I changed to the Cactus theme. The change was not a huge deal, the site was up and running I clicked through it, saw that everything was working, pats on shoulders all around.
TLS troubles
Or so I thought. A dear friend of mine pointed out that a lot of links are not working properly (going to 404). I tested again everything and of course it #worksforme…
…up until I tested it on Edge. You see, I’m using Firefox and I have the HTTPS-only mode enabled for all windows. On Edge the problem became apparent very quickly.
Since this indicated a server-side problem. I tried to see what’s happening under the hood using curl:
curl -I https://blog.sandorkovacs.hu/tags/network
Which resulted in:
|
|
Aaaand got the problem. Nginx rewrites the URI to http://blog.sandorkovacs.hu/tags/network/ and I don’t serve anything there, being this is a docker container of the official nginx flavour behind a reverse proxy and I’m only reversing to port 443.
The solution was a bit tricky for me since I’m not that familiar with nginx (was an Apache guy my whole life). After a few rounds of hit and miss I came up with adding one line into the server config in the container’s /etc/nginx/conf.d/default.conf file:
absolute_redirect off;
This seemed to solve the issue:
|
|
That’s fine and dandy but how to make this permanent?
Of course I was aware that editing the running container’s config is not a permanent solution. So what can I do?
After looking at all the config files, I found a line in /etc/nginx/nginx.conf:
include /etc/nginx/conf.d/*.conf;
This gave me the idea of adding a rewrite.conf file of my own to the docker-compose.yaml file:
|
|
The contents of the rewrite.conf file:
|
|
That seems to solve the issue, case closed.