修改系统源
# apt源
sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
# pip源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
nginx配置
worker_processes 1;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
server_tokens off;
client_max_body_size 20m;
client_body_buffer_size 20m;
keepalive_timeout 65;
sendfile on;
tcp_nodelay on;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:2m;
gzip on;
gzip_static on;
gzip_types text/plain application/json application/javascript application/x-javascript text/css application/xml text/javascript;
gzip_proxied any;
gzip_vary on;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.0;
server {
listen 80;
# 服务器名称
server_name localhost;
location / {
index index.html index.htm;
root /usr/share/nginx/html;
try_files $uri $uri/ /index.html;
}
location /admin {
index index.html index.htm;
alias /var/www/html/admin/;
try_files $uri $uri/ /index.html;
}
location /api {
proxy_pass http://localhost:8000/api;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 定义 404 页面
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# 定义 50x 页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
Dockerfile
# 构建前端页面的阶段
FROM node:16.14-alpine as frontend-build
WORKDIR /app
COPY frontend/package*.json ./
RUN npm config set registry https://registry.npmmirror.com && npm install -g pnpm && pnpm install
COPY frontend/ ./
RUN pnpm build:production
FROM registry.cn-chengdu.aliyuncs.com/moxiaoying/python-nginx:latest
WORKDIR /app
COPY backend/requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
COPY backend/ /app/
COPY --from=frontend-build /app/dist /usr/share/nginx/html
# 暴露端口
EXPOSE 80
# 启动 Nginx 和后端服务
CMD ["sh", "-c", "nginx -g 'daemon off;' & uvicorn run:app --host 0.0.0.0 --port 8000"]