2013-01-17 82 views
3

在我的項目中有一個問題。不要上傳文件。這裏是我的代碼不要通過nginx上傳文件幫助上傳模塊

配置nginx的

server{ 

listen 9000; 
server_name localhost; 
root /home/path/my/project; 

location ~ ^/(static|media)/ { 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header Host $http_host; 
    proxy_redirect off; 
} 

location /file-upload { 
    internal; 
    upload_pass /upload-file/; 
    upload_store /home/payh/my/project/media/video; 
    upload_max_file_size 250m; 

    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"; 

    upload_pass_form_field "^csrfmiddlewaretoken$"; 

    upload_limit_rate 2m; 
    track_uploads upload 1m; 
} 

location = /upload/progress { 
    report_uploads upload; 
} 

location/{ 
    proxy_pass_header Server; 
    proxy_set_header Host $http_host; 
    proxy_redirect off; 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Scheme $scheme; 
    proxy_connect_timeout 10; 
    proxy_read_timeout 10; 
    proxy_pass http://localhost:8000/; 
} 
error_page 500 502 503 504 /media/50x.html; 
} 

請點擊按鈕上傳後nginx_accel進來功能。然後查看爲nginx創建響應併發送它。不允許Nginx返回405。我不知道爲什麼。 我views.py

@login_required 
@csrf_protect 
def upload(request): 
    context = RequestContext(request) 

    if request.method == 'POST': 
     pathfile = request.POST.get('upfile.path') 
     upfile = open(pathfile, 'r') 
     file = File(upfile) 

     form = NewFileForm(request.POST, initial={'upfile':file}) 
     if form.is_valid(): 
      form.save() 
      return HttpResponseRedirect(reverse(upload)) 
    else: 
     form = NewFileForm() 

    return render_to_response('upload.html', 
          {'form':form}, 
          context_instance=context)  
@login_required 
@csrf_protect 
def nginx_accel(request): 
    if request.user.id == 1: 
     file = request.FILES.get('upfile').name 
     url = '/'.join(['/file-upload', file]) 
     response = HttpResponse() 

     response['Content-Type']= '' 
     response['X-Accel-Redirect'] = url 

     return response 

Nginx的瀏覽器返回 「405不允許」。誰能說爲什麼會出現這個問題?

回答

0

錯誤的原因是這樣的file = request.FILES.get('upfile').name。它將繞過並嘗試在POST請求上提供靜態文件,導致405錯誤。解決你的問題。

@login_required 
@csrf_protect 
def nginx_accel(request): 
    if request.method == 'GET': 
     if request.user.id == 1: 
      file = request.GET.get('upfile').name 
      url = '/'.join(['/file-upload', file]) 
      response = HttpResponse() 

      response['Content-Type']= '' 
      response['X-Accel-Redirect'] = url 

      return response 
0

try:remove string internal;
in location/file-upload
如果請求的方法不是POST,模塊將返回錯誤405(方法不允許)。使用這種方法的請求可以通過error_page指令在其他位置處理。 more info