Flask项目打包部署全攻略从开发环境到生产环境的完整指南涵盖Docker容器化服务器配置和自动化部署流程助你轻松上线Flask应用

Flask项目打包部署全攻略从开发环境到生产环境的完整指南涵盖Docker容器化服务器配置和自动化部署流程助你轻松上线Flask应用
引言
Flask作为Python最受欢迎的轻量级Web框架之一,以其简洁、灵活和易于上手的特点赢得了大量开发者的青睐。然而,从开发一个Flask应用到将其成功部署到生产环境,往往是一个充满挑战的过程。许多开发者在这一过程中遇到了各种问题:环境不一致、依赖冲突、服务器配置复杂、部署流程繁琐等。
本文将为你提供一份详尽的Flask项目打包部署全攻略,涵盖从开发环境到生产环境的完整流程,包括Docker容器化、服务器配置和自动化部署流程。无论你是Flask新手还是有经验的开发者,这篇文章都能帮助你轻松上线Flask应用,解决部署过程中的各种难题。
开发环境准备
在开始部署之前,我们需要确保有一个稳定、一致的开发环境。良好的开发环境是成功部署的基础。
Python环境管理
首先,我们需要管理Python环境。推荐使用pyenv或conda来管理Python版本,以避免系统Python版本与应用需求不匹配的问题。
# 安装pyenv
curl https://pyenv.run | bash
echo ‘export PYENV_ROOT=”$HOME/.pyenv”‘ >> ~/.bashrc
echo ‘command -v pyenv >/dev/null || export PATH=”$PYENV_ROOT/bin:$PATH”‘ >> ~/.bashrc
echo ‘eval “$(pyenv init -)”‘ >> ~/.bashrc
exec “$SHELL”

# 安装特定版本的Python
pyenv install 3.9.7
pyenv global 3.9.7
虚拟环境创建
使用虚拟环境可以隔离项目依赖,避免不同项目之间的依赖冲突。
# 创建项目目录
mkdir flask_project
cd flask_project

# 创建虚拟环境
python -m venv venv
source venv/bin/activate # Linux/Mac
# 或
venv\Scripts\activate # Windows
Flask基础应用
创建一个简单的Flask应用作为示例:
# app.py
from flask import Flask, jsonify

app = Flask(__name__)

@app.route(‘/’)
def hello():
return jsonify({“message”: “Hello, Flask!”})

@app.route(‘/health’)
def health():
return jsonify({“status”: “healthy”})

if __name__ == ‘__main__’:
app.run(debug=True)
安装Flask:
pip install flask
运行应用:
python app.py
项目结构优化
为了便于部署,我们需要优化项目结构,使其更加模块化和可维护。
推荐的项目结构
flask_project/
├── app/
│ ├── __init__.py
│ ├── routes.py
│ ├── models.py
│ ├── templates/
│ │ └── index.html
│ └── static/
│ ├── css/
│ ├── js/
│ └── images/
├── tests/
│ ├── __init__.py
│ └── test_app.py
├── config.py
├── requirements.txt
├── .env
├── .flaskenv
├── Dockerfile
├── docker-compose.yml
└── run.py
实现项目结构
让我们按照上述结构重新组织我们的应用:
# app/__init__.py
from flask import Flask
from config import Config

def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)

from app.routes import bp
app.register_blueprint(bp)

return app
# app/routes.py
from flask import Blueprint, jsonify

bp = Blueprint(‘main’, __name__)

@bp.route(‘/’)
def hello():
return jsonify({“message”: “Hello, Flask!”})

@bp.route(‘/health’)
def health():
return jsonify({“status”: “healthy”})
# config.py
import os
from dotenv import load_dotenv

basedir = os.path.abspath(os.path.dirname(__file__))
load_dotenv(os.path.join(basedir, ‘.env’))

class Config:
SECRET_KEY = os.environ.get(‘SECRET_KEY’) or ‘dev-key-please-change’
DEBUG = os.environ.get(‘FLASK_DEBUG’, ‘False’).lower() == ‘true’
# run.py
from app import create_app

app = create_app()

