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

多站点Nginx子文件夹部署Django:Admin页面访问异常

Let's break down your problem and fix it step by step—you're right that the issues stem from Django not knowing it's running under a subpath, plus some misconfigurations in Nginx. Here's how to resolve both the redirect loop and missing static assets:

1. Fix Django's Subpath Awareness

Django doesn't automatically detect it's being served under /django, so it generates URLs and redirects without this prefix. Update your project's settings.py:

  • Add FORCE_SCRIPT_NAME = '/django' — this tells Django to prepend /django to all internal URLs and redirects
  • Update STATIC_URL = '/django/static/' — ensures static asset links include the subpath
  • Confirm STATIC_ROOT = '/home/pi/elops-tracker-project/static' — matches the directory you're pointing to in Nginx
  • Run python manage.py collectstatic — this copies all Django admin (and your app's) static files into the STATIC_ROOT directory so Nginx can serve them

2. Adjust Nginx Configuration

Your current rewrite rule strips the /django prefix before sending requests to Gunicorn, which breaks Django's ability to recognize the subpath. Replace your existing Django-related Nginx blocks with this:

# Serve Django static files
location /django/static/ {
    alias /home/pi/elops-tracker-project/static/;  # Trailing slash fixes path mapping
    expires 30d;  # Optional: cache static files for better performance
    add_header Cache-Control "public, immutable";
}

# Proxy requests to Gunicorn for Django
location /django/ {
    include proxy_params;
    # Tell Django it's running under /django
    proxy_set_header SCRIPT_NAME /django;
    # Ensure Django knows it's behind HTTPS (prevents insecure redirects)
    proxy_set_header X-Forwarded-Proto https;
    # Pass the request to the Gunicorn socket
    proxy_pass http://unix:/home/pi/elops-tracker-project/elops_tracker.sock;
}

# Handle /django endpoint without trailing slash (user-friendly fix)
location = /django {
    return 301 /django/;
}

Key Changes Explained:

  • SCRIPT_NAME header: This tells Gunicorn and Django exactly where the application is rooted, so all internal URL generation includes the /django prefix.
  • Removed rewrite rule: Keeping the full path intact lets Django properly parse requests while using SCRIPT_NAME to isolate the application's route.
  • Trailing slash in alias: Ensures Nginx maps requests like /django/static/css/style.css to the correct file at /home/pi/elops-tracker-project/static/css/style.css (instead of a broken path).

3. Final Checks

  1. Restart Nginx to apply changes:
    sudo systemctl restart nginx
    
  2. Restart your Gunicorn service (replace with your actual service name):
    sudo systemctl restart elops-tracker-gunicorn
    
  3. Verify file permissions: Make sure Nginx's user (www-data) can read the static directory:
    sudo chown -R pi:www-data /home/pi/elops-tracker-project/static
    sudo chmod -R 750 /home/pi/elops-tracker-project/static
    

After these steps, visiting https://example.com/django/admin should redirect correctly to https://example.com/django/admin/login/?next=/django/admin/, and the login page will load with proper styling.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:14:42