nginx使用Django Rest框架返回500以请求后期映像 前言
我已经做了很多研究来解决我遇到的类似问题,但仍然没有解决我遇到的这个主要问题。作为最后的手段,由于许多案例似乎都是特定于应用程序的,因此我决定在这里创建一个带有我的配置的帖子,希望有人能看到一个可能的解决方案 框架 问题 在使用上述框架的服务器的本地设置中,我可以通过我的客户端应用程序(特别是react本机应用程序,它可能不是重要信息)发布图像但是,当尝试使用相同的图像和URL路径向我的远程服务器发出相同的POST请求时,它返回以下500个错误(从Google Chrome控制台调试器拍摄) 我强烈地推断,nginx是仅仅上传图像的罪魁祸首,因为我的远程服务器的django日志没有显示它甚至收到POST请求的迹象,就像我的本地服务器一样但是,不上传图像的普通POST请求在远程和本地服务器上都可以正常工作。 我试过的 正如在其他帖子中发现的,我已经将nginx使用Django Rest框架返回500以请求后期映像 前言,django,nginx,file-upload,django-rest-framework,image-uploading,Django,Nginx,File Upload,Django Rest Framework,Image Uploading,我已经做了很多研究来解决我遇到的类似问题,但仍然没有解决我遇到的这个主要问题。作为最后的手段,由于许多案例似乎都是特定于应用程序的,因此我决定在这里创建一个带有我的配置的帖子,希望有人能看到一个可能的解决方案 框架 问题 在使用上述框架的服务器的本地设置中,我可以通过我的客户端应用程序(特别是react本机应用程序,它可能不是重要信息)发布图像但是,当尝试使用相同的图像和URL路径向我的远程服务器发出相同的POST请求时,它返回以下500个错误(从Google Chrome控制台调试器拍摄)
nginx.conf
的client\u max\u body\u 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;
}
}
}
大问题
如有任何想法或反馈,将不胜感激。谢谢。经过大量的研究和尝试,我找到了一个适合我的情况的解决方案 在
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\u body\u temp\u path定义了一个目录,用于存储保存客户端请求主体的临时文件(ref:)。一旦请求变大(即通过图像上传),nginx服务器在处理整个请求时需要一个缓冲区。显然,我的服务器没有权限访问默认的临时路径client\u body\u temp
,因此我手动创建了一个路径为/tmp/client\u body\u temp
的文件夹,权限为chmod 744
,服务器最终以HTTP 200响应。(/tmp
还有一个额外的好处,就是在一段时间后自动清理自身)
注意:更改nginx.conf
文件后,需要运行nginx-s reload
以使用新配置刷新服务器(事先使用nginx-t
检查其格式是否正确)
当然希望这能帮助人们,因为这对我来说是一个巨大的解脱!你试过查看nginx日志文件吗?谢谢你的评论。是的,我已经检查了访问日志和错误日志,我得到的只是访问日志中的一行代码:“POST[path]/upload image/HTTP/1.1”500 186-“okhttp/3.12.1”
([path]是URL的实际相对路径的替代项)
// nginx.conf
...
http {
...
client_max_body_size 50M; // this can be whatever max size you want
client_body_temp_path /tmp/client_body_temp;
...
}
...