【问题标题】:Base URL for Django, Behind Nginx ProxyDjango 的基本 URL,在 Nginx 代理后面
【发布时间】:2016-07-09 21:07:52
【问题描述】:

我有一个简单的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 属性,上面的代码可以正常工作(不打印图像)。非常感谢您的意见 - 如何设置 nginx 或从 Django 检索 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;
    }

}

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/"

【问题讨论】:

    标签: python django nginx weasyprint


    【解决方案1】:

    回答我自己 - 但这是一种解决方法'。所以 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)
    

    所以我决定更改template。因此,无论我定义了一个 URL,我都将它们更改为...

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

    并在我的View.py 中添加context_instance 以获得MEDIA_URL。希望有人会为weasyprintbase_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,base_url=request.build_absolute_uri()).write_pdf(response, stylesheets=[
        weasyprint.CSS(settings.STATICFILES_DIRS[0] + '/css/print.css')])
    

    【讨论】:

      【解决方案2】:

      我在使用 Wea​​syPrint 时遇到了类似的问题,我发现 base_url 只考虑了 if URLs are not absolute

      如果上下文中存在真实的absolute_paths,我的解决方法是创建一个custom Django template tag 以将它们转换为文件系统绝对路径。同时I posted a question about my own problem scenario

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 2019-08-14
        • 1970-01-01
        • 1970-01-01
        • 2017-05-28
        • 2019-06-20
        • 1970-01-01
        • 2011-12-29
        • 2011-05-09
        相关资源
        最近更新 更多