title: X-Sendfile, Rails & Nginx
body: <p>I'm getting frustated about my problem with X-Sendfile,NGinx and Rails. I've read several docs and tutorials but I just don't get the point.</p>
  
  <p>Whatever I'm trying, I'm getting a 404. Here's the X-Sendfile part of NGINX.</p>
  
  <pre><code> location / {
                      proxy_set_header X-Sendfile-Type X-Accel-Redirect;
                      proxy_set_header X-Accel-Mapping /var/www/cube_storage/uploads/=/cdn;  #maps a real path to the internal location
                      ...
              } # end of location app
  
              location /cdn {
                      root /var/www/cube_storage/;
                      internal;
                      proxy_set_header X-Sendfile-Type X-Accel-Redirect;
                      proxy_set_header X-Accel-Mapping /var/www/cube_storage/=/cdn/;
                      }
  </code></pre>
  
  <p>X-Accel-Redirect is enabled in my rails app:</p>
  
  <pre><code>config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
  </code></pre>
  
  <p>Here's the code of the controller which is responsible for serving the file:</p>
  
  <pre><code>def image
  @activity = AccountActivity.where(:id =&gt; params[:id]).first
  
  if !@activity || !@activity.file_attachment_is_image? || @activity.file_attachment_name != requested_file_name()
    l = Logger.new("#{Rails.root}/logs/cdn_activity.log")
    l.info(params.inspect)
  
    render :file =&gt; "#{Rails.root}/public/404.html", :layout =&gt; false, :status =&gt; :not_found
  else
    begin
      if !params[:version] || params[:version] == "original"
        t = @activity.file_attachment # thats the original image!
      else
        t = @activity.file_attachment.send(params[:version])
      end
  
      send_file t.current_path  ,:disposition =&gt; 'inline'
    rescue Exception =&gt; ex
      l = Logger.new("#{Rails.root}/logs/cdn_activity.log")
      l.info("--------------------------")
      l.info(Time.now.to_s)
      l.info("exception -&gt; #{ex.message}")
      l.info("params: #{params.inspect}")
      render :file =&gt; "#{Rails.root}/public/404", :layout =&gt; false, :status =&gt; :not_found
    end
  
  end
  </code></pre>
  
  <p>end</p>
  
  <p>I use carrierwave as storage engine. Maybe anyone just sees my mistake, but I don't even after several hours of trying.</p>
  
  <p>One more thing, the request doesn't even show up in the development.log (yes I activated X-Accel for dev too).</p>
  
  <p>Regards, Alex</p>