add specific port feature

This commit is contained in:
Frederik Bosch 2018-07-30 21:13:49 +02:00
parent 3547592e16
commit c8bd35f633
7 changed files with 58 additions and 15 deletions

View file

@ -401,6 +401,12 @@ If you are using multiple hostnames for a single container (e.g. `VIRTUAL_HOST=e
If you want most of your virtual hosts to use a default single `location` block configuration and then override on a few specific ones, add those settings to the `/etc/nginx/vhost.d/default_location` file. This file If you want most of your virtual hosts to use a default single `location` block configuration and then override on a few specific ones, add those settings to the `/etc/nginx/vhost.d/default_location` file. This file
will be used on any virtual host which does not have a `/etc/nginx/vhost.d/{VIRTUAL_HOST}_location` file associated with it. will be used on any virtual host which does not have a `/etc/nginx/vhost.d/{VIRTUAL_HOST}_location` file associated with it.
#### Multiple ports per virtual host
A container can expose multiple ports that you want to proxy to different host names. In order to achieve this, you need
to activate pass `VIRTUAL_HOST_SPECIFIC_PORT` with the value `true` to *nginx-proxy*. Then you can start start any container
and pass multiple host names and ports `VIRTUAL_HOST=subdomain1.youdomain.com,subdomain2.youdomain.com:5000`
### Contributing ### Contributing
Before submitting pull requests or issues, please check github to make sure an existing issue or pull request is not already open. Before submitting pull requests or issues, please check github to make sure an existing issue or pull request is not already open.

View file

@ -119,9 +119,9 @@ server {
{{ $host := trim $host }} {{ $host := trim $host }}
{{ $is_regexp := hasPrefix "~" $host }} {{ $is_regexp := hasPrefix "~" $host }}
{{ $upstream_name := when $is_regexp (sha1 $host) $host }} {{ $is_specific_port := eq (or ($.Env.VIRTUAL_HOST_SPECIFIC_PORT) "") "true" }}
{{ $specific_port := eq (or ($.Env.VIRTUAL_HOST_SPECIFIC_PORT) "") "true" }} {{ $upstream_name := when (or $is_regexp $is_specific_port) (sha1 $host) $host }}
{{ $server_name := when $specific_port (coalesce (index (split $host ":") 0) $host) $host }} {{ $server_name := when $is_specific_port (index (split $host ":") 0) $host }}
# {{ $host }} # {{ $host }}
upstream {{ $upstream_name }} { upstream {{ $upstream_name }} {
@ -134,9 +134,10 @@ upstream {{ $upstream_name }} {
{{ if (and (ne $containerNetwork.Name "ingress") (or (eq $knownNetwork.Name $containerNetwork.Name) (eq $knownNetwork.Name "host"))) }} {{ if (and (ne $containerNetwork.Name "ingress") (or (eq $knownNetwork.Name $containerNetwork.Name) (eq $knownNetwork.Name "host"))) }}
## Can be connected with "{{ $containerNetwork.Name }}" network ## Can be connected with "{{ $containerNetwork.Name }}" network
{{ if $specific_port }} {{ $split_host_port := split $host ":" }}
{{/* If ports per virtual host specified, use that, falling back to standard web port $container.Env.VIRTUAL_PORT and 80 */}} {{ if (and $is_specific_port (eq (len $split_host_port) 2)) }}
{{ $port := coalesce (index (split $host ":") 1) $container.Env.VIRTUAL_PORT "80" }} {{/* If ports per virtual host specified, use that */}}
{{ $port := index $split_host_port 1 }}
{{ $address := where $container.Addresses "Port" $port | first }} {{ $address := where $container.Addresses "Port" $port | first }}
{{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }} {{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }}
{{/* If only 1 port exposed, use that */}} {{/* If only 1 port exposed, use that */}}

View file

@ -1,3 +1,3 @@
[pytest] [pytest]
# disable the creation of the `.cache` folders # disable the creation of the `.cache` folders
addopts = -p no:cacheprovider --ignore=requirements --ignore=certs -r s -v addopts = -k "specified_mixed" -p no:cacheprovider --ignore=requirements --ignore=certs -r s -v

View file

@ -1,15 +1,17 @@
web: web:
image: web image: web
expose: expose:
- "81" - "5000"
- "5001"
environment: environment:
WEB_PORTS: 81 WEB_PORTS: "5000 5001"
VIRTUAL_HOST: webA.nginx-proxy.tld:5000,webB.nginx-proxy.tld:5001 VIRTUAL_HOST: webA.nginx-proxy.tld:5000,webB.nginx-proxy.tld:5001
VIRTUAL_HOST_SPECIFIC_PORT: "true"
sut: sut:
image: jwilder/nginx-proxy:test image: jwilder/nginx-proxy:test
volumes: volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro - /var/run/docker.sock:/tmp/docker.sock:ro
- ./lib/ssl/dhparam.pem:/etc/nginx/dhparam/dhparam.pem:ro - ../lib/ssl/dhparam.pem:/etc/nginx/dhparam/dhparam.pem:ro
environment:
VIRTUAL_HOST_SPECIFIC_PORT: "true"

View file

@ -0,0 +1,16 @@
import pytest
def test_unknown_virtual_host_is_503(docker_compose, nginxproxy):
r = nginxproxy.get("http://unknown.nginx-proxy.tld/port")
assert r.status_code == 503
def test_webA_is_forwarded(docker_compose, nginxproxy):
r = nginxproxy.get("http://webA.nginx-proxy.tld/port")
assert r.status_code == 200
assert r.text == "answer from port 80\n"
def test_webB_is_forwarded(docker_compose, nginxproxy):
r = nginxproxy.get("http://webB.nginx-proxy.tld/port")
assert r.status_code == 200
assert r.text == "answer from port 5001\n"

View file

@ -0,0 +1,17 @@
web:
image: web
expose:
- "80"
- "5001"
environment:
WEB_PORTS: "80 5001"
VIRTUAL_HOST: webA.nginx-proxy.tld,webB.nginx-proxy.tld:5001
sut:
image: jwilder/nginx-proxy:test
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
- ../lib/ssl/dhparam.pem:/etc/nginx/dhparam/dhparam.pem:ro
environment:
VIRTUAL_HOST_SPECIFIC_PORT: "true"

View file

@ -1,15 +1,16 @@
web: web:
image: web image: web
expose: expose:
- "81" - "80"
environment: environment:
WEB_PORTS: 81 WEB_PORTS: 80
VIRTUAL_HOST: webA.nginx-proxy.tld,webB.nginx-proxy.tld VIRTUAL_HOST: webA.nginx-proxy.tld,webB.nginx-proxy.tld
VIRTUAL_HOST_SPECIFIC_PORT: "true"
sut: sut:
image: jwilder/nginx-proxy:test image: jwilder/nginx-proxy:test
volumes: volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro - /var/run/docker.sock:/tmp/docker.sock:ro
- ./lib/ssl/dhparam.pem:/etc/nginx/dhparam/dhparam.pem:ro - ../lib/ssl/dhparam.pem:/etc/nginx/dhparam/dhparam.pem:ro
environment:
VIRTUAL_HOST_SPECIFIC_PORT: "true"