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

asked 11 days ago286 views
1 Answer
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
GK
answered 11 days ago
profile picture
EXPERT
reviewed 11 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions