Connecting ecs fargate backend service to frontend nginx using service connect

0

Im trying to connect my service using service connect, I was connecting them using a load balancer but it seems over kill for my use case, I have an nginx config with my nextjs app, locally I can connect it using the container name of port for example http://backend:8000/ logically it would seem that connecting it using the service connect would be http://backend-srv-discovery:8000/; this is what I config file looks like:

server { listen 80;

  location /backend/ {
proxy_pass http://backend-srv-discovery:8000/;

}

location / {
    root /usr/share/nginx/html/;
    include /etc/nginx/mime.types;
    try_files $uri $uri/ /index.html;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
   proxy_connect_timeout 5s;
proxy_send_timeout 10s;
proxy_read_timeout 30s;
}

}

both my back and frontend are in the same namespace, the backend is discoverable using the client and server option and my frontend is discoverable using the client only option. The current setup ti not working and my logs error is host not found in upstream "backend-srv-discovery" in /etc/nginx/conf.d/default.conf:5

gefragt vor einem Monat463 Aufrufe
1 Antwort
0

Hi ** Problem:** backend-srv-discovery is the service namespace, not the actual hostname for a backend instance. Service Connect uses a different mechanism to locate healthy backend tasks.

Resolution: * Fix your Nginx configuration:*

  • Instead of hardcoding the service namespace, leverage environment variables provided by ECS to access the discovered backend endpoint.
  • In your task definition for the frontend service, add an environment variable named BACKEND_SERVICE_ENDPOINT.
  • Modify your Nginx configuration to use this environment variable:
server {
    listen 80;
    location /backend/ {
        proxy_pass http://$BACKEND_SERVICE_ENDPOINT:8000/;
    }
    # ... other locations
}

profile picture
EXPERTE
GK
beantwortet vor einem Monat
profile picture
EXPERTE
überprüft vor einem Monat

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen