# Nginx-Django Bağlantı Sorunu Çözümü (Coolify) ## 🔧 Yapılan Düzeltmeler ### 1. Docker Compose Güncellemeleri - ✅ Nginx için `ports: - "80:80"` eklendi (Coolify proxy için gerekli) - ✅ Network alias düzeltildi: `nginx` ve `nginx_proxy` - ✅ Doğru network yapılandırması: `coolify` (external) ### 2. Nginx Config Güncellemeleri - ✅ Upstream block eklendi: `django_backend` - ✅ İki farklı server tanımı: - Primary: `django_web_prod:8000` (network alias) - Backup: `web:8000` (servis adı) - ✅ Timeout ayarları eklendi - ✅ Connection timeout: 60s ## 🚀 Coolify'da Deploy ### Adım 1: Kod Değişikliklerini Push Edin ```bash git add nginx/default.conf docker-compose.c.yml git commit -m "Fix: Nginx-Django connection for Coolify" git push ``` ### Adım 2: Coolify'da Redeploy 1. Coolify dashboard → Projeniz 2. **Redeploy** butonuna tıklayın 3. Build loglarını izleyin ## 🔍 Deploy Sonrası Test Komutları ### 1. Container'ları Kontrol Edin ```bash # Tüm container'ları listele docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" # Nginx ve web container'larını bulun docker ps | grep -E "nginx|web" ``` ### 2. Network Bağlantısını Test Edin ```bash # Nginx container ID'sini bulun NGINX_ID=$(docker ps | grep nginx | awk '{print $1}') # Web container ID'sini bulun WEB_ID=$(docker ps | grep "web" | grep -v nginx | awk '{print $1}') # Nginx'ten Django'ya DNS çözümlemesi test et docker exec $NGINX_ID nslookup django_web_prod docker exec $NGINX_ID nslookup web # Eğer nslookup yoksa: docker exec $NGINX_ID getent hosts django_web_prod docker exec $NGINX_ID getent hosts web ``` ### 3. HTTP Bağlantısını Test Edin ```bash # Nginx container'dan Django'ya wget ile test docker exec $NGINX_ID wget -qO- http://django_web_prod:8000 || echo "HATA: django_web_prod çözülemiyor" docker exec $NGINX_ID wget -qO- http://web:8000 || echo "HATA: web çözülemiyor" # Curl kullanarak (eğer varsa) docker exec $NGINX_ID curl -I http://django_web_prod:8000 ``` ### 4. Nginx Config ve Loglarını Kontrol Edin ```bash # Nginx config test docker exec $NGINX_ID nginx -t # Nginx conf dosyasını görüntüle docker exec $NGINX_ID cat /etc/nginx/conf.d/default.conf # Nginx error logları docker exec $NGINX_ID cat /var/log/nginx/error.log # Nginx access logları docker exec $NGINX_ID cat /var/log/nginx/access.log # Real-time log izleme docker logs -f $NGINX_ID docker logs -f $WEB_ID ``` ### 5. Network İnceleme ```bash # Coolify network'ünü incele docker network inspect coolify # Hangi container'lar bu network'te? docker network inspect coolify | grep -A 5 "Containers" # Web container'ın IP adresini bul docker inspect $WEB_ID | grep -A 10 "Networks" | grep "IPAddress" # Nginx'ten web container IP'sine ping docker exec $NGINX_ID ping -c 3 ``` ## 🐛 Sorun Giderme ### Problem 1: "could not resolve host: django_web_prod" **Sebep**: Network alias çözülmüyor. **Çözüm**: ```bash # docker-compose.c.yml içinde web servisinin network alias'ını kontrol edin: cat docker-compose.c.yml | grep -A 5 "networks:" | grep -A 2 "aliases" # Beklenen çıktı: # aliases: # - django_web_prod # Eğer yoksa veya yanlışsa, compose dosyasını düzeltin ve redeploy edin ``` ### Problem 2: "Connection refused" **Sebep**: Django uygulaması çalışmıyor veya port 8000'de dinlemiyor. **Çözüm**: ```bash # Web container'ın loglarını kontrol edin docker logs $WEB_ID --tail 100 # Django'nun port 8000'de dinlediğini kontrol edin docker exec $WEB_ID netstat -tuln | grep 8000 # Veya docker exec $WEB_ID ss -tuln | grep 8000 # Web container içinden kendine bağlanmayı deneyin docker exec $WEB_ID curl -I http://localhost:8000 ``` ### Problem 3: "upstream timed out" **Sebep**: Django yanıt vermesi çok uzun sürüyor. **Çözüm**: ```bash # Django loglarını kontrol edin docker logs $WEB_ID -f # Gunicorn worker sayısını artırın (docker-compose.c.yml): # command: gunicorn core.wsgi:application --bind 0.0.0.0:8000 --workers 5 # Nginx timeout'ları artırın (default.conf zaten 60s): # proxy_connect_timeout 120s; # proxy_send_timeout 120s; # proxy_read_timeout 120s; ``` ### Problem 4: "502 Bad Gateway" **Sebep**: Nginx Django'ya bağlanamıyor. **Çözüm**: ```bash # Tüm container'ların aynı network'te olduğunu doğrulayın docker inspect $NGINX_ID | grep -A 10 "Networks" docker inspect $WEB_ID | grep -A 10 "Networks" # Her ikisi de "coolify" network'ünde olmalı # Network alias'larını kontrol edin docker network inspect coolify | jq '.[0].Containers' # Nginx upstream config'ini kontrol edin docker exec $NGINX_ID cat /etc/nginx/conf.d/default.conf | grep -A 3 "upstream" ``` ### Problem 5: Static/Media dosyaları 404 **Sebep**: Volume mount sorunları. **Çözüm**: ```bash # Nginx container içinde volume'ların mount edildiğini kontrol edin docker exec $NGINX_ID ls -la /app/staticfiles/ docker exec $NGINX_ID ls -la /app/media/ # Web container içinde static dosyaları kontrol edin docker exec $WEB_ID ls -la /app/staticfiles/ # Collectstatic çalıştırın docker exec $WEB_ID python manage.py collectstatic --noinput ``` ## 📊 Beklenen Çıktılar ### Başarılı DNS Çözümlemesi ```bash $ docker exec $NGINX_ID nslookup django_web_prod Server: 127.0.0.11 Address: 127.0.0.11#53 Non-authoritative answer: Name: django_web_prod Address: 172.18.0.3 # IP değişebilir ``` ### Başarılı HTTP Testi ```bash $ docker exec $NGINX_ID wget -qO- http://django_web_prod:8000 | head -n 5 ... ``` ### Başarılı Nginx Config Test ```bash $ docker exec $NGINX_ID nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful ``` ## 🎯 Upstream Stratejisi Nginx config'de şu upstream stratejisi kullanılıyor: ```nginx upstream django_backend { server django_web_prod:8000 max_fails=3 fail_timeout=30s; server web:8000 backup; } ``` **Nasıl Çalışır**: 1. İlk olarak `django_web_prod:8000` (network alias) denenir 2. Eğer 3 kez başarısız olursa, 30 saniye fail olarak işaretlenir 3. Backup olarak `web:8000` (servis adı) kullanılır 4. Bu sayede her iki DNS çözümleme yöntemi de desteklenir ## 📝 Kontrol Listesi Deploy öncesi: - [ ] `nginx/default.conf` dosyası upstream block içeriyor - [ ] `docker-compose.c.yml` içinde nginx ports tanımı var - [ ] `docker-compose.c.yml` içinde web servisi network alias'ı `django_web_prod` - [ ] Her iki servis de `coolify` network'üne bağlı - [ ] Git'e push edildi Deploy sonrası: - [ ] Container'lar ayakta: `docker ps` - [ ] DNS çözümlemesi çalışıyor: `nslookup django_web_prod` - [ ] HTTP bağlantısı çalışıyor: `wget http://django_web_prod:8000` - [ ] Nginx config geçerli: `nginx -t` - [ ] Nginx loglarında hata yok - [ ] Browser'dan site açılıyor ## 🔗 Hızlı Debug Script Aşağıdaki script'i Coolify sunucusunda çalıştırarak tüm testleri yapabilirsiniz: ```bash #!/bin/bash # nginx-django-debug.sh echo "=== CONTAINER STATUS ===" docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | grep -E "nginx|web|NAME" NGINX_ID=$(docker ps | grep nginx | awk '{print $1}') WEB_ID=$(docker ps | grep "web" | grep -v nginx | awk '{print $1}') echo -e "\n=== DNS RESOLUTION TEST ===" echo "Testing django_web_prod:" docker exec $NGINX_ID nslookup django_web_prod 2>&1 || echo "FAILED" echo "Testing web:" docker exec $NGINX_ID nslookup web 2>&1 || echo "FAILED" echo -e "\n=== HTTP CONNECTION TEST ===" echo "Testing http://django_web_prod:8000:" docker exec $NGINX_ID wget -qO- http://django_web_prod:8000 2>&1 | head -n 3 echo "Testing http://web:8000:" docker exec $NGINX_ID wget -qO- http://web:8000 2>&1 | head -n 3 echo -e "\n=== NGINX CONFIG TEST ===" docker exec $NGINX_ID nginx -t echo -e "\n=== NGINX ERROR LOG (last 10 lines) ===" docker exec $NGINX_ID tail -n 10 /var/log/nginx/error.log 2>&1 || echo "No errors" echo -e "\n=== WEB CONTAINER LOG (last 10 lines) ===" docker logs $WEB_ID --tail 10 echo -e "\n=== NETWORK INFO ===" docker network inspect coolify | grep -A 20 "Containers" echo -e "\n=== DONE ===" ``` Kullanım: ```bash chmod +x nginx-django-debug.sh ./nginx-django-debug.sh ``` ## ✅ Başarı Kriterleri Eğer aşağıdakiler çalışıyorsa, sorun çözülmüştür: 1. ✅ `docker ps` ile nginx ve web container'ları görünüyor 2. ✅ `docker exec nslookup django_web_prod` çalışıyor 3. ✅ `docker exec wget -qO- http://django_web_prod:8000` HTML dönüyor 4. ✅ `docker exec nginx -t` başarılı 5. ✅ Browser'dan Coolify domain'e girdiğinizde Django uygulaması açılıyor --- **Son Güncelleme**: 29 Ocak 2026 **Durum**: Nginx upstream stratejisi eklendi, Coolify uyumlu