From 13c761781415912f1b76649e041e706e559fcd96 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Sat, 16 Mar 2019 14:53:52 +0900 Subject: [PATCH 1/2] Rename VIRTUAL_SERVICE_NAME to VIRTUAL_HOST_NAME --- Production/nginx.tmpl | 6 +++--- .../docker-fragments/btcpayserver-nginx.yml | 2 +- .../docker-fragments/opt-add-librepatron.yml | 2 +- .../docker-fragments/opt-add-woocommerce.yml | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Production/nginx.tmpl b/Production/nginx.tmpl index 9867d12..9093e8c 100644 --- a/Production/nginx.tmpl +++ b/Production/nginx.tmpl @@ -159,7 +159,7 @@ upstream {{ $upstream_name }} { {{/* Get the VIRTUAL_PROTO defined by containers w/ the same vhost, falling back to "http" */}} {{ $proto := trim (or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http") }} -{{ $service_name := trim (or (first (groupByKeys $containers "Env.VIRTUAL_SERVICE_NAME")) "") }} +{{ $host_name := trim (or (first (groupByKeys $containers "Env.VIRTUAL_HOST_NAME")) "") }} {{/* Get the NETWORK_ACCESS defined by containers w/ the same vhost, falling back to "external" */}} {{ $network_tag := or (first (groupByKeys $containers "Env.NETWORK_ACCESS")) "external" }} @@ -299,7 +299,7 @@ server { {{ end }} } - {{ if eq $service_name "btcpay" }} + {{ if eq $host_name "btcpay" }} {{ range $container := $ }} {{ $serviceName := (index $container.Labels "com.docker.compose.service") }} {{ if (eq $serviceName "lnd_bitcoin") }} @@ -350,7 +350,7 @@ server { {{ end }} {{ end }} - {{ if eq $service_name "librepatron" }} + {{ if eq $host_name "librepatron" }} {{ range $container := $ }} {{ $serviceName := (index $container.Labels "com.docker.compose.service") }} {{ if (eq $serviceName "isso") }} diff --git a/docker-compose-generator/docker-fragments/btcpayserver-nginx.yml b/docker-compose-generator/docker-fragments/btcpayserver-nginx.yml index 9a65d4a..19af857 100644 --- a/docker-compose-generator/docker-fragments/btcpayserver-nginx.yml +++ b/docker-compose-generator/docker-fragments/btcpayserver-nginx.yml @@ -8,7 +8,7 @@ services: VIRTUAL_NETWORK: nginx-proxy VIRTUAL_PORT: 49392 VIRTUAL_HOST: ${BTCPAY_HOST} - VIRTUAL_SERVICE_NAME: "btcpay" + VIRTUAL_HOST_NAME: "btcpay" SSL_POLICY: Mozilla-Modern # Let's encrypt settings diff --git a/docker-compose-generator/docker-fragments/opt-add-librepatron.yml b/docker-compose-generator/docker-fragments/opt-add-librepatron.yml index 776a1a9..692e7a6 100644 --- a/docker-compose-generator/docker-fragments/opt-add-librepatron.yml +++ b/docker-compose-generator/docker-fragments/opt-add-librepatron.yml @@ -18,7 +18,7 @@ services: VIRTUAL_HOST: ${LIBREPATRON_HOST} LETSENCRYPT_HOST: ${LIBREPATRON_HOST} LETSENCRYPT_EMAIL: ${LETSENCRYPT_EMAIL:-} - VIRTUAL_SERVICE_NAME: "librepatron" + VIRTUAL_HOST_NAME: "librepatron" isso: container_name: isso diff --git a/docker-compose-generator/docker-fragments/opt-add-woocommerce.yml b/docker-compose-generator/docker-fragments/opt-add-woocommerce.yml index ae24f63..a86b688 100644 --- a/docker-compose-generator/docker-fragments/opt-add-woocommerce.yml +++ b/docker-compose-generator/docker-fragments/opt-add-woocommerce.yml @@ -11,7 +11,7 @@ services: LETSENCRYPT_HOST: ${WOOCOMMERCE_HOST} LETSENCRYPT_EMAIL: ${LETSENCRYPT_EMAIL:-} VIRTUAL_HOST: ${WOOCOMMERCE_HOST} - VIRTUAL_SERVICE_NAME: "woocommerce" + VIRTUAL_HOST_NAME: "woocommerce" expose: - "80" links: From c70dbb8916f4d26de408524493147e1b92578780 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Sat, 16 Mar 2019 15:13:37 +0900 Subject: [PATCH 2/2] Refactor the redirects of nginx inside a template --- Production/nginx.tmpl | 133 +++++++++++++++++++++--------------------- 1 file changed, 67 insertions(+), 66 deletions(-) diff --git a/Production/nginx.tmpl b/Production/nginx.tmpl index 9093e8c..4c4aaf6 100644 --- a/Production/nginx.tmpl +++ b/Production/nginx.tmpl @@ -21,6 +21,72 @@ {{ end }} +{{ define "redirects" }} + {{ if eq $.HostName "btcpay" }} + {{ range $container := $.Containers }} + {{ $serviceName := (index $container.Labels "com.docker.compose.service") }} + {{ if (eq $serviceName "lnd_bitcoin") }} + location /lnrpc.Lightning { + grpc_pass grpcs://lnd_bitcoin:10009; + } + location /lnd-rest/btc/ { + rewrite ^/lnd-rest/btc/(.*) /$1 break; + proxy_pass http://lnd_bitcoin:8080/; + } + {{ end }} + {{ if (eq $serviceName "lnd_bitcoin_rtl") }} + location /rtl/ { + proxy_pass http://lnd_bitcoin_rtl:3000/rtl/; + } + {{ end }} + {{ if (eq $serviceName "btcqbo") }} + location /btcqbo/ { + proxy_pass http://btcqbo:8001; + proxy_redirect off; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } + {{ end }} + {{ if (eq $serviceName "clightning_bitcoin_spark") }} + location /spark/btc/ { + proxy_pass http://clightning_bitcoin_spark:9737/; + } + {{ end }} + {{ if (eq $serviceName "clightning_bitcoin_charge") }} + location /lightning-charge/btc/ { + proxy_pass http://clightning_bitcoin_charge:9112/; + } + {{ end }} + {{ if (eq $serviceName "clightning_groestlcoin_spark") }} + location /spark/grs/ { + proxy_pass http://clightning_groestlcoin_spark:9739/; + } + {{ end }} + {{ if (eq $serviceName "clightning_groestlcoin_charge") }} + location /lightning-charge/grs/ { + proxy_pass http://clightning_groestlcoin_charge:9112/; + } + {{ end }} + {{ end }} + {{ end }} + + {{ if eq $.HostName "librepatron" }} + {{ range $container := $.Containers }} + {{ $serviceName := (index $container.Labels "com.docker.compose.service") }} + {{ if (eq $serviceName "isso") }} + location /isso { + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Script-Name /isso; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_pass http://isso:8080; + } + {{ end }} + {{ end }} + {{ end }} +{{ end }} + # If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the # scheme used to connect to this server map $http_x_forwarded_proto $proxy_x_forwarded_proto { @@ -297,73 +363,8 @@ server { {{ else if (exists "/etc/nginx/vhost.d/default_location") }} include /etc/nginx/vhost.d/default_location; {{ end }} - } - - {{ if eq $host_name "btcpay" }} - {{ range $container := $ }} - {{ $serviceName := (index $container.Labels "com.docker.compose.service") }} - {{ if (eq $serviceName "lnd_bitcoin") }} - location /lnrpc.Lightning { - grpc_pass grpcs://lnd_bitcoin:10009; - } - location /lnd-rest/btc/ { - rewrite ^/lnd-rest/btc/(.*) /$1 break; - proxy_pass http://lnd_bitcoin:8080/; - } - {{ end }} - {{ if (eq $serviceName "lnd_bitcoin_rtl") }} - location /rtl/ { - proxy_pass http://lnd_bitcoin_rtl:3000/rtl/; - } - {{ end }} - {{ if (eq $serviceName "btcqbo") }} - location /btcqbo/ { - proxy_pass http://btcqbo:8001; - proxy_redirect off; - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } - {{ end }} - {{ if (eq $serviceName "clightning_bitcoin_spark") }} - location /spark/btc/ { - proxy_pass http://clightning_bitcoin_spark:9737/; - } - {{ end }} - {{ if (eq $serviceName "clightning_bitcoin_charge") }} - location /lightning-charge/btc/ { - proxy_pass http://clightning_bitcoin_charge:9112/; - } - {{ end }} - {{ if (eq $serviceName "clightning_groestlcoin_spark") }} - location /spark/grs/ { - proxy_pass http://clightning_groestlcoin_spark:9739/; - } - {{ end }} - {{ if (eq $serviceName "clightning_groestlcoin_charge") }} - location /lightning-charge/grs/ { - proxy_pass http://clightning_groestlcoin_charge:9112/; - } - {{ end }} - - - {{ end }} - {{ end }} - - {{ if eq $host_name "librepatron" }} - {{ range $container := $ }} - {{ $serviceName := (index $container.Labels "com.docker.compose.service") }} - {{ if (eq $serviceName "isso") }} - location /isso { - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Script-Name /isso; - proxy_set_header Host $host; - proxy_set_header X-Forwarded-Proto $scheme; - proxy_pass http://isso:8080; - } - {{ end }} - {{ end }} - {{ end }} + {{ template "redirects" (dict "HostName" $host_name "Containers" $) }} } {{ end }}