Nginx使用Django Rest Framework返回500用于POST映像请求

ebdffaop  于 5个月前  发布在  Nginx
关注(0)|答案(2)|浏览(62)

前言

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

框架

Django 2.1.7
nginx/1.14.2
djangorestframework==3.9.2

字符串
问题
在我使用上述框架的服务器本地设置上,我可以通过我的客户端应用程序(特别是react-native应用程序,这可能不是重要信息)发布图像。然而,当尝试使用相同的图像和URL路径向我的 * 远程 * 服务器发出相同的POST请求时,它返回以下500错误(从Google Chrome Console调试器快照)。
x1c 0d1x的数据
我强烈推断nginx是图片上传的罪魁祸首,因为我的远程服务器的django日志显示它甚至没有收到POST请求的迹象,就像我的本地服务器一样。

What I Have Tried

正如在其他帖子中发现的那样,我已经将nginx.confclient_max_body_size增加到了2000 M的非常大的数量。我还设置了以下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错误?
1.有没有一种方法可以从nginx中获取更详细的错误消息,以了解为什么它会返回500错误?
1.我的配置/设置是否以一种我应该期望上传图像的方式设置,或者是否有改进或缺失的部分可以解决这个问题?
任何想法或反馈都将非常感谢。谢谢。

2w3kk1z5

2w3kk1z51#

经过大量的研究和尝试,通过试验/错误,我找到了一个解决方案,为我的情况。
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检查它是否格式正确)。
当然希望这对人们有帮助,因为这对我来说是一个巨大的解脱!

6yjfywim

6yjfywim2#

对于以上答案不起作用的人,请尝试增加client_body_buffer_size
client_body_buffer_size=2M

说明

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

相关问题