Nginx upload_module只会在/ var / www中存储文件

时间:2012-12-02 09:53:20

标签: nginx

当前设置:

  • nginx侦听端口80,将example.com请求转发到8080上的Apache
  • Apache侦听端口8080,通过wsgi
  • 连接到Django应用程序
  • Django表单将图片上传到子域photos.example.com/upload /

这似乎是来自我在nginx中的配置文件,但我不能指责它。我大多尝试在nginx中更改upload_store指令。 我希望用户上传的照片存储在/ var / www / dev / source / nginx / media /

我现在能做的最好的是从/ var / www到我想要的文件夹的符号链接。

相关的nginx配置:

server {
        client_max_body_size 100m;
        listen 80;
        server_name photos.example.com;
        access_log /var/log/nginx/photos.example.com/access.log;
        error_log /var/log/nginx/photos.example.com/error.log;


        # Upload form should be submitted to this location
        location /upload {
                # Pass altered request body to this location

                # Store files to this location
                upload_store /var/www/dev/source/nginx/media 1;
                # Set specified fields in request body
                upload_set_form_field $upload_field_name.name "$upload_file_name";
                upload_set_form_field $upload_field_name.content_type "$upload_content_type";
                upload_set_form_field $upload_field_name.path "$upload_tmp_path";
        }
        # Pass altered request body to a proxy

        location / {
                root /var/www/dev/source/nginx/media;
        }
}

相关models.py

def rename_image(instance,filename):
    random = ''.join([choice(letters) for i in xrange(10)])
    today = datetime.date.today()
    userid = instance.user.username
    filename = '_'.join([today.strftime('%s'),userid,random,filename])
    return'/'.join(['images','users',userid,filename])


class Users(models.Model):
    user        = models.OneToOneField(User)
    ...
    photo       = models.ImageField(upload_to=rename_image)

编辑:

当我尝试将其设置为另一个路径(构成一个名为imgz的目录作为测试)时,这是在nginx错误日志中:(我之后也创建了imgz以查看是否会修复它,但它确实不)

2012/12/02 09:37:44 [错误] 31229#0:* 56472 open()" /var/www/dev/source/nginx/media/imgz/users/1354424400_lmDdkskvEPbrtUYBudgzsLjioxvywx_gwslfBWlBk_me.jpg&# 34;失败(2:没有这样的文件或目录),客户端:xx.xx.example.example,server:photos.example.com,request:" GET /imgz/users/1354424400_lmDdkskvEPbrtUYBudgzsLjioxvywx_gwslfBWlBk_me.jpg HTTP / 1.1&#34 ;,主持人:" photos.example.com",推荐人:" http://dev.example.com/lmDdkskvEPbrtUYBudgzsLjiOXvywx"

EDIT2:

实际上,这是在POST之后发生的GET请求。它是试图显示刚刚上传的图像的Django应用程序。如果我没记错的话,表格的POST几乎总是成功的。如果我改变路径,它只是在/ var / www。

中创建一个不同的路径

0 个答案:

没有答案