Bladeren bron

增加docker配置文件

swjian 3 weken geleden
bovenliggende
commit
f46ff9aa9c
3 gewijzigde bestanden met toevoegingen van 105 en 0 verwijderingen
  1. 12 0
      Dockerfile
  2. 34 0
      docker-compose.yml
  3. 59 0
      nginx.conf

+ 12 - 0
Dockerfile

@@ -0,0 +1,12 @@
+FROM python:3.11-slim
+
+WORKDIR /app
+
+COPY requirements.txt .
+RUN pip install --no-cache-dir -r requirements.txt
+
+COPY . .
+
+EXPOSE 8100
+
+CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8100", "--workers", "8"]

+ 34 - 0
docker-compose.yml

@@ -0,0 +1,34 @@
+services:
+  math-questions:
+    build: .
+    # 不再直接暴露端口,通过 Nginx 访问
+    # ports:
+    #   - "8100:8100"
+    env_file:
+      - .env
+    restart: unless-stopped
+    networks:
+      - app-network
+
+  nginx:
+    image: nginx:alpine
+    ports:
+      - "80:80"
+      - "443:443"
+    volumes:
+      - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
+      - ./ssl:/etc/nginx/ssl:ro
+      - nginx-logs:/var/log/nginx
+    depends_on:
+      - math-questions
+    restart: unless-stopped
+    networks:
+      - app-network
+
+volumes:
+  nginx-logs:
+
+
+networks:
+  app-network:
+    driver: bridge

+ 59 - 0
nginx.conf

@@ -0,0 +1,59 @@
+server {
+    listen 80;
+    server_name math-questions.chunsunqiuzhu.com;
+    
+    # 重定向 HTTP 到 HTTPS
+    return 301 https://$server_name$request_uri;
+}
+
+server {
+    listen 443 ssl http2;
+    server_name math-questions.chunsunqiuzhu.com;
+
+    # SSL 证书配置
+    ssl_certificate /etc/nginx/ssl/cert.pem;
+    ssl_certificate_key /etc/nginx/ssl/key.pem;
+    
+    # SSL 优化配置
+    ssl_protocols TLSv1.2 TLSv1.3;
+    ssl_ciphers HIGH:!aNULL:!MD5;
+    ssl_prefer_server_ciphers on;
+    ssl_session_cache shared:SSL:10m;
+    ssl_session_timeout 10m;
+
+    # 安全头
+    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
+    add_header X-Frame-Options "SAMEORIGIN" always;
+    add_header X-Content-Type-Options "nosniff" always;
+    add_header X-XSS-Protection "1; mode=block" always;
+
+    # 日志
+    access_log /var/log/nginx/access.log;
+    error_log /var/log/nginx/error.log;
+
+    # 反向代理到应用
+    location / {
+        proxy_pass http://math-questions:8100;
+        proxy_set_header Host $host;
+        proxy_set_header X-Real-IP $remote_addr;
+        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+        proxy_set_header X-Forwarded-Proto $scheme;
+        
+        # WebSocket 支持(如果需要)
+        proxy_http_version 1.1;
+        proxy_set_header Upgrade $http_upgrade;
+        proxy_set_header Connection "upgrade";
+        
+        # 超时设置
+        proxy_connect_timeout 300s;
+        proxy_send_timeout 300s;
+        proxy_read_timeout 300s;
+    }
+
+    # 静态文件缓存(如果有)
+    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
+        proxy_pass http://math-questions:8100;
+        expires 30d;
+        add_header Cache-Control "public, immutable";
+    }
+}