简体   繁体   中英

nginx 405 - uploading img from ember to rails

I implemented an image upload in Ember, with dropzone addon (ember-cli-dropzonejs) . Rails 5 is running on the server (Ubuntu 14.x), imagemagick is installed in the Rails Docker Container and all files should go to the folder "uploads". Everything is running as docker containers, so rails, frontend, database and nginx (1.11.4) are seperate containers. Now, if I try to upload an image, the server response is a 405 and rails didn't get anything (nothing in the log).

ember hbs file

{{drop-zone url=uploadURL
            method='PATCH'
            headers=authentication acceptedFiles='.jpg, .jpeg, .gif, .png'
            resize=''
            maxFilesize='10'
            dictDefaultMessage='Upload file'
            success=(action 'succesfulFileUpload')
            complete=resetUploader
            previewTemplate='<div></div>'
            uploadprogress=progressManipulation
            sending=sending
            }}

rails uploader.rb

class AvatarUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  version :thumb do
    process resize_to_fill: [200, 200]
  end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  def extension_whitelist
    %w(jpg jpeg gif png)
  end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  def filename
    return unless original_filename
    contenttype = original_filename.split('.').last
    "avatar.#{contenttype}"
  end
end

nginx config

server {
  listen 443 ssl;

  server_name example.com;

    ....
    ssl stuff
    .... 

  location / {
    proxy_read_timeout 150;
    proxy_set_header        Host $host:$server_port;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        X-Forwarded-Proto $scheme;
    proxy_pass http://backend:3000;
  }

}

Request: Request img details

Response: Response img output

If i put the following line into the neginx config (like recommended 100 times on stackoverflow)...

error_page  405     =200 $uri;

... the server throws the error:

Can not read property 'attributes' of undefined

This error message is from dropzone. It can't complete the upload. In rails there is nothing received, no entry in logs, nothing...

UPDATE & SOLUTION Finaly i fixed it. The problem was the wrong uploadURL in Handlebars. Local it worked with this wrong url because it was an relative path. But in production an abolute path was needed.

Finaly i fixed it. The problem was the wrong uploadURL in Handlebars. Local it worked with this wrong url because it was an relative path. But in production an abolute path was needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM