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:

1
2
3
4
5
6
HTTP/2 301
server: noindex
date: Thu, 24 Nov 2022 11:53:25 GMT
content-type: text/html
content-length: 169
location: http://blog.sandorkovacs.hu/tags/network/

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:

1
2
3
4
5
6
HTTP/2 301
server: noindex
date: Thu, 24 Nov 2022 11:47:26 GMT
content-type: text/html
content-length: 169
location: /tags/network

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
services:
  nginx:
    image: nginx:stable-alpine
    container_name: nginx
    network_mode: "bridge"
    restart: unless-stopped
    ports:
      - 8095:80
    volumes:
      - /volume1/docker/nginx/www:/usr/share/nginx/html:ro
      - /volume1/docker/nginx/config/rewrite.conf:/etc/nginx/conf.d/rewrite.conf

The contents of the rewrite.conf file:

1
2
3
server {
    absolute_redirect off;
}

That seems to solve the issue, case closed.