2

I'm containerizing some websites. I would like to be able to migrate some rewrite rules to automatically append or remove www from the domain name in the request.

How/Where in the jwilder/nginx-proxy Docker container nginx.tmpl or otherwise can I add some simple rewrite rules?

One rule for all proxied containers is okay, although it would be nice if rules could be specified per container.

Redsandro
  • 11,060
  • 13
  • 76
  • 106
  • Where you actually wrote these rewrite rules ? inside `location /` or inside a custom location ? – Mostafa Hussein Mar 12 '19 at 23:14
  • @MostafaHussein neither. I wrote them inside the vhost `server` block. Can they even legally be inside `location`? I think that would make things easier. – Redsandro Mar 13 '19 at 08:26
  • I get it! I thought you were using the location way which contain a rewrite inside it but you seems to be using the single line `rewrite`, am i right? then will post the answer – Mostafa Hussein Mar 13 '19 at 08:30

1 Answers1

5

The jwilder/nginx-proxy docker image allows you to add a configuration per virtual host where you can add the rewrite rules as described in here:

To add settings on a per-VIRTUAL_HOST basis, add your configuration file under /etc/nginx/vhost.d. The per-VIRTUAL_HOST file must be named exactly after the VIRTUAL_HOST.

In order to allow virtual hosts to be dynamically configured as backends are added and removed, it makes the most sense to mount an external directory as /etc/nginx/vhost.d as opposed to using derived images or mounting individual configuration files.

For example, if you have a virtual host named www.app.example.com, you could provide a custom rewrite configuration for that host as follows:

Under /etc/nginx/vhost.d create a file called www.app.example.com then add the following content:

return 301 $scheme://app.example.com$request_uri;

Then create a new nginx container and mount this directory to it. If you checked /etc/nginx/conf.d/default.conf you will notice that the virtual host has been modified to something like this:

server {
    server_name www.app.example.com;
    listen 80 ;
    access_log /var/log/nginx/access.log vhost;
    include /etc/nginx/vhost.d/www.app.example.com;
    location / {
        proxy_pass http://www.app.example.com;
    }
}

The include line contains the return statement that we wrote and of course you can add more rewrite rules to it

Mostafa Hussein
  • 11,063
  • 3
  • 36
  • 61
  • Makes sense, thank you. Upvoted for now. Give me some time to check this out and I will get back to you! – Redsandro Mar 13 '19 at 16:27
  • This does exactly what I asked! Although I had hoped I could keep the configuration with the relevant container (such as `environment` for vhost names) and use `docker-gen` for adding the rewrite rules, but this answers my question! Accepted. – Redsandro Mar 13 '19 at 23:10
  • This does not actually work in production because [the config conflicts with letsencrypt-companion](https://github.com/JrCs/docker-letsencrypt-nginx-proxy-companion/issues/521). Is there another way to do this? – Redsandro Mar 14 '19 at 01:01
  • Have you tried to recreate the container ? Restarting only wont be enough – Mostafa Hussein Mar 14 '19 at 01:11
  • Yes, `down -v` followed by `up -d` – Redsandro Mar 14 '19 at 19:55
  • So on the host there is no `return` but inside the container you still have it ? – Mostafa Hussein Mar 14 '19 at 19:57