【发布时间】: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.conf 的client_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;
}
}
}
大问题
- 为什么 nginx 会返回 500 错误?
- 有没有办法从 nginx 获取更详细的错误消息,以了解为什么返回 500 错误?
- 我的配置/设置设置是否符合我预期的图片上传方式,或者是否有改进或缺失的部分可以解决此问题?
任何想法或反馈将不胜感激。谢谢。
【问题讨论】:
-
你试过查看 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