how to redirect no-www to www under jwilder/nginx-proxy?

Viewed 3587
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

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

Related