I use nginx docker(https://github.com/jwilder/nginx-proxy), but find it no way to amend .htaccess as mentioned here(Nginx no-www to www and www to no-www). Could anyone tell me how to redirect no-www to www under the above jwilder/nginx-proxy.
6 Answers
This may be a little late but I found the solutions here to be too much faffing, so I created the adamkdean/redirect
lightweight companion service for jwilder/nginx-proxy
.
The example below simply shows HTTP but you can hook this up to HTTPS if you like using the letsencrypt-nginx-proxy companion service by JrCs.
For adamkdean/redirect
, you simply provide two environment vars, one of the redirect location and one of the status code (which can be either 301, 302, 303, or 307) with the default being 307 (if you omit REDIRECT_STATUS_CODE).
version: '2'
services:
nginx-proxy:
image: jwilder/nginx-proxy
ports:
- 80:80
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
redirect:
image: adamkdean/redirect
environment:
- VIRTUAL_HOST=example.com
- REDIRECT_LOCATION="http://www.example.com"
- REDIRECT_STATUS_CODE=301
example:
image: example
environment:
- VIRTUAL_HOST=www.example.com
Find the repo here: https://github.com/adamkdean/redirect

- 7,387
- 10
- 47
- 68
-
@VonC Ha ha. No offence intended, just sometimes you need a solution out of the box that is repeatable and reliable with minimal to zero customisation. – Adam K Dean Aug 15 '19 at 15:37
-
That seems a nice solution and works in reverse i.e. www to non-www but can it handle multiple sites? For example, can I redirect two hosted sites - www.domain1.com to domain1.com and www.domain2 to domain2? If so, it could be a neat way of handling the redirection of different sites in one place rather than through DNS for each one. – mharran Mar 01 '21 at 10:25
-
@mharran as it stands, no, but it is something that I'm planning to add as I find myself using many instances for single redirects. – Adam K Dean Mar 12 '21 at 10:37
-
That's great, @AdamKDean, it will be really useful. – mharran Mar 12 '21 at 10:59
-
For anyone reading, this has now been done and multiple redirects is now supported. – Adam K Dean Apr 13 '21 at 12:22
-
1@AdamKDean how can i also hook it to HTTPS? If I add port "443:443" it conflicts with nginx-proxy – Andrea Apr 21 '21 at 16:04
You would need to:
git clone https://github.com/jwilder/nginx-proxy
,- amend
nginx.tmpl
and - rebuild the nginx-proxy image yourself.
That way, you would generate a new nginx-proxy image which does include the directives you need.

- 1,262,500
- 529
- 4,410
- 5,250
You can add the redirect without changing the nginx.tmpl. There is the option to import further configuration files either directly under server {
or in the default location location / {
. See https://github.com/jwilder/nginx-proxy#per-virtual_host.
Create and mount a file /etc/nginx/vhost.d/your-website.com
or /etc/nginx/vhost.d/your-website.com_location
with the following content:
rewrite ^/(.*)$ http://www.your-website.com/$1 permanent;

- 151
- 6
Introduction
Unfortunately you did not specify your technical setup. So i do have to make some assumptions. Let's assume that you want to start a blog using a fully supported docker environment with following docker images:
- nginx-proxy,
- docker-gen,
- letsencrypt,
- ghost (blog software) and
- mariaDB.
Therefore you registered a domain with the name personalblog.com. You already set up an DNS A record to the IP address where your blog content will be hosted. So you have an A record for personalblog.com to that IP address and an A record for www.personalblog.com to that IP address.
Requirements
Please follow the instructions to setup your nginx-proxy with letsencrypt environment (you will find in these repositories a full instruction for setup):
After finishing the nginx-proxy setup for docker, please follow these instructions for setup your blog software with ghost and maria db:
There might be a docker bug when pulling the latest image for ghost. So change in the docker-compose.yml ghost:latest to ghost:1.22.1
Redirect non-www to www for jwilder/nginx-proxy
In the nginx-proxy setup (described by @evertramos repository above) you specified in the .env file your Nginx File Path (Check out the line with NGINX_FILES_PATH=/path/to/your/nginx/data). With this you created a directory outside of the nginx docker container.
Step 1
cd /path/to/your/nginx/data/vhost.d/
sudo vim personalblog.com
Step 2
In the new file personalblog.com you add following line:
return 301 http://www.personalblog.com$request_uri;
Save the file with ESC and :wq
Step 3
Go to your path where your docker-compose.yml for nginx lies and do:
sudo docker-compose up -d --force-recreate
Notes
The file that you are creating under /path/to/your/nginx/data/vhost.d/ has to be the name of your domain. In this case you want to redirect from personalblog.com to www.personalblog.com, so the file name is personalblog.com. If you followed the instructions of the repositories above you will automatically redirected to https. So don't worry if you just redirect to http://www.personalblog.com
.

- 773
- 8
- 11
Don't forget to update the variable VIRTUAL_HOST to have both the www and non-WW hostname:
VIRTUAL_HOST=www.target-host.com, target-host.com

- 2,243
- 1
- 15
- 16
First you need to clone git using: https://github.com/jwilder/nginx-proxy
Then amend it using nginx.tmpl
and at last rebuild it using nginx-proxy
image yourself.