英文:
Nginx gives 502 bad gateway but only for a few http requests
问题
我使用Django制作了一个投资组合网站,并将其托管在DigitalOcean droplet上,使用了Postgres、Nginx和Gunicorn。在测试后,我注意到投资组合项目的一些子页面在加载1秒后返回502响应,但有趣的是不是所有子页面都有此问题。那些失败的页面通常包含更多的图像,但我尝试在views.py发送到模板的图像中排除了图像,然而仍然出现502响应的问题。
(供参考,这些图像由本地服务器提供,而不是通过在线存储服务提供)
当我手动重新加载这些页面时,问题似乎已解决。
以下是views.py:
def project_view(request:HttpRequest, name:str) -> HttpResponse:
project = Project.objects.filter(name=name).first()
images = ProjectImages.objects.filter(project=project)
# 对于图像中的每个图像,将其缩小并使用PIL进行压缩
for projectimage in images:
generate_projectimage(projectimage)
# 缩小项目的缩略图图像
generate_downscaled_thumbnail(project)
return render(request, "project.html", {
'project': project,
'images' : images,
})
接收此内容的模板:
<div class="carousel">
<div class="carousel-cell">
<img src="data:image/jpeg;base64,{{ project.downscale }}" data-src="/media/{{ project.thumbnail }}">
</div>
{% for projectimage in images %}
<div class="carousel-cell">
<img src="data:image/jpeg;base64,{{ projectimage.downscale }}" alt="" data-src="/media/{{ projectimage.image }}">
</div>
{% endfor %}
</div>
这是Nginx错误日志:
Nginx upstream prematurely closed connection while reading response header from upstream
这个问题是否与Nginx有关,或者与发送的数据量过大或受设置限制有关?
[End of Translation]
英文:
I made a portfolio website using django and hosted it on a digitalocean droplet with Postgres, Nginx, and Gunicorn. After testing it I noticed that a few of the subpages of the portfolio projects gives back the 502 response after 1 second of loading but interestingly enough it's not all of them. The pages that fail tend to have more images but I tried excluding the images the views.py sends to the template, however the ones with the 502 response failed out again.
(for context the images are provided by local server side and not by an online storage service)
The issue seems to be resolved when I reload those pages manually.
Here is the views.py:
def project_view(request:HttpRequest, name:str) -> HttpResponse:
project = Project.objects.filter(name=name).first()
images = ProjectImages.objects.filter(project=project)
# for every image in images this downscales that image and compresses it with PIL
for projectimage in images:
generate_projectimage(projectimage)
# this downscales the thumbnail image of the project
generate_downscaled_thumbnail(project)
return render(request, "project.html", {
'project': project,
'images' : images,
})
The template that receives it:
<div class="carousel">
<div class="carousel-cell">
<img src="data:image/jpeg;base64,{{ project.downscale }}" data-src="/media/{{ project.thumbnail }}">
</div>
{% for projectimage in images %}
<div class="carousel-cell">
<img src="data:image/jpeg;base64,{{ projectimage.downscale }}" alt="" data-src="/media/{{ projectimage.image }}">
</div>
{% endfor %}
</div>
here is the nginx error log:
Nginx upstream prematurely closed connection while reading response header from upstream
Is it something to do with nginx or the data that is being sent is too much or limited by a setting?
答案1
得分: 1
检查成功请求的运行时间,我猜generate_projectimage
和generate_downscaled_thumbnail
可能运行时间较长,它们应该移至后台任务(例如使用celery)。
英文:
Check runtime of successful requests, I guess generate_projectimage
and generate_downscaled_thumbnail
can be long running and they should be moved to background tasks (e.g. use celery)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论