当前设置:
- 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" 失败(2:没有这样的文件或目录),客户端:xx.xx.example.example,服务器:photos.example.com,请求:“GET /imgz/users/1354424400_lmDdkskvEPbrtUYBudgzsLjiOXvywx_gwslfBWlBk_me.jpg HTTP/1.1”,主机:“photos.example.com”,推荐人:“ http://dev.example.com/lmDdkskvEPbrtUYBudgzsLjiOXvywx ”
编辑2:
实际上,这是在 POST 之后发生的 GET 请求。这是 Django 应用程序试图显示刚刚上传的图像。如果我没记错的话,表单的 POST 几乎总是成功的。如果我更改路径,它只会在 /var/www 中创建一个不同的路径。