2016-03-22 24 views
4

Nginx'un arkasında bulunan basit bir Django Uygulaması var. PDF raporları üretmek için weasyprint kullanıyorum. weasyprint, static dosyalarına erişmek için base_url özelliği gerektirir. Django için Temel URL, Behind Nginx Proxy

altına django kod yerel makinede (dev sunucuya altında) çalışıyor iken

, ben Nginx arkasında yayımlamak 502 Bozuk Ağ Geçidi hatası alıyorum.

View.py

html = render_to_string('admin/enquiry/quoterequest/generate.html', {'enquiry': enquiry}) 

response = HttpResponse(content_type='application/pdf') 

response['Content-Disposition'] = 'filename=\"Enquiry_{}.pdf'.format(enquiry.reference) 
weasyprint.HTML(string=html,base_url=request.build_absolute_uri()).write_pdf(response, stylesheets=[ 
    weasyprint.CSS(settings.STATICFILES_DIRS[0] + '/css/print.css')]) 

yukarıdaki kodu (baskı resimlerin olmadan) çalışıyor, ben base_url özelliğini kaldırırsanız. girişinizi seviniriz - Ya nasıl kurulum nginx veya bu nginx kaydından hata mesajıdır

@page { 
    size: letter; 
    margin: 1.5in .25in 1.9in .5in; 

    @top-center { 
    content: url("/uploads/header_footer/header.jpg"); 
    height: 100%; 
    width: 100%; 
    } 

    @bottom-center { 
    background-image: url("/uploads/header_footer/footer.jpg"); 
    background-repeat: no-repeat; 
    background-position: center; 
    content: "Page " counter(page); 
    height: 100%; 
    width: 100%; 
    vertical-align: bottom;; 
    } 
} 

print.css

Nginx Yapılandırma

# configuration of the server 
server { 
    listen  80; 
    server_name 192.168.33.10; # Vagrant IP 
    root  /home/www/my_project; 
    charset  utf-8; 

    client_max_body_size 75M; # max upload size 

    location /media { 
     alias /home/www/my_project/assets/uploads; 
    } 

    location /static { 
     alias /home/www/my_project/assets/static; 
    } 

    location/{ 
     proxy_pass http://localhost:8001; 
     proxy_set_header Host $host; 
     proxy_set_header X-Real-IP $remote_addr; 
    } 

} 

Django

den base_url alınamadı.

upstream prematurely closed connection while reading response header from upstream, client: 192.168.33.1, server: 192.168.33.10, request: "GET /enquiry/admin/enquiry/quoterequest/view/1/ HTTP/1.1", upstream: "http://127.0.0.1:8001/enquiry/admin/enquiry/quoterequest/view/1/", host: "192.168.33.10", referrer: "http://192.168.33.10/admin/enquiry/quoterequest/" 

cevap

1

Kendi yanıtımı yanıtlama - ama bu bir çeşit geçici çözümdür. Bu yüzden ip 192.168.33.10 için ve temel adresi http://192.168.33.10/media/' taban adresinde parameter for weasyprint`de hala sorunlar var - hatta ana adresi hile yapmadı bile el ile girdi.

Bu hala çalışmak ve 502 Hatalı Ağ Geçidi

weasyprint.HTML(string=html, base_url='http://192.168.33.10/media/').write_pdf(response) 

ile dönmek yüzden template değiştirmeye karar verdi etmez. Ben tanımlanmış bir URL ettik her yerde Yani, ben ...

<img src="http://{{ request.META.HTTP_HOST }}{{ MEDIA_URL }}{{ myapp.mymodel.my_image }}"> 

onları değiştirdi ve MEDIA_URL olsun benim View.py yılında context_instance ekledi. Umarım birisi weasyprint 's base_url sorunu için bir cevap ile gelecek.

html = render_to_string('admin/enquiry/quoterequest/generate.html', {'enquiry': enquiry}, context_instance=RequestContext(request)) 

response = HttpResponse(content_type='application/pdf') 

response['Content-Disposition'] = 'filename=\"Enquiry_{}.pdf'.format(enquiry.reference) 

weasyprint.HTML(string=html,base_url=request.build_absolute_uri()).write_pdf(response, stylesheets=[ 
    weasyprint.CSS(settings.STATICFILES_DIRS[0] + '/css/print.css')])