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

質問済み 1ヶ月前461ビュー
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
回答済み 1ヶ月前
profile picture
エキスパート
レビュー済み 1ヶ月前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