diff --git a/.gitignore b/.gitignore index 19fb83c2..a0278c3a 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,4 @@ post_restore.sh .vscode *.code-workspace - +.idea diff --git a/.templates/adminer/service.yml b/.templates/adminer/service.yml old mode 100644 new mode 100755 index 95721843..f7428985 --- a/.templates/adminer/service.yml +++ b/.templates/adminer/service.yml @@ -4,4 +4,6 @@ adminer: restart: unless-stopped ports: - "9080:8080" - + environment: + - VIRTUAL_HOST=~^adminer\..*\.xip\.io + - VIRTUAL_PORT=9080 diff --git a/.templates/blynk_server/service.yml b/.templates/blynk_server/service.yml old mode 100644 new mode 100755 index 28c3e01b..3f3665ef --- a/.templates/blynk_server/service.yml +++ b/.templates/blynk_server/service.yml @@ -16,4 +16,7 @@ blynk_server: volumes: - ./volumes/blynk_server/data:/data - ./volumes/blynk_server/config:/config + environment: + - VIRTUAL_HOST=~^blynk\..*\.xip\.io + - VIRTUAL_PORT=8180 diff --git a/.templates/diyhue/service.yml b/.templates/diyhue/service.yml old mode 100644 new mode 100755 index 3038ae84..0b1b6294 --- a/.templates/diyhue/service.yml +++ b/.templates/diyhue/service.yml @@ -12,4 +12,6 @@ diyhue: volumes: - ./volumes/diyhue:/opt/hue-emulator/export restart: unless-stopped - + environment: + - VIRTUAL_HOST=~^diyhue\..*\.xip\.io + - VIRTUAL_PORT=8070 \ No newline at end of file diff --git a/.templates/grafana/service.yml b/.templates/grafana/service.yml old mode 100644 new mode 100755 index 594267ae..e2c9cd96 --- a/.templates/grafana/service.yml +++ b/.templates/grafana/service.yml @@ -18,4 +18,6 @@ grafana: timeout: 10s retries: 3 start_period: 30s - + environment: + - VIRTUAL_HOST=~^grafana\..*\.xip\.io + - VIRTUAL_PORT=3000 \ No newline at end of file diff --git a/.templates/home_assistant/service.yml b/.templates/home_assistant/service.yml index a03c4b1e..fef1c369 100644 --- a/.templates/home_assistant/service.yml +++ b/.templates/home_assistant/service.yml @@ -9,8 +9,8 @@ home_assistant: - /etc/localtime:/etc/localtime:ro - ./volumes/home_assistant:/config - /var/run/dbus/system_bus_socket:/var/run/dbus/system_bus_socket - devices: - - "/dev/ttyAMA0:/dev/ttyAMA0" - - "/dev/vcio:/dev/vcio" - - "/dev/gpiomem:/dev/gpiomem" +# devices: +# - "/dev/ttyAMA0:/dev/ttyAMA0" +# - "/dev/vcio:/dev/vcio" +# - "/dev/gpiomem:/dev/gpiomem" privileged: true diff --git a/.templates/homebridge/service.yml b/.templates/homebridge/service.yml old mode 100644 new mode 100755 index c2461c40..7d970049 --- a/.templates/homebridge/service.yml +++ b/.templates/homebridge/service.yml @@ -11,4 +11,6 @@ homebridge: volumes: - ./volumes/homebridge:/homebridge network_mode: host - + environment: + - VIRTUAL_HOST=~^homebridge\..*\.xip\.io + - VIRTUAL_PORT=8380 \ No newline at end of file diff --git a/.templates/influxdb/service.yml b/.templates/influxdb/service.yml old mode 100644 new mode 100755 index 34793cc6..762a392d --- a/.templates/influxdb/service.yml +++ b/.templates/influxdb/service.yml @@ -1,3 +1,4 @@ +<<<<<<< HEAD influxdb: container_name: influxdb image: "influxdb:1.8" @@ -25,3 +26,21 @@ influxdb: retries: 3 start_period: 30s +======= + influxdb: + container_name: influxdb + image: "influxdb:latest" + restart: unless-stopped + ports: + - 8086:8086 + - 8083:8083 + - 2003:2003 + env_file: + - ./services/influxdb/influxdb.env + environment: + - VIRTUAL_HOST=~^influxdb\..*\.xip\.io + - VIRTUAL_PORT=8086 + volumes: + - ./volumes/influxdb/data:/var/lib/influxdb + - ./backups/influxdb/db:/var/lib/influxdb/backup +>>>>>>> other-IOTstack/feature/AddNginxProxy diff --git a/.templates/nextcloud/service.yml b/.templates/nextcloud/service.yml old mode 100644 new mode 100755 index 9a31651a..e6b07d37 --- a/.templates/nextcloud/service.yml +++ b/.templates/nextcloud/service.yml @@ -16,6 +16,9 @@ nextcloud: networks: - default - nextcloud + environment: + - VIRTUAL_HOST=~^nextcloud\..*\.xip\.io + - VIRTUAL_PORT=9321 nextcloud_db: container_name: nextcloud_db diff --git a/.templates/nginx-gen/nginx.tmpl b/.templates/nginx-gen/nginx.tmpl new file mode 100755 index 00000000..8b73280f --- /dev/null +++ b/.templates/nginx-gen/nginx.tmpl @@ -0,0 +1,402 @@ +{{ $CurrentContainer := where $ "ID" .Docker.CurrentContainerID | first }} + +{{ $external_http_port := coalesce $.Env.HTTP_PORT "80" }} +{{ $external_https_port := coalesce $.Env.HTTPS_PORT "443" }} + +{{ define "upstream" }} + {{ if .Address }} + {{/* If we got the containers from swarm and this container's port is published to host, use host IP:PORT */}} + {{ if and .Container.Node.ID .Address.HostPort }} + # {{ .Container.Node.Name }}/{{ .Container.Name }} + server {{ .Container.Node.Address.IP }}:{{ .Address.HostPort }}; + {{/* If there is no swarm node or the port is not published on host, use container's IP:PORT */}} + {{ else if .Network }} + # {{ .Container.Name }} + server {{ .Network.IP }}:{{ .Address.Port }}; + {{ end }} + {{ else if .Network }} + # {{ .Container.Name }} + {{ if .Network.IP }} + server {{ .Network.IP }} down; + {{ else }} + server 127.0.0.1 down; + {{ end }} + {{ end }} + +{{ end }} + +{{ define "ssl_policy" }} + {{ if eq .ssl_policy "Mozilla-Modern" }} + ssl_protocols TLSv1.3; + {{/* nginx currently lacks ability to choose ciphers in TLS 1.3 in configuration, see https://trac.nginx.org/nginx/ticket/1529 /*}} + {{/* a possible workaround can be modify /etc/ssl/openssl.cnf to change it globally (see https://trac.nginx.org/nginx/ticket/1529#comment:12 ) /*}} + {{/* explicitly set ngnix default value in order to allow single servers to override the global http value */}} + ssl_ciphers HIGH:!aNULL:!MD5; + ssl_prefer_server_ciphers off; + {{ else if eq .ssl_policy "Mozilla-Intermediate" }} + ssl_protocols TLSv1.2 TLSv1.3; + ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384'; + ssl_prefer_server_ciphers off; + {{ else if eq .ssl_policy "Mozilla-Old" }} + ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; + ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA'; + ssl_prefer_server_ciphers on; + {{ else if eq .ssl_policy "AWS-TLS-1-2-2017-01" }} + ssl_protocols TLSv1.2 TLSv1.3; + ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:AES128-GCM-SHA256:AES128-SHA256:AES256-GCM-SHA384:AES256-SHA256'; + ssl_prefer_server_ciphers on; + {{ else if eq .ssl_policy "AWS-TLS-1-1-2017-01" }} + ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; + ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA'; + ssl_prefer_server_ciphers on; + {{ else if eq .ssl_policy "AWS-2016-08" }} + ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; + ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA'; + ssl_prefer_server_ciphers on; + {{ else if eq .ssl_policy "AWS-2015-05" }} + ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; + ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:DES-CBC3-SHA'; + ssl_prefer_server_ciphers on; + {{ else if eq .ssl_policy "AWS-2015-03" }} + ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; + ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:DHE-DSS-AES128-SHA:DES-CBC3-SHA'; + ssl_prefer_server_ciphers on; + {{ else if eq .ssl_policy "AWS-2015-02" }} + ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; + ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:DHE-DSS-AES128-SHA'; + ssl_prefer_server_ciphers on; + {{ 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 { + default $http_x_forwarded_proto; + '' $scheme; +} + +# If we receive X-Forwarded-Port, pass it through; otherwise, pass along the +# server port the client connected to +map $http_x_forwarded_port $proxy_x_forwarded_port { + default $http_x_forwarded_port; + '' $server_port; +} + +# If we receive Upgrade, set Connection to "upgrade"; otherwise, delete any +# Connection header that may have been passed to this server +map $http_upgrade $proxy_connection { + default upgrade; + '' close; +} + +# Apply fix for very long server names +server_names_hash_bucket_size 128; + +# Default dhparam +{{ if (exists "/etc/nginx/dhparam/dhparam.pem") }} +ssl_dhparam /etc/nginx/dhparam/dhparam.pem; +{{ end }} + +# Set appropriate X-Forwarded-Ssl header +map $scheme $proxy_x_forwarded_ssl { + default off; + https on; +} + +gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; + +log_format vhost '$host $remote_addr - $remote_user [$time_local] ' + '"$request" $status $body_bytes_sent ' + '"$http_referer" "$http_user_agent"'; + +access_log off; + +{{/* Get the SSL_POLICY defined by this container, falling back to "Mozilla-Intermediate" */}} +{{ $ssl_policy := or ($.Env.SSL_POLICY) "Mozilla-Intermediate" }} +{{ template "ssl_policy" (dict "ssl_policy" $ssl_policy) }} + +{{ if $.Env.RESOLVERS }} +resolver {{ $.Env.RESOLVERS }}; +{{ end }} + +{{ if (exists "/etc/nginx/proxy.conf") }} +include /etc/nginx/proxy.conf; +{{ else }} +# HTTP 1.1 support +proxy_http_version 1.1; +proxy_buffering off; +proxy_set_header Host $http_host; +proxy_set_header Upgrade $http_upgrade; +proxy_set_header Connection $proxy_connection; +proxy_set_header X-Real-IP $remote_addr; +proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; +proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto; +proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl; +proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port; + +# Mitigate httpoxy attack (see README for details) +proxy_set_header Proxy ""; +{{ end }} + +{{ $access_log := (or (and (not $.Env.DISABLE_ACCESS_LOGS) "access_log /var/log/nginx/access.log vhost;") "") }} + +{{ $enable_ipv6 := eq (or ($.Env.ENABLE_IPV6) "") "true" }} +server { + server_name _; # This is just an invalid value which will never trigger on a real hostname. + listen {{ $external_http_port }}; + {{ if $enable_ipv6 }} + listen [::]:{{ $external_http_port }}; + {{ end }} + {{ $access_log }} + return 503; +} + +{{ if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }} +server { + server_name _; # This is just an invalid value which will never trigger on a real hostname. + listen {{ $external_https_port }} ssl http2; + {{ if $enable_ipv6 }} + listen [::]:{{ $external_https_port }} ssl http2; + {{ end }} + {{ $access_log }} + return 503; + + ssl_session_cache shared:SSL:50m; + ssl_session_tickets off; + ssl_certificate /etc/nginx/certs/default.crt; + ssl_certificate_key /etc/nginx/certs/default.key; +} +{{ end }} + +{{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }} + +{{ $host := trim $host }} +{{ $is_regexp := hasPrefix "~" $host }} +{{ $upstream_name := when $is_regexp (sha1 $host) $host }} + +# {{ $host }} +upstream {{ $upstream_name }} { + +{{ range $container := $containers }} + {{ $addrLen := len $container.Addresses }} + + {{ range $knownNetwork := $CurrentContainer.Networks }} + {{ range $containerNetwork := $container.Networks }} + {{ if (and (ne $containerNetwork.Name "ingress") (or (eq $knownNetwork.Name $containerNetwork.Name) (eq $knownNetwork.Name "host"))) }} + ## Can be connected with "{{ $containerNetwork.Name }}" network + + {{/* If only 1 port exposed, use that */}} + {{ if eq $addrLen 1 }} + {{ $address := index $container.Addresses 0 }} + {{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }} + {{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}} + {{ else }} + {{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }} + {{ $address := where $container.Addresses "Port" $port | first }} + {{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }} + {{ end }} + {{ else }} + # Cannot connect to network of this container + server 127.0.0.1 down; + {{ end }} + {{ end }} + {{ end }} +{{ end }} +} + +{{ $default_host := or ($.Env.DEFAULT_HOST) "" }} +{{ $default_server := index (dict $host "" $default_host "default_server") $host }} + +{{/* 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") }} + +{{/* 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" }} + +{{/* Get the HTTPS_METHOD defined by containers w/ the same vhost, falling back to "redirect" */}} +{{ $https_method := or (first (groupByKeys $containers "Env.HTTPS_METHOD")) "redirect" }} + +{{/* Get the SSL_POLICY defined by containers w/ the same vhost, falling back to empty string (use default) */}} +{{ $ssl_policy := or (first (groupByKeys $containers "Env.SSL_POLICY")) "" }} + +{{/* Get the HSTS defined by containers w/ the same vhost, falling back to "max-age=31536000" */}} +{{ $hsts := or (first (groupByKeys $containers "Env.HSTS")) "max-age=31536000" }} + +{{/* 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 cert name defined by containers w/ the same vhost */}} +{{ $certName := (first (groupByKeys $containers "Env.CERT_NAME")) }} + +{{/* Get the best matching cert by name for the vhost. */}} +{{ $vhostCert := (closest (dir "/etc/nginx/certs") (printf "%s.crt" $host))}} + +{{/* vhostCert is actually a filename so remove any suffixes since they are added later */}} +{{ $vhostCert := trimSuffix ".crt" $vhostCert }} +{{ $vhostCert := trimSuffix ".key" $vhostCert }} + +{{/* Use the cert specified on the container or fallback to the best vhost match */}} +{{ $cert := (coalesce $certName $vhostCert) }} + +{{ $is_https := (and (ne $https_method "nohttps") (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert))) }} + +{{ if $is_https }} + +{{ if eq $https_method "redirect" }} +server { + server_name {{ $host }}; + listen {{ $external_http_port }} {{ $default_server }}; + {{ if $enable_ipv6 }} + listen [::]:{{ $external_http_port }} {{ $default_server }}; + {{ end }} + {{ $access_log }} + + # Do not HTTPS redirect Let'sEncrypt ACME challenge + location /.well-known/acme-challenge/ { + auth_basic off; + allow all; + root /usr/share/nginx/html; + try_files $uri =404; + break; + } + + location / { + return 301 https://$host$request_uri; + } +} +{{ end }} + +server { + server_name {{ $host }}; + listen {{ $external_https_port }} ssl http2 {{ $default_server }}; + {{ if $enable_ipv6 }} + listen [::]:{{ $external_https_port }} ssl http2 {{ $default_server }}; + {{ end }} + {{ $access_log }} + + {{ if eq $network_tag "internal" }} + # Only allow traffic from internal clients + include /etc/nginx/network_internal.conf; + {{ end }} + + {{ template "ssl_policy" (dict "ssl_policy" $ssl_policy) }} + + ssl_session_timeout 5m; + ssl_session_cache shared:SSL:50m; + ssl_session_tickets off; + + ssl_certificate /etc/nginx/certs/{{ (printf "%s.crt" $cert) }}; + ssl_certificate_key /etc/nginx/certs/{{ (printf "%s.key" $cert) }}; + + {{ if (exists (printf "/etc/nginx/certs/%s.dhparam.pem" $cert)) }} + ssl_dhparam {{ printf "/etc/nginx/certs/%s.dhparam.pem" $cert }}; + {{ end }} + + {{ if (exists (printf "/etc/nginx/certs/%s.chain.pem" $cert)) }} + ssl_stapling on; + ssl_stapling_verify on; + ssl_trusted_certificate {{ printf "/etc/nginx/certs/%s.chain.pem" $cert }}; + {{ end }} + + {{ if (not (or (eq $https_method "noredirect") (eq $hsts "off"))) }} + add_header Strict-Transport-Security "{{ trim $hsts }}" always; + {{ end }} + + {{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }} + include {{ printf "/etc/nginx/vhost.d/%s" $host }}; + {{ else if (exists "/etc/nginx/vhost.d/default") }} + include /etc/nginx/vhost.d/default; + {{ end }} + + location / { + {{ if eq $proto "uwsgi" }} + include uwsgi_params; + uwsgi_pass {{ trim $proto }}://{{ trim $upstream_name }}; + {{ else if eq $proto "fastcgi" }} + root {{ trim $vhost_root }}; + include fastcgi_params; + fastcgi_pass {{ trim $upstream_name }}; + {{ else if eq $proto "grpc" }} + grpc_pass {{ trim $proto }}://{{ trim $upstream_name }}; + {{ else }} + proxy_pass {{ trim $proto }}://{{ trim $upstream_name }}; + {{ end }} + + {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }} + auth_basic "Restricted {{ $host }}"; + auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }}; + {{ end }} + {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }} + include {{ printf "/etc/nginx/vhost.d/%s_location" $host}}; + {{ else if (exists "/etc/nginx/vhost.d/default_location") }} + include /etc/nginx/vhost.d/default_location; + {{ end }} + } +} + +{{ end }} + +{{ if or (not $is_https) (eq $https_method "noredirect") }} + +server { + server_name {{ $host }}; + listen {{ $external_http_port }} {{ $default_server }}; + {{ if $enable_ipv6 }} + listen [::]:80 {{ $default_server }}; + {{ end }} + {{ $access_log }} + + {{ if eq $network_tag "internal" }} + # Only allow traffic from internal clients + include /etc/nginx/network_internal.conf; + {{ end }} + + {{ if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }} + include {{ printf "/etc/nginx/vhost.d/%s" $host }}; + {{ else if (exists "/etc/nginx/vhost.d/default") }} + include /etc/nginx/vhost.d/default; + {{ end }} + + location / { + {{ if eq $proto "uwsgi" }} + include uwsgi_params; + uwsgi_pass {{ trim $proto }}://{{ trim $upstream_name }}; + {{ else if eq $proto "fastcgi" }} + root {{ trim $vhost_root }}; + include fastcgi_params; + fastcgi_pass {{ trim $upstream_name }}; + {{ else if eq $proto "grpc" }} + grpc_pass {{ trim $proto }}://{{ trim $upstream_name }}; + {{ else }} + proxy_pass {{ trim $proto }}://{{ trim $upstream_name }}; + {{ end }} + {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }} + auth_basic "Restricted {{ $host }}"; + auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }}; + {{ end }} + {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }} + include {{ printf "/etc/nginx/vhost.d/%s_location" $host}}; + {{ else if (exists "/etc/nginx/vhost.d/default_location") }} + include /etc/nginx/vhost.d/default_location; + {{ end }} + } +} + +{{ if (and (not $is_https) (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }} +server { + server_name {{ $host }}; + listen {{ $external_https_port }} ssl http2 {{ $default_server }}; + {{ if $enable_ipv6 }} + listen [::]:{{ $external_https_port }} ssl http2 {{ $default_server }}; + {{ end }} + {{ $access_log }} + return 500; + + ssl_certificate /etc/nginx/certs/default.crt; + ssl_certificate_key /etc/nginx/certs/default.key; +} +{{ end }} + +{{ end }} +{{ end }} \ No newline at end of file diff --git a/.templates/nginx-gen/service.yml b/.templates/nginx-gen/service.yml new file mode 100755 index 00000000..f51fa9e7 --- /dev/null +++ b/.templates/nginx-gen/service.yml @@ -0,0 +1,12 @@ +nginx-gen: + image: ajoubert/docker-gen-arm + command: -notify-sighup nginx-proxy -watch -wait 5s:30s /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf + container_name: nginx-gen + restart: unless-stopped + volumes: + - /var/run/docker.sock:/tmp/docker.sock:ro + - "./volumes/nginx-proxy/nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl:ro" + - "./volumes/nginx-proxy/nginx-conf:/etc/nginx/conf.d" + - "./volumes/nginx-proxy/nginx-vhost:/etc/nginx/vhost.d" + - "./volumes/nginx-proxy/html:/usr/share/nginx/html" + - "./volumes/nginx-proxy/certs:/etc/nginx/certs:ro" diff --git a/.templates/nginx-letsencrypt/service.yml b/.templates/nginx-letsencrypt/service.yml new file mode 100755 index 00000000..3fe13855 --- /dev/null +++ b/.templates/nginx-letsencrypt/service.yml @@ -0,0 +1,13 @@ +nginx-letsencrypt: + image: budry/jrcs-letsencrypt-nginx-proxy-companion-arm + container_name: nginx-letsencrypt + restart: unless-stopped + volumes: + - /var/run/docker.sock:/var/run/docker.sock:ro + - "./volumes/nginx-proxy/nginx-conf:/etc/nginx/conf.d" + - "./volumes/nginx-proxy/nginx-vhost:/etc/nginx/vhost.d" + - "./volumes/nginx-proxy/html:/usr/share/nginx/html" + - "./volumes/nginx-proxy/certs:/etc/nginx/certs:rw" + environment: + NGINX_DOCKER_GEN_CONTAINER: "nginx-gen" + NGINX_PROXY_CONTAINER: "nginx-proxy" diff --git a/.templates/nginx-proxy/directoryfix.sh b/.templates/nginx-proxy/directoryfix.sh new file mode 100755 index 00000000..eea0c829 --- /dev/null +++ b/.templates/nginx-proxy/directoryfix.sh @@ -0,0 +1,27 @@ +#!/bin/bash +if [ ! -d ./volumes/nginx-proxy/nginx-conf ]; then + sudo mkdir -p ./volumes/nginx-proxy/nginx-conf + sudo chown -R pi:pi ./volumes/nginx-proxy/nginx-conf +fi + +if [ ! -d ./volumes/nginx-proxy/nginx-vhost ]; then + sudo mkdir -p ./volumes/nginx-proxy/nginx-vhost + sudo chown -R pi:pi ./volumes/nginx-proxy/nginx-vhost +fi + +if [ ! -d ./volumes/nginx-proxy/html ]; then + sudo mkdir -p ./volumes/nginx-proxy/html + sudo chown -R pi:pi ./volumes/nginx-proxy/html +fi + +if [ ! -d ./volumes/nginx-proxy/certs ]; then + sudo mkdir -p ./volumes/nginx-proxy/certs + sudo chown -R pi:pi ./volumes/nginx-proxy/certs +fi + +if [ ! -d ./volumes/nginx-proxy/log ]; then + sudo mkdir -p ./volumes/nginx-proxy/log + sudo chown -R pi:pi ./volumes/nginx-proxy/log +fi + +cp ./services/nginx-proxy//nginx.tmpl ./volumes/nginx-proxy diff --git a/.templates/nginx-proxy/service.yml b/.templates/nginx-proxy/service.yml new file mode 100755 index 00000000..09de2951 --- /dev/null +++ b/.templates/nginx-proxy/service.yml @@ -0,0 +1,22 @@ +# As described in https://github.com/nginx-proxy/nginx-proxy. We use separate containers. +# The HTTPS implementation as documented here: https://medium.com/@francoisromain/host-multiple-websites-with-https-inside-docker-containers-on-a-single-server-18467484ab95 +# We are using xip.io to be able make subdomains for IP address. So the nodered service can be accessed: +# nodered.X.X.X.X.xip.org where X.X.X.X is the IP address of IOTstack. Any other domain can be used, on that case please +# replace VIRTUAL_HOST env variable of the given instance with the coresponding value. + +nginx-proxy: + image: nginx + container_name: nginx-proxy + network_mode: host + restart: unless-stopped # Always restart container + ports: + - "80:80" # Port mappings in format host:container + - "443:443" + labels: + - "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy" # Label needed for Let's Encrypt companion container + volumes: # Volumes needed for container to configure proixes and access certificates genereated by Let's Encrypt companion container + - "./volumes/nginx-proxy/nginx-conf:/etc/nginx/conf.d" + - "./volumes/nginx-proxy/nginx-vhost:/etc/nginx/vhost.d" + - "./volumes/nginx-proxy/html:/usr/share/nginx/html" + - "./volumes/nginx-proxy/certs:/etc/nginx/certs:ro" + - "./volumes/nginx-proxy/log:/var/log/nginx" \ No newline at end of file diff --git a/.templates/nodered/addons.yml b/.templates/nodered/addons.yml index bb0fc38d..5ce2e504 100644 --- a/.templates/nodered/addons.yml +++ b/.templates/nodered/addons.yml @@ -5,13 +5,13 @@ comment: "Addons available for NodeRed" dockerFileInstallCommand: "RUN cd /usr/src/node-red && npm install --save " addons: default_on: - - "node-red-node-pi-gpiod" - "node-red-contrib-influxdb" - "node-red-contrib-boolean-logic" - "node-red-node-rbe" - "node-red-configurable-ping" - "node-red-dashboard" default_off: + - "node-red-node-pi-gpiod" - "node-red-node-openweathermap" - "node-red-contrib-discord" - "node-red-node-email" diff --git a/.templates/nodered/service.yml b/.templates/nodered/service.yml old mode 100644 new mode 100755 index c567f7fa..4fa84872 --- a/.templates/nodered/service.yml +++ b/.templates/nodered/service.yml @@ -16,8 +16,10 @@ nodered: - ./volumes/nodered/ssh:/root/.ssh - /var/run/docker.sock:/var/run/docker.sock - /var/run/dbus/system_bus_socket:/var/run/dbus/system_bus_socket - devices: - - "/dev/ttyAMA0:/dev/ttyAMA0" - - "/dev/vcio:/dev/vcio" - - "/dev/gpiomem:/dev/gpiomem" - +# devices: +# - "/dev/ttyAMA0:/dev/ttyAMA0" +# - "/dev/vcio:/dev/vcio" +# - "/dev/gpiomem:/dev/gpiomem" + environment: + - VIRTUAL_HOST=~^nodered\..*\.xip\.io + - VIRTUAL_PORT=1880 \ No newline at end of file diff --git a/.templates/openhab/service.yml b/.templates/openhab/service.yml old mode 100644 new mode 100755 index 45d5f3f6..90629794 --- a/.templates/openhab/service.yml +++ b/.templates/openhab/service.yml @@ -17,4 +17,6 @@ openhab: # options: # max-size: "5m" # max-file: "3" - + environment: + - VIRTUAL_HOST=~^openhab\..*\.xip\.io + - VIRTUAL_PORT=8080 diff --git a/.templates/pihole/service.yml b/.templates/pihole/service.yml old mode 100644 new mode 100755 index 341f3408..c1135e1f --- a/.templates/pihole/service.yml +++ b/.templates/pihole/service.yml @@ -19,4 +19,6 @@ pihole: cap_add: - NET_ADMIN restart: unless-stopped - + environment: + - VIRTUAL_HOST=~^pihole\..*\.xip\.io + - VIRTUAL_PORT=8089 \ No newline at end of file diff --git a/.templates/portainer-ce/service.yml b/.templates/portainer-ce/service.yml index 6d5e0914..fce5fd5b 100644 --- a/.templates/portainer-ce/service.yml +++ b/.templates/portainer-ce/service.yml @@ -8,4 +8,6 @@ portainer-ce: volumes: - /var/run/docker.sock:/var/run/docker.sock - ./volumes/portainer-ce/data:/data - + environment: + - VIRTUAL_HOST=~^portainer\..*\.xip\.io + - VIRTUAL_PORT=9000 diff --git a/.templates/portainer/service.yml b/.templates/portainer/service.yml new file mode 100755 index 00000000..68416d2e --- /dev/null +++ b/.templates/portainer/service.yml @@ -0,0 +1,12 @@ + portainer: + container_name: portainer + image: portainer/portainer + restart: unless-stopped + ports: + - 9000:9000 + environment: + - VIRTUAL_HOST=~^portainer\..*\.xip\.io + - VIRTUAL_PORT=9000 + volumes: + - /var/run/docker.sock:/var/run/docker.sock + - ./volumes/portainer/data:/data diff --git a/.templates/tasmoadmin/service.yml b/.templates/tasmoadmin/service.yml old mode 100644 new mode 100755 index 2c5f9371..d09d0f34 --- a/.templates/tasmoadmin/service.yml +++ b/.templates/tasmoadmin/service.yml @@ -1,3 +1,4 @@ +<<<<<<< HEAD tasmoadmin: container_name: tasmoadmin image: ghcr.io/tasmoadmin/tasmoadmin:latest @@ -9,3 +10,17 @@ tasmoadmin: volumes: - ./volumes/tasmoadmin/data:/data +======= + tasmoadmin: + container_name: tasmoadmin + image: raymondmm/tasmoadmin + restart: unless-stopped + ports: + - "8088:80" + volumes: + - ./volumes/tasmoadmin/data:/data + environment: + - VIRTUAL_HOST=~^tasmoadmin\..*\.xip\.io + - VIRTUAL_PORT=8088 + +>>>>>>> other-IOTstack/feature/AddNginxProxy diff --git a/.templates/webthings_gateway/service.yml b/.templates/webthings_gateway/service.yml new file mode 100755 index 00000000..a0c07ad0 --- /dev/null +++ b/.templates/webthings_gateway/service.yml @@ -0,0 +1,16 @@ + webthings_gateway: + image: mozillaiot/gateway:arm + container_name: webthings_gateway + network_mode: host + #ports: + # - 8080:8080 + # - 4443:4443 + #devices: + # - /dev/ttyACM0:/dev/ttyACM0 + #environment: + # - VIRTUAL_HOST=~^webthings\..*\.xip\.io + # - VIRTUAL_PORT=8080 + volumes: + - ./volumes/webthings_gateway/share:/home/node/.mozilla-iot + + \ No newline at end of file diff --git a/scripts/docker_commands.py b/scripts/docker_commands.py index e3bfa9c9..e5216f3f 100755 --- a/scripts/docker_commands.py +++ b/scripts/docker_commands.py @@ -30,8 +30,8 @@ def onResize(sig, action): def startStack(): print("Start Stack:") - print("docker-compose up -d --remove-orphans") - subprocess.call("docker-compose up -d", shell=True) + print("docker compose up -d --remove-orphans") + subprocess.call("docker compose up -d", shell=True) print("") print("Stack Started") input("Process terminated. Press [Enter] to show menu and continue.") @@ -41,12 +41,12 @@ def startStack(): def restartStack(): print("Restarting Stack...") print("Stop Stack:") - print("docker-compose down") - subprocess.call("docker-compose down", shell=True) + print("docker compose down") + subprocess.call("docker compose down", shell=True) print("") print("Start Stack:") - print("docker-compose up -d --remove-orphans") - subprocess.call("docker-compose up -d", shell=True) + print("docker compose up -d --remove-orphans") + subprocess.call("docker compose up -d", shell=True) # print("docker-compose restart") # subprocess.call("docker-compose restart", shell=True) print("") @@ -57,8 +57,8 @@ def restartStack(): def stopStack(): print("Stop Stack:") - print("docker-compose down") - subprocess.call("docker-compose down", shell=True) + print("docker compose down") + subprocess.call("docker compose down", shell=True) print("") print("Stack Stopped") input("Process terminated. Press [Enter] to show menu and continue.") @@ -85,17 +85,17 @@ def pruneVolumes(): def updateAllContainers(): print("Update All Containers:") - print("docker-compose down") + print("docker compose down") subprocess.call("docker-compose down", shell=True) print("") - print("docker-compose pull") - subprocess.call("docker-compose pull", shell=True) + print("docker compose pull") + subprocess.call("docker compose pull", shell=True) print("") - print("docker-compose build") - subprocess.call("docker-compose build", shell=True) + print("docker compose build") + subprocess.call("docker compose build", shell=True) print("") - print("docker-compose up -d") - subprocess.call("docker-compose up -d", shell=True) + print("docker compose up -d") + subprocess.call("docker compose up -d", shell=True) print("") input("Process terminated. Press [Enter] to show menu and continue.") needsRender = 1 @@ -124,9 +124,9 @@ def monitorLogs(): print("Press CTRL+X or CTRL+C to exit.") time.sleep(2) print("") - print("docker-compose logs -f") + print("docker compose logs -f") time.sleep(0.5) - subprocess.call("docker-compose logs -f", shell=True) + subprocess.call("docker compose logs -f", shell=True) print("") time.sleep(0.5) input("Process terminated. Press [Enter] to show menu and continue.") @@ -291,4 +291,4 @@ def isMenuItemSelectable(menu, index): originalSignalHandler = signal.getsignal(signal.SIGINT) main() -signal.signal(signal.SIGWINCH, originalSignalHandler) \ No newline at end of file +signal.signal(signal.SIGWINCH, originalSignalHandler)