if __name__ == ‘__main__’:
app.run()
依赖管理
良好的依赖管理是部署成功的关键。我们需要明确记录项目所需的所有依赖及其版本。
requirements.txt
创建requirements.txt文件:
pip freeze > requirements.txt
一个典型的requirements.txt文件可能如下:
Flask==2.0.1
Werkzeug==2.0.1
click==8.0.1
itsdangerous==2.0.1
Jinja2==3.0.1
MarkupSafe==2.0.1
gunicorn==20.1.0
python-dotenv==0.19.0
使用Pipenv进行依赖管理
Pipenv是一个更先进的依赖管理工具,它结合了pip和virtualenv的功能。
# 安装Pipenv
pip install pipenv

# 初始化项目
pipenv install

# 安装依赖
pipenv install flask
pipenv install gunicorn –dev # 开发依赖

# 生成requirements.txt
pipenv lock -r > requirements.txt
配置管理
在生产环境中,硬编码配置是不可取的。我们需要使用环境变量或配置文件来管理不同环境的配置。
环境变量配置
创建.env文件:
FLASK_APP=run.py
FLASK_ENV=development
FLASK_DEBUG=True
SECRET_KEY=your-secret-key-here
DATABASE_URL=sqlite:///app.db
创建.flaskenv文件(用于Flask命令行工具):
FLASK_APP=run.py
FLASK_ENV=development
不同环境的配置
扩展config.py以支持不同环境:
# config.py
import os
from dotenv import load_dotenv

basedir = os.path.abspath(os.path.dirname(__file__))
load_dotenv(os.path.join(basedir, ‘.env’))

class Config:
SECRET_KEY = os.environ.get(‘SECRET_KEY’) or ‘dev-key-please-change’
SQLALCHEMY_DATABASE_URI = os.environ.get(‘DATABASE_URL’) or \
‘sqlite:///’ + os.path.join(basedir, ‘app.db’)
SQLALCHEMY_TRACK_MODIFICATIONS = False

class DevelopmentConfig(Config):
DEBUG = True

class TestingConfig(Config):
TESTING = True
SQLALCHEMY_DATABASE_URI = ‘sqlite:///’ + os.path.join(basedir, ‘test.db’)

class ProductionConfig(Config):
DEBUG = False

config = {
‘development’: DevelopmentConfig,
‘testing’: TestingConfig,
‘production’: ProductionConfig,
‘default’: DevelopmentConfig
}
Flask应用生产环境配置
在生产环境中,我们不能使用Flask自带的开发服务器。我们需要使用一个生产级的WSGI服务器,如Gunicorn或uWSGI。
Gunicorn配置
Gunicorn是一个流行的Python WSGI HTTP服务器,适合运行Flask应用。
安装Gunicorn:
pip install gunicorn
创建Gunicorn配置文件gunicorn_config.py:
# gunicorn_config.py
bind = “0.0.0.0:8000”
workers = 4
worker_class = “sync”
worker_connections = 1000
max_requests = 1000
max_requests_jitter = 100
timeout = 30
keepalive = 2
使用Gunicorn运行Flask应用:
gunicorn -c gunicorn_config.py run:app
uWSGI配置
uWSGI是另一个流行的选择,特别适合高并发场景。
安装uWSGI:
pip install uwsgi
创建uWSGI配置文件uwsgi.ini:
[uwsgi]
module = run:app
master = true
processes = 4
socket = 0.0.0.0:8000
chmod-socket = 664
vacuum = true
die-on-term = true
使用uWSGI运行Flask应用:
uwsgi –ini uwsgi.ini
Docker容器化
Docker容器化可以确保应用在任何环境中都以相同的方式运行,大大简化了部署过程。
创建Dockerfile
创建Dockerfile:
# 使用官方Python运行时作为基础镜像
FROM python:3.9-slim

# 设置工作目录
WORKDIR /app

# 复制依赖文件
COPY requirements.txt .

# 安装依赖
RUN pip install –no-cache-dir -r requirements.txt

# 复制应用代码
COPY . .

# 设置环境变量
ENV FLASK_APP=run.py
ENV FLASK_ENV=production

# 暴露端口
EXPOSE 8000

