我已从 CloudFlare 购买了域名 example.com。在 Cloudflare 中设置证书和私钥。 2 个 docker 镜像在 EC2 的不同端口上运行,Flask Hello Word 在端口 3000 上运行,Angular Hello World 在端口 4000 上运行。 我希望 example.com:3000 为 Flask 提供服务,example.com:4000 为 Angular 提供服务。
在我添加的 CloudFlare 域 DNS 记录下
类型 | 姓名 | 内容 | 代理状态 | TTL |
---|---|---|---|---|
A | example.com | EC2 实例的公共 ipv4 地址 | 仅 DNS | 自动 |
A | www | EC2 实例的公共 ipv4 地址 | 仅 DNS | 自动 |
Flask Dockerfile
# Use Fedora as the base image
FROM fedora
# Update the package repository and upgrade all packages
RUN dnf upgrade -y
# Install pip and other necessary packages
RUN dnf install -y python3-pip gcc python3-devel
# Install Flask (this can be done using requirements.txt if specified)
# RUN pip3 install Flask
# Set the working directory to /app
WORKDIR /app
# Copy the entire contents of the current directory to /app in the container
COPY . .
# Install Python dependencies from requirements.txt
RUN pip3 install -r requirements.txt
# Expose the port that the Flask app will run on
EXPOSE 5000
# Set the environment variable for Flask
ENV FLASK_APP=run.py
# Run the Flask app
CMD ["flask", "run", "--host=0.0.0.0"]
Angular Dockerfile
# Use an official Node.js runtime as a parent image
FROM node:18 AS build
# Set the working directory in the container
WORKDIR /app
# Copy the package.json and package-lock.json files to the working directory
COPY package*.json ./
# Install the dependencies
RUN npm install
# Copy the rest of the application code to the working directory
COPY . .
# Build the Angular application for production
RUN npm run build --prod
# Use an official Nginx runtime as a parent image
FROM nginx:alpine
# Copy the built Angular app from the previous stage to the Nginx html directory
COPY --from=build /app/dist/sample-frontend/browser /usr/share/nginx/html
# Copy the SSL certificates
COPY ssl/origin.pem /etc/nginx/ssl/origin.pem
COPY ssl/origin.key /etc/nginx/ssl/origin.key
# Remove the default Nginx configuration file
RUN rm /etc/nginx/conf.d/default.conf
# Copy the custom Nginx configuration file to the Nginx configuration directory
COPY nginx.conf /etc/nginx/conf.d
# Expose port 443 to the outside world (HTTPS)
EXPOSE 443
# Start Nginx when the container launches
CMD ["nginx", "-g", "daemon off;"]
Angular nginx.conf
# HTTP (port 80) to HTTPS (port 443) redirect
server {
listen 80;
server_name example.com www.example.com;
# Redirect all HTTP requests to HTTPS
return 301 https://$host$request_uri;
}
# HTTPS server block
server {
listen 443 ssl;
server_name example.com www.example.com;
# SSL Certificate & Key
ssl_certificate /etc/nginx/ssl/origin.pem;
ssl_certificate_key /etc/nginx/ssl/origin.key;
# Enable Strong SSL
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers on;
root /usr/share/nginx/html;
index index.html;
# Serve Angular application
location / {
try_files $uri $uri/ /index.html;
}
# Enable Gzip Compression
gzip on;
gzip_types text/plain application/javascript text/css application/json;
gzip_min_length 256;
}
我上面遇到的问题和我理解和解决问题的方法不匹配。因此出现了上述问题陈述。我想在 1 个 EC2 实例上运行 2 个独立的应用程序,一个 Flask (Python) 和一个 Angular (Node.js),并能够访问它们。我找到了一个解决方案,下面是我的 GitHub 存储库的链接,其中详细解释了该方法。它适合我的需求,并且还有改进的空间。 两个应用程序-aws-docker