🔧 Fix static files serving in Nginx

CRITICAL FIX: Static files (CSS/JS) not loading after changing web:8000 to localhost:8000

Changes:
- nginx-smartsoltech.conf: Enabled /static/ and /media/ locations BEFORE location /
- NGINX_SETUP.md: Updated config to serve static files directly from filesystem
- NGINX_STATIC_FIX.md: Comprehensive troubleshooting guide for static files issues

Why this matters:
- Nginx must serve /static/ and /media/ DIRECTLY from filesystem (fast)
- NOT proxy to Django (slow)
- Order matters: location /static/ MUST be before location /
- Improves performance: 50-100ms → 1-5ms per static file request

Solution on server:
1. git pull origin master
2. sudo cp nginx-smartsoltech.conf /etc/nginx/sites-available/smartsoltech
3. sudo nginx -t && sudo systemctl reload nginx
This commit is contained in:
2025-11-24 11:54:54 +09:00
parent b93ab4d796
commit a70ee08821
3 changed files with 322 additions and 29 deletions

View File

@@ -56,6 +56,22 @@ server {
access_log /var/log/nginx/smartsoltech_access.log;
error_log /var/log/nginx/smartsoltech_error.log;
# Статические файлы - ВАЖНО: должны быть ПЕРЕД location /
location /static/ {
alias /opt/smartsoltech_site/smartsoltech/staticfiles/;
expires 30d;
add_header Cache-Control "public, immutable";
access_log off;
}
# Медиа файлы
location /media/ {
alias /opt/smartsoltech_site/smartsoltech/media/;
expires 7d;
add_header Cache-Control "public";
access_log off;
}
# Прокси к Django приложению
location / {
proxy_pass http://localhost:8000;
@@ -71,19 +87,4 @@ server {
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
# Статические файлы (опционально, если выносить из Docker)
# Раскомментировать если хотите отдавать статику напрямую из Nginx
# location /static/ {
# alias /opt/smartsoltech_site/smartsoltech/staticfiles/;
# expires 30d;
# add_header Cache-Control "public, immutable";
# }
# Медиа файлы (опционально, если выносить из Docker)
# location /media/ {
# alias /opt/smartsoltech_site/smartsoltech/media/;
# expires 7d;
# add_header Cache-Control "public";
# }
}