python – Django的基本URL,在Nginx代理后面

python – Django的基本URL,在Nginx代理后面,第1张

概述我有一个简单的Django应用程序,它在Nginx后面托管.我正在使用weasyprint来生成PDF报告. weasyprint需要base_url属性来访问静态文件. 虽然下面的django代码在本地机器上工作正常(在dev服务器下),但在Nginx后面发布时会出现502 Bad Gateway错误. View.py html = render_to_string('admin/enquiry 我有一个简单的Django应用程序,它在Nginx后面托管.我正在使用weasyprint来生成pdf报告. weasyprint需要base_url属性来访问静态文件.

虽然下面的django代码在本地机器上工作正常(在dev服务器下),但在Nginx后面发布时会出现502 Bad Gateway错误.

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')])

如果我删除base_url属性,上面的代码工作正常(没有打印图像).非常感谢您的输入 – 如何设置Nginx或从Django中恢复base_url

Nginx配置

# configuration of the serverserver {    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;    }}

print.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/"
解决方法 回答我自己 – 但这是一种解决方法’.对于所以ip 192.168.33.10,并且它的基地址http://192.168.33.10/media/’base_urlparameter forweasyprint`仍然有问题 – 即使手动输入基地址也没有办法解决问题.

这仍然无效,并返回502 Bad Gateway

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

所以我决定改变模板.所以无论我在哪里定义URL,我都将它们改为……

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

并在我的VIEw.py中添加了context_instance来获取MEDIA_URL.希望有人会为weasyprint的base_url问题找到答案.

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,stylesheets=[    weasyprint.CSS(settings.STATICfileS_Dirs[0] + '/CSS/print.CSS')])
总结

以上是内存溢出为你收集整理的python – Django的基本URL,在Nginx代理后面全部内容,希望文章能够帮你解决python – Django的基本URL,在Nginx代理后面所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1197568.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存