【问题标题】:nginx Returning 500 for POST Image Request with Django Rest Frameworknginx 使用 Django Rest 框架为 POST 图像请求返回 500
【发布时间】:2019-12-11 21:23:12
【问题描述】:

前言

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

框架

Django 2.1.7
nginx/1.14.2
djangorestframework==3.9.2

问题

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

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

我尝试过的

正如在其他帖子中发现的那样,我已经将nginx.confclient_max_body_size 增加到了非常大的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;
        }
    }
}

大问题

  1. 为什么 nginx 会返回 500 错误?
  2. 有没有办法从 nginx 获取更详细的错误消息,以了解为什么返回 500 错误?
  3. 我的配置/设置设置是否符合我预期的图片上传方式,或者是否有改进或缺失的部分可以解决此问题?

任何想法或反馈将不胜感激。谢谢。

【问题讨论】:

  • 你试过查看 nginx 日志文件吗?
  • 感谢您的评论。是的,我已经检查了访问日志和错误日志,我得到的只是访问日志中的一行:"POST [path]/upload-image/ HTTP/1.1" 500 186 "-" "okhttp/3.12.1"([path] 是 URL 的真实相对路径的替代品)

标签: django nginx file-upload django-rest-framework image-uploading


【解决方案1】:

经过大量研究和反复试验,我找到了适合我情况的解决方案。

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 以检查其格式是否正确)。

绝对希望这能帮助人们,因为这对我来说是一种巨大的解脱!

【讨论】:

    猜你喜欢
    • 2018-12-20
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 2023-03-16
    • 2021-05-06
    • 2020-10-05
    • 1970-01-01
    相关资源
    最近更新 更多