# 运行应用
CMD [“gunicorn”, “-c”, “gunicorn_config.py”, “run:app”]
多阶段构建优化
为了减小镜像大小,我们可以使用多阶段构建:
# 第一阶段:构建阶段
FROM python:3.9-slim as builder

WORKDIR /app

COPY requirements.txt .

# 安装依赖
RUN pip install –user –no-cache-dir -r requirements.txt

# 第二阶段:运行阶段
FROM python:3.9-slim

WORKDIR /app

# 从构建阶段复制安装的依赖
COPY –from=builder /root/.local /home/app/.local

# 复制应用代码
COPY . .

# 确保PATH包含用户安装的包
ENV PATH=/home/app/.local/bin:$PATH

# 设置环境变量
ENV FLASK_APP=run.py
ENV FLASK_ENV=production

# 暴露端口
EXPOSE 8000

# 运行应用
CMD [“gunicorn”, “-c”, “gunicorn_config.py”, “run:app”]
使用docker-compose
Docker Compose可以简化多容器应用的管理。创建docker-compose.yml:
version: ‘3.8’

services:
web:
build: .
ports:
– “8000:8000”
environment:
– FLASK_ENV=production
– SECRET_KEY=your-secret-key-here
volumes:
– ./logs:/app/logs
restart: unless-stopped

nginx:
image: nginx:latest
ports:
– “80:80”
– “443:443”
volumes:
– ./nginx.conf:/etc/nginx/nginx.conf
– ./ssl:/etc/nginx/ssl
depends_on:
– web
restart: unless-stopped

db:
image: postgres:13
environment:
– POSTGRES_USER=flaskuser
– POSTGRES_PASSWORD=flaskpassword
– POSTGRES_DB=flaskdb
volumes:
– postgres_data:/var/lib/postgresql/data
restart: unless-stopped

volumes:
postgres_data:
使用.dockerignore
创建.dockerignore文件以排除不必要的文件:
.git
.gitignore
README.md
venv/
__pycache__/
*.pyc
.env
*.log
服务器配置
在部署Flask应用之前,我们需要配置服务器环境。这里我们以Ubuntu服务器为例。
系统更新和基础软件安装
# 更新系统
sudo apt update && sudo apt upgrade -y

# 安装基础工具
sudo apt install -y curl wget git htop vim

# 安装Docker
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
sudo usermod -aG docker $USER

# 安装Docker Compose
sudo curl -L “https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)” -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
防火墙配置
# 安装UFW防火墙
sudo apt install ufw -y

# 默认拒绝所有传入连接
sudo ufw default deny incoming

# 允许所有传出连接
sudo ufw default allow outgoing

# 允许SSH连接
sudo ufw allow ssh

# 允许HTTP和HTTPS连接
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

# 启用防火墙
sudo ufw enable
Nginx配置
Nginx可以作为反向代理,处理静态文件和SSL终止。
创建nginx.conf:
events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

# 日志格式
log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;

access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

# Gzip压缩
gzip on;
gzip_disable “msie6”;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

# 上游服务器
upstream flask_app {
server web:8000;
}

# HTTP服务器
server {
listen 80;
server_name your-domain.com www.your-domain.com;

# 重定向到HTTPS
return 301 https://$server_name$request_uri;
}

# HTTPS服务器
server {
listen 443 ssl http2;
server_name your-domain.com www.your-domain.com;

# SSL配置
ssl_certificate /etc/nginx/ssl/cert.pem;
ssl_certificate_key /etc/nginx/ssl/key.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;

# 安全头
add_header Strict-Transport-Security “max-age=31536000; includeSubDomains” always;
add_header X-Content-Type-Options “nosniff” always;
add_header X-Frame-Options “SAMEORIGIN” always;
add_header X-XSS-Protection “1; mode=block” always;

# 静态文件
location /static/ {
alias /app/static/;
expires 1y;
add_header Cache-Control “public, immutable”;
}

# 主应用
location / {
proxy_pass http://flask_app;
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;
proxy_redirect off;
}

# 健康检查
location /health {
access_log off;
proxy_pass http://flask_app/health;
}
}
}
SSL证书配置
使用Let’s Encrypt获取免费SSL证书:
# 安装Certbot
sudo apt install certbot python3-certbot-nginx -y

