Spaces:
Sleeping
Sleeping
Commit ·
3ef791f
1
Parent(s): db16d90
add nginx & start.sh
Browse files- Dockerfile +14 -1
- nginx/nginx.conf +64 -0
- scripts/start.sh +11 -0
Dockerfile
CHANGED
|
@@ -3,14 +3,27 @@
|
|
| 3 |
|
| 4 |
FROM python:3.9
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
RUN useradd -m -u 1000 user
|
| 7 |
USER user
|
| 8 |
ENV PATH="/home/user/.local/bin:$PATH"
|
| 9 |
|
| 10 |
WORKDIR /app
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
COPY --chown=user ./requirements.txt requirements.txt
|
| 13 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 14 |
|
| 15 |
COPY --chown=user . /app
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
FROM python:3.9
|
| 5 |
|
| 6 |
+
# 安装运行时依赖
|
| 7 |
+
RUN apt-get update && apt-get install -y \
|
| 8 |
+
nginx \
|
| 9 |
+
libgomp1 \
|
| 10 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 11 |
+
|
| 12 |
RUN useradd -m -u 1000 user
|
| 13 |
USER user
|
| 14 |
ENV PATH="/home/user/.local/bin:$PATH"
|
| 15 |
|
| 16 |
WORKDIR /app
|
| 17 |
|
| 18 |
+
# 复制 Nginx 配置
|
| 19 |
+
COPY nginx/nginx.conf /etc/nginx/nginx.conf
|
| 20 |
+
|
| 21 |
COPY --chown=user ./requirements.txt requirements.txt
|
| 22 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 23 |
|
| 24 |
COPY --chown=user . /app
|
| 25 |
+
# 暴露端口
|
| 26 |
+
EXPOSE 7860
|
| 27 |
+
# 启动服务
|
| 28 |
+
CMD ["/app/scripts/start.sh"]
|
| 29 |
+
#CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
nginx/nginx.conf
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
events {
|
| 2 |
+
worker_connections 1024;
|
| 3 |
+
}
|
| 4 |
+
|
| 5 |
+
http {
|
| 6 |
+
upstream django {
|
| 7 |
+
server localhost:8000;
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
server {
|
| 11 |
+
listen 7860;
|
| 12 |
+
server_name localhost;
|
| 13 |
+
|
| 14 |
+
# Django 静态文件
|
| 15 |
+
location /static/ {
|
| 16 |
+
alias /app/;
|
| 17 |
+
expires 30d;
|
| 18 |
+
add_header Cache-Control "public, immutable";
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
# Django 媒体文件
|
| 22 |
+
location /media/ {
|
| 23 |
+
alias /app/django_app/media/;
|
| 24 |
+
expires 30d;
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
# API 路由
|
| 28 |
+
location /api/ {
|
| 29 |
+
proxy_pass http://django;
|
| 30 |
+
proxy_set_header Host $host;
|
| 31 |
+
proxy_set_header X-Real-IP $remote_addr;
|
| 32 |
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
| 33 |
+
proxy_set_header X-Forwarded-Proto $scheme;
|
| 34 |
+
|
| 35 |
+
# WebSocket 支持
|
| 36 |
+
proxy_http_version 1.1;
|
| 37 |
+
proxy_set_header Upgrade $http_upgrade;
|
| 38 |
+
proxy_set_header Connection "upgrade";
|
| 39 |
+
|
| 40 |
+
# 超时设置
|
| 41 |
+
proxy_connect_timeout 60s;
|
| 42 |
+
proxy_send_timeout 60s;
|
| 43 |
+
proxy_read_timeout 60s;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
# 管理后台
|
| 47 |
+
location /admin/ {
|
| 48 |
+
proxy_pass http://django;
|
| 49 |
+
proxy_set_header Host $host;
|
| 50 |
+
proxy_set_header X-Real-IP $remote_addr;
|
| 51 |
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
| 52 |
+
proxy_set_header X-Forwarded-Proto $scheme;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
# 根路径
|
| 56 |
+
location / {
|
| 57 |
+
proxy_pass http://django;
|
| 58 |
+
proxy_set_header Host $host;
|
| 59 |
+
proxy_set_header X-Real-IP $remote_addr;
|
| 60 |
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
| 61 |
+
proxy_set_header X-Forwarded-Proto $scheme;
|
| 62 |
+
}
|
| 63 |
+
}
|
| 64 |
+
}
|
scripts/start.sh
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
# 设置环境变量
|
| 4 |
+
#export PYTHONPATH=/usr/local/lib/python3.9/site-packages:$PYTHONPATH
|
| 5 |
+
#export DJANGO_SETTINGS_MODULE=myproject.settings
|
| 6 |
+
|
| 7 |
+
# --workers 3
|
| 8 |
+
uvicorn app:app --reload --host 0.0.0.0 --port 8000 --log-level info
|
| 9 |
+
|
| 10 |
+
# 启动 Nginx
|
| 11 |
+
nginx -g "daemon off;"
|