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

已提问 25 天前460 查看次数
1 回答
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
已回答 25 天前
profile picture
专家
已审核 25 天前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则