# 获取SSL证书
sudo certbot –nginx -d your-domain.com -d www.your-domain.com

# 设置自动续期
sudo crontab -e
# 添加以下行
0 12 * * * /usr/bin/certbot renew –quiet
自动化部署流程
自动化部署可以大大提高部署效率和可靠性。我们可以使用GitHub Actions、GitLab CI/CD或Jenkins等工具实现自动化部署。
GitHub Actions配置
在项目根目录创建.github/workflows/deploy.yml:
name: Deploy Flask App

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test:
runs-on: ubuntu-latest

steps:
– uses: actions/checkout@v2

– name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.9

– name: Install dependencies
run: |
python -m pip install –upgrade pip
pip install -r requirements.txt

– name: Run tests
run: |
python -m pytest tests/

deploy:
needs: test
runs-on: ubuntu-latest
if: github.ref == ‘refs/heads/main’

steps:
– uses: actions/checkout@v2

– name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

– name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

– name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: your-dockerhub-username/flask-app:latest

– name: Deploy to production server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_SSH_KEY }}
script: |
cd /path/to/your/app
docker-compose pull
docker-compose up -d
GitLab CI/CD配置
在项目根目录创建.gitlab-ci.yml:
stages:
– test
– build
– deploy

variables:
DOCKER_REGISTRY: your-registry.com
DOCKER_IMAGE: your-registry.com/flask-app

test:
stage: test
image: python:3.9
before_script:
– pip install -r requirements.txt
script:
– python -m pytest tests/

build:
stage: build
image: docker:latest
services:
– docker:dind
script:
– docker build -t $DOCKER_IMAGE:$CI_COMMIT_SHA .
– docker push $DOCKER_IMAGE:$CI_COMMIT_SHA
only:
– main

deploy:
stage: deploy
image: alpine:latest
before_script:
– ‘which ssh-agent || ( apk update && apk add openssh-client )’
– eval $(ssh-agent -s)
– echo “$SSH_PRIVATE_KEY” | tr -d ‘\r’ | ssh-add –
– mkdir -p ~/.ssh
– chmod 700 ~/.ssh
– ssh-keyscan -H $SERVER_IP >> ~/.ssh/known_hosts
– chmod 644 ~/.ssh/known_hosts
script:
– ssh $SERVER_USER@$SERVER_IP “cd /path/to/your/app && docker-compose pull && docker-compose up -d”
only:
– main
Jenkins配置
创建Jenkinsfile:
pipeline {
agent any

environment {
DOCKER_REGISTRY = ‘your-registry.com’
DOCKER_IMAGE = ‘your-registry.com/flask-app’
SERVER_IP = credentials(‘server-ip’)
SERVER_USER = credentials(‘server-user’)
SSH_KEY = credentials(‘ssh-key’)
}

stages {
stage(‘Checkout’) {
steps {
checkout scm
}
}

stage(‘Test’) {
steps {
sh ‘docker run –rm -v $(pwd):/app python:3.9 sh -c “cd /app && pip install -r requirements.txt && python -m pytest tests/”‘
}
}

stage(‘Build’) {
steps {
sh “docker build -t ${DOCKER_IMAGE}:${env.BUILD_ID} .”
sh “docker push ${DOCKER_IMAGE}:${env.BUILD_ID}”
}
}

stage(‘Deploy’) {
steps {
sh ”’
ssh -i ${SSH_KEY} ${SERVER_USER}@${SERVER_IP} \
“cd /path/to/your/app && \
sed -i ‘s|${DOCKER_IMAGE}:.*|${DOCKER_IMAGE}:${env.BUILD_ID}|’ docker-compose.yml && \
docker-compose pull && \
docker-compose up -d”
”’
}
}
}

post {
always {
cleanWs()
}
}
}
监控和日志
在生产环境中,监控和日志管理是确保应用稳定运行的关键。
应用监控
我们可以使用Prometheus和Grafana来监控Flask应用:
# 在app/__init__.py中添加监控支持
from prometheus_flask_exporter import PrometheusMetrics

def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)

