Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for virtual host redirection #1563

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ services:
whoami:
image: jwilder/whoami
environment:
- VIRTUAL_HOST=whoami.local
- VIRTUAL_HOST=whoami.local,www.whoami.local
- REDIRECT=www.whoami.local
```

```shell
Expand Down Expand Up @@ -82,6 +83,10 @@ If you need to support multiple virtual hosts for a container, you can separate

You can also use wildcards at the beginning and the end of host name, like `*.bar.com` or `foo.bar.*`. Or even a regular expression, which can be very useful in conjunction with a wildcard DNS service like [xip.io](http://xip.io), using `~^foo\.bar\..*\.xip\.io` will match `foo.bar.127.0.0.1.xip.io`, `foo.bar.10.0.2.2.xip.io` and all other given IPs. More information about this topic can be found in the nginx documentation about [`server_names`](http://nginx.org/en/docs/http/server_names.html).

### Redirect Hosts

Using the `REDIRECT` environment variable, you can 301 permanent redirect all your virtual hosts to a specific host. Commonly used in production for SEO purposes. For example, you can redirect both `www.bar.com,bar.com` to `www.bar.com` using `REDIRECT=www.bar.com` or `www.bar.com,bar.com` to `bar.com` using `REDIRECT=bar.com`.

### Multiple Networks

With the addition of [overlay networking](https://docs.docker.com/engine/userguide/networking/get-started-overlay/) in Docker 1.9, your `nginx-proxy` container may need to connect to backend containers on multiple networks. By default, if you don't pass the `--net` flag when your `nginx-proxy` container is created, it will only be attached to the default `bridge` network. This means that it will not be able to connect to containers on networks other than `bridge`.
Expand Down
38 changes: 37 additions & 1 deletion nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ upstream {{ $upstream_name }} {
{{/* Get the VIRTUAL_ROOT By containers w/ use fastcgi root */}}
{{ $vhost_root := or (first (groupByKeys $containers "Env.VIRTUAL_ROOT")) "/var/www/public" }}

{{/* Get the first redirect defined by containers w/ the same vhost */}}
{{ $redirectHost := or (first (groupByKeys $containers "Env.REDIRECT")) ""}}

{{/* Get the first cert name defined by containers w/ the same vhost */}}
{{ $certName := (first (groupByKeys $containers "Env.CERT_NAME")) }}
Expand Down Expand Up @@ -260,10 +262,20 @@ server {
try_files $uri =404;
break;
}


{{ if (and $redirectHost (ne $redirectHost $host)) }}
# Redirect host {{ $host }} => {{ $redirectHost }}
{{ end }}

{{ if (and $redirectHost (ne $redirectHost $host)) }}
location / {
return 301 https://{{ $redirectHost }}$request_uri;
}
{{ else }}
location / {
return 301 https://$host$request_uri;
}
{{ end }}
}
{{ end }}

Expand Down Expand Up @@ -308,7 +320,12 @@ server {
{{ else if (exists "/etc/nginx/vhost.d/default") }}
include /etc/nginx/vhost.d/default;
{{ end }}

{{ if (and $redirectHost (ne $redirectHost $host)) }}
# Redirect host {{ $host }} => {{ $redirectHost }}
{{ end }}

{{ if (or (not $redirectHost) (eq $redirectHost $host)) }}
location / {
{{ if eq $proto "uwsgi" }}
include uwsgi_params;
Expand All @@ -333,6 +350,13 @@ server {
include /etc/nginx/vhost.d/default_location;
{{ end }}
}
{{ end }}

{{ if (and $redirectHost (ne $redirectHost $host)) }}
location / {
return 301 https://{{$redirectHost}}$request_uri;
}
{{ end }}
}

{{ end }}
Expand All @@ -357,7 +381,12 @@ server {
{{ else if (exists "/etc/nginx/vhost.d/default") }}
include /etc/nginx/vhost.d/default;
{{ end }}

{{ if (and $redirectHost (ne $redirectHost $host)) }}
# Redirect host {{ $host }} => {{ $redirectHost }}
{{ end }}

{{ if (or (not $redirectHost) (eq $redirectHost $host)) }}
location / {
{{ if eq $proto "uwsgi" }}
include uwsgi_params;
Expand All @@ -381,6 +410,13 @@ server {
include /etc/nginx/vhost.d/default_location;
{{ end }}
}
{{ end }}

{{ if (and $redirectHost (ne $redirectHost $host)) }}
location / {
return 301 http://{{ $redirectHost }}$request_uri;
}
{{ end }}
}

{{ if (and (not $is_https) (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
Expand Down