Nginx không thể connect được TCP server trên AWS ECS
Chào mọi người, mình đang deploy Rails app lên AWS ECS bằng auto deploy thông qua github actions và ECR.
Các container mình đã build ở local và chạy ổn rồi.
Mình config github actions để đẩy lên ECR và ECS đã thành công, các config về container name trong task definition mình cũng xử lí đúng hết, nhưng khi truy cập đến service thông qua Nginx thì Service ECS "die" và khi check log của Cloudwatch thì phát hiện log như thế này:
Mặc dù trước đấy ở local mình đã build và nginx serve ngon lành.
........................................................................
Đây là file docker-compose.yml của mình
version: "3"
services:
nginx:
build:
context: .
dockerfile: ./docker/nginx/Dockerfile
args:
- HOST=${HOST}
# restart: on-failure
depends_on:
- app
volumes_from:
- app
ports:
- 80:80
- 443:443
env_file:
- .env
links:
- app
app:
platform: linux/x86_64
build:
context: .
dockerfile: ./docker/rails/Dockerfile
ports:
- "3000:3000"
volumes:
- .:/kirin
- tmp_data:/kirin/tmp
- log_data:/kirin/log
- vendor:/kirin/vendor
- gem_dir:/usr/local/bundle
- gem_cache:/usr/local/bundle/gems
env_file: .env
database:
build:
context: .
dockerfile: ./docker/postgresql/Dockerfile
ports:
- "54320:5432"
volumes:
- db_data:/var/lib/postgresql/data
volumes:
gem_cache:
db_data:
tmp_data:
log_data:
vendor:
gem_dir:
Đây là Dockerfile của Nginx:
ARG NGINX_VERSION=1.17.9
FROM nginx:${NGINX_VERSION}
ARG HOST
ARG APP_PATH=/kirin
RUN apt-get update && apt-get -y install \
vim \
curl \
openssl \
apache2-utils \
--no-install-recommends apt-utils && \
rm -r /var/lib/apt/lists/*
WORKDIR ${APP_PATH}
COPY docker/nginx/nginx.conf /tmp/
RUN envsubst '${APP_PATH} ${HOST}' < /tmp/nginx.conf > /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Đây là file config Nginx
events {
worker_connections 768;
}
http {
# Nginx will handle gzip compression of responses from the app server
gzip on;
gzip_proxied any;
gzip_types text/plain application/json;
gzip_min_length 1000;
server{
listen 8080;
location /stub_status {
stub_status on;
}
}
server {
listen 80;
# Nginx will reject anything not matching /api
location / {
# Reject requests with unsupported HTTP method
if ($request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|DELETE)$) {
return 405;
}
# Only requests matching the whitelist expectations will
# get sent to the application server
proxy_pass http://app:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
}
}
Đây là Dockerfile của Rails
FROM ruby:2.7.1
ARG BUNDLER_VERSION=2.3.19
ENV RAILS_ROOT /kirin
ENV BUNDLER_VERSION=${BUNDLER_VERSION}
ENV BUNDLER_PATH=/bundle BUNDLE_BIN=/bundle/bin GEM_HOME=/bundle
ENV PATH="${BUNDLE_BIN}:${PATH}"
RUN mkdir -p $RAILS_ROOT
WORKDIR $RAILS_ROOT
COPY Gemfile Gemfile.lock ./
COPY . ./
RUN apt-get update && \
apt-get install build-essential -y --no-install-recommends \
vim \
git \
gnupg2 \
curl \
wget \
patch \
ruby-dev \
zlib1g-dev \
liblzma-dev \
libmariadb-dev
RUN gem install bundler -v ${BUNDLER_VERSION}
RUN mkdir -p tmp/sockets
RUN gem install spring
EXPOSE 3000
ENTRYPOINT ["docker/entrypoint.sh"]
Hi vọng mọi người xem qua và cho mình giải pháp ạ. Thanks all!
3 ANSWERS
Bạn dùng short name của service thì phải public DNS name cho VPC thì các service mới tìm được nhau. Còn không thì phải dùng kiểu FQN (Fully Qualified Name): <service>.<compose_project_name>.local
@huukimit trong trường hợp này thì FQN của mình là như thế nào b nhỉ, b có thể cho mình xin 1 ví dụ ko ạ?
FQN (Fully Qualified Name): <service>.<compose_project_name>.local
Kiểu như này: http://app.kirin-prod.local:3000
@huukimit vậy p tách làm 2 service à bác, mỗi service chạy 1 container bác nhỉ?
@KaynAssassin Vẫn là 2 container 1 cái là nginx và một cái là của rails app như bạn đang config.
t nghĩ bác nên thêm "links": ["app"]
, trong "containerDefinitions"
object trong file task defination.
@linhdn1198 ko đc bác ạ, hôm qua e cũng thử r, lỗi thế này nhé: ClientException: Links are not supported when networkMode=awsvpc.
ok, t vừa check thấy networkMode=awsvpc
ko support links
. bác thử đổi proxy_pass http://app:3000;
-> proxy_pass localhost:3000;
trong file config nginx
@linhdn1198 ko đc đâu bác ạ, như thế sai config, nginx ko run đc, e thử r
http://localhost:3000
thì sao
@linhdn1198 cũng ko đc bác ạ (
@ngoccong không được bác nhé, nó cũng tương tự localhost thôi bác, e thử r
em làm với node js thì k cần port mapping ở con app chỉ cần ở nginx thôi rồi để proxy pass là http://127.0.0.1:3000/ bác thử xem có giống k
mình cũng làm khong được https://inuvhanoi.com/
Có thể share file ECS task definition ko bác?
@linhdn1198 Đây nhé bác