Nginx返回502坏网关,但仅对少数HTTP请求。

huangapple go评论57阅读模式
英文:

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) -&gt; 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, &quot;project.html&quot;, {
        &#39;project&#39;: project,
        &#39;images&#39; : images,
    })

The template that receives it:

 &lt;div class=&quot;carousel&quot;&gt;
                            &lt;div class=&quot;carousel-cell&quot;&gt;
                                &lt;img src=&quot;data:image/jpeg;base64,{{ project.downscale }}&quot; data-src=&quot;/media/{{ project.thumbnail }}&quot;&gt;
                            &lt;/div&gt;
                            {% for projectimage in images %}
                                &lt;div class=&quot;carousel-cell&quot;&gt;
                                    &lt;img src=&quot;data:image/jpeg;base64,{{ projectimage.downscale }}&quot; alt=&quot;&quot; data-src=&quot;/media/{{ projectimage.image }}&quot;&gt;
                                &lt;/div&gt;
                            {% endfor %}
&lt;/div&gt;

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_projectimagegenerate_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)

huangapple
  • 本文由 发表于 2023年3月3日 21:54:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定