0

I'm using nginx and consul template for a while and working fine. When I delete records of the one service from consul config, nginx getting error like "no servers are inside upstream in" and nginx doesn't work until restart. Probably I need to add "service exist condition" to upstream part , but i could'nt find how to do.

consul config ;

# services.hcl
services {
  id = "svc1"
  name = "service1"
  address = "1.1.1.1"
  port = 8080
}

services {
  id = "svc2"
  name = "service1"
  address = "1.1.1.2"
  port = 8081
  check = {
    http = "http://1.1.1.1/Service1.asmx"
    method = "GET"
    interval = "10s"
    timeout = "1s"
  }
}

I'm using this consul template from [https://stackoverflow.com/questions/71486612/how-can-i-set-automaticly-registered-services-on-nginx-config-using-consul-templ]

thanks

{{- /*

  Upstream template

  This template is used to the upstream configuration for the service.
  It takes an array of service health objects as input and returns the template
  as a string.

*/ -}}
{{- define "upstream-template" -}}
### Upstream configuration for {{ . }}
upstream {{ . }} {
  zone upstream_{{ . }} 128k;
  {{ range service . "any" }}
  # Instance: {{ printf "%s-%s" .ID .Name }}

  {{- /* Mark the backend as down if the health status is critical */ -}}
  {{- $is_down := sprig_ternary " down" "" (eq .Status "critical") }}
  server {{ printf "%s:%d%s" .Address .Port $is_down -}};
  {{ end }}
}
{{ end -}}

{{- /* Obtain list of services from the catalog */ -}}
{{- $servicesList := services -}}

{{- /* Generate upstream configurations for each logical service */ -}}
{{- range $servicesList -}}
  {{- if and (ne .Name "consul") (.Name | contains "sidecar" | not) -}}
    {{- executeTemplate "upstream-template" .Name -}}
  {{- end -}}
{{- end -}}

server {
  listen                443 ssl;
  server_name           domainname.com.tr;
  proxy_set_header      X-Forwarded-Port 443;
  ssl_certificate       /etc/nginx/tls/..crt;
  ssl_certificate_key   /etc/nginx/tls/..key;

  access_log /etc/nginx/log/gw/https/access.log;
  error_log /etc/nginx/log/gw/https/error.log;

  location / {
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_pass https://domainanother;
  }

  {{- /*
    Generate the location blocks for each logical service.
    Ignore the 'consul' service itself, and any sidecar services.
  */ -}}
  {{ range $servicesList -}}
  {{- if and (ne .Name "consul") (.Name | contains "sidecar" | not) }}
  location ^~ /{{ .Name | sprig_title }}.asmx {
    proxy_pass http://{{ .Name }};
    proxy_redirect off;
  }
  {{ end -}}
  {{- end }}
}
'''


  [1]: https://stackoverflow.com/questions/71486612/how-can-i-set-automaticly-registered-services-on-nginx-config-using-consul-templ
zgr
  • 15
  • 3

0 Answers0