多站点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/djangoto 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 theSTATIC_ROOTdirectory 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_NAMEheader: This tells Gunicorn and Django exactly where the application is rooted, so all internal URL generation includes the/djangoprefix.- Removed rewrite rule: Keeping the full path intact lets Django properly parse requests while using
SCRIPT_NAMEto isolate the application's route. - Trailing slash in
alias: Ensures Nginx maps requests like/django/static/css/style.cssto the correct file at/home/pi/elops-tracker-project/static/css/style.css(instead of a broken path).
3. Final Checks
- Restart Nginx to apply changes:
sudo systemctl restart nginx - Restart your Gunicorn service (replace with your actual service name):
sudo systemctl restart elops-tracker-gunicorn - 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

