我有一个简单的Django
应用程序,它托管在Nginx
. 我weasyprint
用来生成 PDF 报告。weasyprint
需要一个base_url
属性来访问static
文件。
虽然下面的django
代码在本地机器上运行良好(在开发服务器下),但在 Nginx 后面发布它时出现502 Bad Gateway错误。
视图.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')])
如果我删除该base_url
属性,上面的代码可以正常工作(不打印图像)。非常感谢您的输入 - 如何设置或从 Djangonginx
检索base_url
Nginx 配置
# 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;
}
}
打印.css
@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;;
}
}
nginx
这是来自日志的错误消息。
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/"