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

标签 python django nginx weasyprint

我有一个简单的 Django 应用程序,它托管在 Nginx 后面。我正在使用 weasyprint 生成 PDF 报告。 weasyprint 需要一个 base_url 属性来访问 static 文件。

虽然下面的 django 代码在本地机器(在开发服务器下)上运行良好,但在 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 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/"

最佳答案

回答我自己的 - 但这是一种解决方法'。对于 so ip 192.168.33.10,以及它的基址 http://192.168.33.10/media/'base_urlweasyprint` 的参数有问题 - 即使手动输入基址也没有成功。

这仍然不起作用并返回 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。希望有人能为 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')])

关于python - Django 的基本 URL,在 Nginx 代理后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36163542/

相关文章:

python - 单元格注释和颜色可以使用 gspread 查询吗?

python - 将百分比列添加到数据框

python - 在 Django 中显示来自数据库的动态数据

python - 即使调试关闭并且没有设置提供服务的 Web 服务器,Django 也会提供静态文件

python - 在 Django 模型表单中覆盖 clean() 方法时返回 cleaned_data

Django:prefetch_related() 是否遵循反向关系查找?

python 2.7 : create dictionary from list of sets

nginx - 如何不在 NginX 负载均衡器上终止 ssl

ubuntu - 在 ubuntu 上安装特定的 nginx-extras 包

python - 使用gunicorn运行Django : Bad Request (400)