metrics = PrometheusMetrics(app)

from app.routes import bp
app.register_blueprint(bp)

return app
更新requirements.txt:
prometheus-flask-exporter==0.18.0
创建docker-compose监控配置:
version: ‘3.8’

services:
web:
build: .
ports:
– “8000:8000”
environment:
– FLASK_ENV=production
– SECRET_KEY=your-secret-key-here
restart: unless-stopped

nginx:
image: nginx:latest
ports:
– “80:80”
– “443:443”
volumes:
– ./nginx.conf:/etc/nginx/nginx.conf
– ./ssl:/etc/nginx/ssl
depends_on:
– web
restart: unless-stopped

prometheus:
image: prom/prometheus:latest
ports:
– “9090:9090”
volumes:
– ./prometheus.yml:/etc/prometheus/prometheus.yml
command:
– ‘–config.file=/etc/prometheus/prometheus.yml’
restart: unless-stopped

grafana:
image: grafana/grafana:latest
ports:
– “3000:3000”
volumes:
– grafana_data:/var/lib/grafana
environment:
– GF_SECURITY_ADMIN_PASSWORD=admin
restart: unless-stopped

volumes:
grafana_data:
创建prometheus.yml:
global:
scrape_interval: 15s

scrape_configs:
– job_name: ‘flask-app’
static_configs:
– targets: [‘web:8000’]
日志管理
使用ELK(Elasticsearch, Logstash, Kibana)或EFK(Elasticsearch, Fluentd, Kibana)栈进行日志管理。
添加Flask日志配置:
# config.py
import os
import logging
from logging.handlers import RotatingFileHandler

class Config:
# … 其他配置 …

@staticmethod
def init_app(app):
# 确保日志目录存在
if not os.path.exists(‘logs’):
os.mkdir(‘logs’)

# 配置文件日志处理器
file_handler = RotatingFileHandler(‘logs/flask-app.log’, maxBytes=10240, backupCount=10)
file_handler.setFormatter(logging.Formatter(
‘%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]’))
file_handler.setLevel(logging.INFO)
app.logger.addHandler(file_handler)

app.logger.setLevel(logging.INFO)
app.logger.info(‘Flask App startup’)
更新docker-compose.yml以包含日志管理:
version: ‘3.8’

services:
web:
build: .
ports:
– “8000:8000”
environment:
– FLASK_ENV=production
– SECRET_KEY=your-secret-key-here
volumes:
– ./logs:/app/logs
logging:
driver: “fluentd”
options:
fluentd-address: localhost:24224
tag: flask.web
restart: unless-stopped

# … 其他服务 …

fluentd:
build: ./fluentd
volumes:
– ./fluentd/conf:/fluentd/etc
ports:
– “24224:24224”
– “24224:24224/udp”
restart: unless-stopped

elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.15.0
environment:
– discovery.type=single-node
volumes:
– elasticsearch_data:/usr/share/elasticsearch/data
ports:
– “9200:9200”
restart: unless-stopped

kibana:
image: docker.elastic.co/kibana/kibana:7.15.0
ports:
– “5601:5601”
depends_on:
– elasticsearch
restart: unless-stopped

volumes:
elasticsearch_data:
性能优化
在生产环境中,性能优化是确保应用能够高效处理请求的关键。
Flask应用优化
使用缓存
# app/__init__.py
from flask_caching import Cache

def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)

# 配置缓存
cache_config = {
‘CACHE_TYPE’: ‘redis’,
‘CACHE_REDIS_URL’: os.environ.get(‘REDIS_URL’, ‘redis://localhost:6379/0’)
}
app.config.from_mapping(cache_config)
cache = Cache(app)

from app.routes import bp
app.register_blueprint(bp)

return app
使用连接池
# config.py
from flask_sqlalchemy import SQLAlchemy

class Config:
# … 其他配置 …

SQLALCHEMY_ENGINE_OPTIONS = {
‘pool_size’: 10,
‘pool_recycle’: 120,
‘pool_pre_ping’: True
}
异步任务处理
# app/tasks.py
from celery import Celery

def make_celery(app):
celery = Celery(
app.import_name,
backend=app.config[‘CELERY_RESULT_BACKEND’],
broker=app.config[‘CELERY_BROKER_URL’]
)
celery.conf.update(app.config)

class ContextTask(celery.Task):
def __call__(self, *args, **kwargs):
with app.app_context():
return self.run(*args, **kwargs)

celery.Task = ContextTask
return celery

# app/__init__.py
from app.tasks import make_celery

def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)

