You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django生产部署异常:SESSION_COOKIE_SECURE引发访问矛盾

Django生产环境配置修复方案

核心问题拆解

  • SESSION_COOKIE_SECURE=True时:Django要求Cookie仅通过HTTPS传输,但现有配置中CSRF_TRUSTED_ORIGINS、CORS_ALLOWED_ORIGINS仍使用http://协议,协议不匹配触发上游连接异常(对应Nginx日志的upstream prematurely closed connection错误),导致网站无法访问;但admin后台因请求路径的特殊性暂时兼容。
  • SESSION_COOKIE_SECURE=False时:Cookie可通过HTTP传输,但admin后台的安全校验(Secure属性要求)不通过,导致登录失败;前端登录路径依赖安全Cookie,因此同样无法访问。

分步修复配置

1. Django核心配置调整

生产环境必须统一使用HTTPS协议,更新以下配置项:

ALLOWED_HOSTS = ["localhost","102.219.85.91","founderslooking.com","app.founderslooking.com"]

# CORS配置切换为HTTPS,关闭全局允许(生产环境更安全)
CORS_ALLOWED_ORIGINS = [
    "https://localhost",
    "https://127.0.0.1",
    "https://app.founderslooking.com",
]
CORS_ORIGIN_ALLOW_ALL = False
CORS_ALLOW_CREDENTIALS = True

# Cookie安全配置优化
SESSION_COOKIE_SAMESITE = "Lax"  # 替代None,兼顾安全与兼容性
SESSION_COOKIE_DOMAIN = ".founderslooking.com"  # 前缀点支持子域名共享Cookie
SESSION_COOKIE_SECURE = True
SESSION_COOKIE_HTTPONLY = True  # 新增,防止JS读取Cookie

# CSRF配置同步更新为HTTPS
CSRF_TRUSTED_ORIGINS = [
    "https://localhost",
    "https://*.founderslooking.com",
]
CSRF_COOKIE_SECURE = True
CSRF_COOKIE_SAMESITE = "Lax"
CSRF_COOKIE_DOMAIN = ".founderslooking.com"

# 生产环境必备配置
DEBUG = False
SECRET_KEY = "your-strong-production-secret-key"  # 替换为实际强密钥
STATIC_URL = '/static/'
STATIC_ROOT = "/path/to/your/django/static_root"  # 执行collectstatic后的目录
MEDIA_URL = '/media/'
MEDIA_ROOT = "/path/to/your/django/media_root"

2. Nginx配置修复

确保Nginx正确转发HTTPS请求,并传递关键请求头给Django:

server {
    listen 443 ssl;
    server_name founderslooking.com app.founderslooking.com;

    # SSL证书路径替换为实际文件
    ssl_certificate /etc/nginx/ssl/founderslooking.crt;
    ssl_certificate_key /etc/nginx/ssl/founderslooking.key;

    location / {
        proxy_pass http://127.0.0.1:8000;  # 匹配Django服务的监听端口
        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;  # 关键:告知Django请求使用HTTPS
        proxy_set_header X-Forwarded-Host $server_name;
    }

    # 静态文件单独托管
    location /static/ {
        root /path/to/your/django/static_root;
        expires 30d;
    }

    # 媒体文件托管
    location /media/ {
        root /path/to/your/django/media_root;
        expires 30d;
    }
}

# 强制HTTP重定向到HTTPS
server {
    listen 80;
    server_name founderslooking.com app.founderslooking.com;
    return 301 https://$host$request_uri;
}

注意:X-Forwarded-Proto头必须配置,否则Django会误判请求为HTTP,导致SESSION_COOKIE_SECURE=True时Cookie无法正常生效。

3. 验证与收尾步骤

  1. 执行python manage.py collectstatic,将静态文件同步到STATIC_ROOT目录。
  2. 重启Nginx服务:sudo systemctl restart nginx。
  3. 重启Django服务(如Gunicorn/UWSGI):sudo systemctl restart gunicorn。
  4. 访问https://founderslooking.com/admin测试登录功能,同时检查网站前端页面及登录路径是否正常。
  5. 查看Nginx日志(/var/log/nginx/error.log)和Django日志,确认无upstream prematurely closed类错误。

内容的提问来源于stack exchange,提问作者crawlingdev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 20:22:03