nginx 使用 Django Rest Framework 进行 POST 图像请求返回 500

问题描述 投票:0回答:2

前言

我已经做了很多研究来解决我遇到的类似问题,但仍然没有解决我遇到的这个主要问题。作为最后的手段,由于很多情况似乎都是特定于应用程序的,我决定在这里用我的配置创建一个帖子,希望有人能看到可能的解决方案。

框架

Django 2.1.7
nginx/1.14.2
djangorestframework==3.9.2

问题

在使用上述框架的服务器本地设置中,我可以通过我的客户端应用程序(特别是反应本机应用程序,这可能不是重要信息)发布图像。 但是,当尝试使用相同的图像和 URL 路径向我的远程服务器发出相同的 POST 请求时,它会返回以下 500 错误(从 Google Chrome 控制台调试器截取的快照)。

我强烈推断 nginx 是图像上传的罪魁祸首,因为我的远程服务器的 django 日志没有显示它甚至像我的本地服务器那样收到了 POST 请求。 但是,不上传图像的普通 POST 请求在远程和本地服务器上都可以正常工作。 我尝试过的事情

在其他帖子中发现,我把

client_max_body_size

nginx.conf
增加到了2000M这个非常大的数字。我还设置了以下 django 设置:
FILE_UPLOAD_PERMISSIONS = 0o777

FILE_UPLOAD_MAX_MEMORY_SIZE = 2621440000

DATA_UPLOAD_MAX_MEMORY_SIZE = 2621440000

这些提议的解决方案均无效。

我的配置

nginx.conf

user                        www;
worker_processes            auto;

events { worker_connections 1024; }
daemon off;

http {
    server_tokens off;
    sendfile      on;
    include       mime.types;

    default_type  application/octet-stream;

    gzip              on;
    gzip_http_version 1.0;
    gzip_proxied      any;
    gzip_min_length   500;
    gzip_disable      "MSIE [1-6]\.";
    gzip_types        text/plain text/xml text/css
                    text/comma-separated-values
                    text/javascript
                    application/x-javascript
                    application/atom+xml;

    # List of application servers
    upstream app_server {
        server 127.0.0.1:9090;
    }

    # PRODUCTION (also default)
    server {
        # Using an alias, not the actual server name
        server_name my-remote-server.com;

        # Running port
        listen 8080 default_server;
        client_max_body_size 100M;
        keepalive_timeout    15;

        # Principle difference between production
        if ($http_x_forwarded_proto = "http") {
            return 301 https://$host$request_uri;
        }

        # Don't serve .py files
        location ~ (\.py$|\.pyc$) {
            return 403;
        }

        # Static assets served directly
        location /static/ {
            alias           /var/app/myserver/src/site/static/;
            access_log      off;
            log_not_found   off;
        }

        location /media/ {
            alias           /var/app/myserver/src/myserver/media/;
            access_log      off;
            log_not_found   off;
        }

        # Proxying the connections
        location / {
            proxy_pass              http://app_server;
            proxy_redirect          off;
            proxy_set_header        Host $host;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        X-Forwarded-Host $server_name;
        }
    }
}

大问题

为什么 nginx 可能会返回 500 错误?
  1. 有没有办法从 nginx 获取更详细的错误消息以了解为什么返回 500 错误?
  2. 我的配置/设置设置是否符合我预期上传图像的方式,或者是否有可以解决此问题的改进或缺失部分?
  3. 如有任何想法或反馈,我们将不胜感激。谢谢你。

django nginx file-upload django-rest-framework image-uploading
2个回答
2
投票

nginx.conf

对象的

http
中,设置以下属性:
// nginx.conf

...
http { 
    ...
    client_max_body_size 50M; // this can be whatever max size you want
    client_body_temp_path /tmp/client_body_temp;
    ...
}
...

根据 nginx 文档,
client_body_temp_path defines a directory for storing temporary files holding client request bodies

(参考:

http://nginx.org/en/docs/http/ngx_http_core_module.html#client_body_temp_path
)。一旦请求变大(即通过图像上传),nginx 服务器在处理整个请求时需要一个地方来缓冲。显然我的服务器没有默认临时路径client_body_temp的权限,所以我手动创建了一个路径为
/tmp/client_body_temp
且权限为chmod 744
的文件夹,服务器最终响应了HTTP 200。
/tmp
有一段时间后自动清理的额外好处)。
注意:更改 
nginx.conf

文件后,您需要运行

nginx -s reload

 以使用新配置刷新服务器(事先使用 
nginx -t
 检查其格式是否正确)。
绝对希望这对人们有帮助,就像这对我来说是一个巨大的解脱一样!

对于上述答案不起作用,请尝试增加

0
投票

client_body_buffer_size=2M

解释

client_body_buffer_size指令的默认值取决于Nginx的架构和版本。在 32 位平台上,默认大小为 256 字节。在 64 位平台上,默认大小为 512 字节。但是,Nginx 仅按需分配缓冲区,实际缓冲区大小可能会更大,具体取决于请求正文的大小。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.