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.
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