Падение сервера при загрузке файла в приложении Django на DigitalOcean
Я разработал приложение, в котором пользователь может загрузить zip-файл, zip-файл распаковывается и сохраняется во вновь созданной папке с помощью os.mkdir()
На локальной машине процесс работает отлично, но при загрузке на digitalocean в производственном режиме (gunicorn + nginx) процесс загрузки приводит к ошибке браузера "Site cannot be reached", даже не к ошибке HTTP, где мы можем отследить проблему.
Вот все элементы, которые вам могут понадобиться:
views.py:
@user_passes_test(lambda u: u.is_staff or u.is_superuser)
def createCategory(request):
if request.method == "POST":
name = request.POST.get("name_kw")
lang = request.POST.get("lang_kw")
moduleid = request.POST.get("module_kw")
zip = request.FILES.get("zip_kw")
category = Category()
category.name = name
category.lang = lang
category.module = Module.objects.get(id=moduleid)
category.zip = zip
category.created_by = request.user
try:
if zip.name.endswith(".zip"):
rtn = publish_doc(category.name, zip)
if rtn == "No index file found":
messages.error(
request,
"Pas de fichier index.html détecté. Assuez-vous qu'un fichier index.html est présent dans votre fichier ZIP",
)
return redirect("categoryList")
else:
category.root = rtn
category.save()
messages.success(request, "Catégorie créée avec succès")
print("categorie et zip créées")
return redirect("categoryList")
else:
messages.error(
request,
"Seuls les fichiers .ZIP sont acceptés",
)
return redirect("categoryList")
except IntegrityError:
print("erreuuuuuuuur")
messages.error(
request,
"Une erreur s'est produite. Veuillez réessayer plutard, ou contacter notre équipe technique.",
)
utils.py:
def publish_doc(catg_name, zip):
# create folder in machine
path = os.path.join("/documenter/docs/", catg_name)
try:
os.mkdir(path)
except OSError as error:
print(error)
print("Directory '% s' created" % path)
# check if contents have an index html file
zipObj = ZipFile(zip, "r")
if "index.html" in zipObj.namelist():
zipObj.extractall(path)
# return root path
return path
else:
return "No index file found"
settings.py:
FILE_UPLOAD_MAX_MEMORY_SIZE = 52428800 # 50 Mbytes
FYI: zip-файл, который я пытаюсь загрузить, имеет размер 6,35 Мб.
А вот конфигурация nginx:
server {
listen 80;
server_name 68.183.76.212;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /documenter/site/public;
}
location /media/ {
root /documenter/site/public;
client_max_body_size 50m;
}
location /docs/ {
root /documenter/docs;
client_max_body_size 50m;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
После загрузки файла я получаю эту ошибку (см. прикрепленное изображение).