当请求太大时,Nginx(我认为)以错误的权限保存文件 [英] Nginx (I think) saves files with wrong permissions when request is too large

查看:49
本文介绍了当请求太大时,Nginx(我认为)以错误的权限保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我对托管和 Linux 以及所有方面都是全新的,所以如果我说错了,请原谅我.我还在学习:)

So, I'm completely new to hosting and Linux and all, so please forgive me if I'm saying things that are wrong. I'm still learning :)

我正在使用 Django 创建一个小型个人网站.我想把它放到网上看看是否一切正常.我从 linode 获得了一个便宜的服务器,并使用了 Digital-Ocean 的指南(讽刺的是)我让一切正常.我很高兴.

I'm working a small personal website created using Django. I wanted to get it online to see if everything would work. I got a cheap server from linode and using a guide from Digital-Ocean (the irony) I got everything working. I was happy.

但是...有一个主要问题,我已经阅读了一半的互联网并且找不到任何类似的问题:当发布请求大于~4MB(上传图像等)时,文件将与权限错误.

But... There's one major problem and I've read half the internet and can't find any similar problems: when the post request is larger than ~4MB (uploading images etc.) the files are being saved with the wrong permissions.

当请求大小小于 4MB 时,一切正常,没有任何问题.当请求大小较大时,所有内容都会上传并保存到服务器,没有任何问题,但从那时起,我无法再从浏览器访问文件,并且该站点不断向这些文件抛出 403 禁止访问.那是因为文件突然以错误的权限或类似的方式保存.

When the request size is smaller than about 4MB, everything works without a single problem. When the request size is bigger, everything gets uploaded and saved to the server without a single problem, but from there on I can't access the files from the browser anymore and the site keeps throwing 403 forbidden on those files. That's because the files are suddenly saved with wrong permissions or something like that.

禁止文件的权限:-rw------- 1 [user] root 7900880 May 12 08:24 filename1.pdf

Permissions of a forbidden file: -rw------- 1 [user] root 7900880 May 12 08:24 filename1.pdf

工作文件的权限:-rw-r--r-- 1 [user] root 207420 May 11 19:36 filename2.jpg

Permissions of a working file: -rw-r--r-- 1 [user] root 207420 May 11 19:36 filename2.jpg

我不确定工作文件是否完全正确,因为我认为它不需要 root.

I'm not sure if even the working file is completely correct, since it doesn't need root I think.

我已经厌倦了这个问题.这听起来很不合逻辑;文件大小与权限有什么关系!?事实上,我缺乏这方面的任何经验并没有让解决它变得更容易.如果这里有人有黄金小费,我会很感激:)

I'm getting tired of this problem. It sounds so illogical; what has filesize to do with permissions!? And the fact that I lack any experience with this doesn't make solving it any easier. If anyone here has the golden tip, I'd be so thankful :)

我在 Ubuntu 17.04 上使用 NginxuWSGI 以及 Django 1.11 应用程序.>

I'm using Nginx and uWSGI on Ubuntu 17.04 together with a Django 1.11 application.

推荐答案

在我自己解决这个问题之后,我想通了.AlexD 的大文件以不同方式处理的理论是正确的.

After struggling with this issue myself I figured it out. AlexD's theory of large files being handled differently is correct.

来自 Django 的文档:

From Django's documentation:

FILE_UPLOAD_PERMISSIONS

FILE_UPLOAD_PERMISSIONS

默认值:无

将新上传的文件设置为的数字模式(即 0o644).有关这些模式含义的更多信息,请参阅 os.chmod() 的文档.

The numeric mode (i.e. 0o644) to set newly uploaded files to. For more information about what these modes mean, see the documentation for os.chmod().

如果没有给出或者是 None,你会得到操作系统相关的行为.在大多数平台上,临时文件的模式为 0o600,从内存中保存的文件将使用系统的标准 umask 进行保存.

If this isn’t given or is None, you’ll get operating-system dependent behavior. On most platforms, temporary files will have a mode of 0o600, and files saved from memory will be saved using the system’s standard umask.

所以总结一下:较大的文件使用临时文件存储,默认情况下将获得 0o600 权限.通过将值设置为 FILE_UPLOAD_PERMISSIONS,可以轻松解决该问题.

So in summary: larger files are stored using temporary files, which will get 0o600 permissions by default. The problem can easily be fixed by setting a value to FILE_UPLOAD_PERMISSIONS.

https://docs.djangoproject.com/en/1.11/ref/settings/#file-upload-permissions

这篇关于当请求太大时,Nginx(我认为)以错误的权限保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