# 配置Celery
app.config[‘CELERY_BROKER_URL’] = os.environ.get(‘CELERY_BROKER_URL’, ‘redis://localhost:6379/0’)
app.config[‘CELERY_RESULT_BACKEND’] = os.environ.get(‘CELERY_RESULT_BACKEND’, ‘redis://localhost:6379/0’)
celery = make_celery(app)

from app.routes import bp
app.register_blueprint(bp)

return app, celery
服务器优化
调整Gunicorn配置
# gunicorn_config.py
import multiprocessing

bind = “0.0.0.0:8000”
workers = multiprocessing.cpu_count() * 2 + 1
worker_class = “gevent” # 使用gevent worker处理并发
worker_connections = 1000
max_requests = 1000
max_requests_jitter = 100
timeout = 30
keepalive = 2
使用Nginx缓存
# 在nginx.conf中添加缓存配置
http {
# … 其他配置 …

# 缓存路径配置
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=flask_cache:10m inactive=60m use_temp_path=off;

server {
# … 其他配置 …

location / {
proxy_pass http://flask_app;
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;
proxy_redirect off;

# 缓存配置
proxy_cache flask_cache;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_lock on;
}
}
}
数据库优化
# config.py
class Config:
# … 其他配置 …

# 数据库连接池配置
SQLALCHEMY_ENGINE_OPTIONS = {
‘pool_size’: 20,
‘max_overflow’: 10,
‘pool_recycle’: 3600,
‘pool_pre_ping’: True
}

# 查询缓存
CACHE_TYPE = ‘redis’
CACHE_REDIS_URL = os.environ.get(‘REDIS_URL’, ‘redis://localhost:6379/0’)
CACHE_DEFAULT_TIMEOUT = 300
安全考虑
在生产环境中,安全性是至关重要的。我们需要采取多种措施来保护我们的Flask应用。
Flask安全配置
使用HTTPS
# config.py
class ProductionConfig(Config):
# … 其他配置 …

SESSION_COOKIE_SECURE = True
SESSION_COOKIE_HTTPONLY = True
SESSION_COOKIE_SAMESITE = ‘Lax’
PERMANENT_SESSION_LIFETIME = 3600 # 1小时
CSRF保护
# app/__init__.py
from flask_wtf.csrf import CSRFProtect

def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)

# CSRF保护
csrf = CSRFProtect(app)

from app.routes import bp
app.register_blueprint(bp)

return app
安全头
# app/__init__.py
from flask_talisman import Talisman

def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)

# 安全头
csp = {
‘default-src’: [
‘\’self\”,
‘*.example.com’
],
‘style-src’: [
‘\’self\”,
‘\’unsafe-inline\”
],
‘script-src’: [
‘\’self\”
]
}
Talisman(app, content_security_policy=csp, force_https=True)

from app.routes import bp
app.register_blueprint(bp)

return app
服务器安全配置
防火墙规则
# 限制SSH访问
sudo ufw limit ssh

# 只允许特定IP访问管理端口
sudo ufw allow from 192.168.1.0/24 to any port 8000
Fail2Ban配置
# 安装Fail2Ban
sudo apt install fail2ban -y

# 创建Nginx过滤器
sudo tee /etc/fail2ban/filter.d/nginx-auth.conf > /dev/null <, server: \S*, request: “\S+ \S+ HTTP/\d+\.\d+”, host: “\S+”(, referrer: “\S+”)?\s*$
^ \[error\] \d+#\d+: \*\d+ no user/password was provided for basic authentication, client: , server: \S+, request: “\S+ \S+ HTTP/\d+\.\d+”, host: “\S+”\s*$
ignoreregex =
EOL

# 创建Jail配置
sudo tee /etc/fail2ban/jail.d/custom.conf > /dev/null < /dev/null <