I cant manage to make Nginx handle my file downloads. They keep being sent by Rails directly, which is not a good idea for known reasons.
Files to be downloaded by an iOS app are zipped on an Amazon EC2 instance and put into a folder called /var/app/current/tmp/zip on request. I made sure the files actually are available in that folder by logging into the instance.
Now from within a rails controller the files are sent like this:
send_file("tmp/zip/#{filename}.zip", :type => 'application/zip', :filename => "documents.zip", :x_sendfile => true, :disposition => "inline")
The client receives the files with headers:
Cache-Control:private
Connection:keep-alive
Content-Disposition:attachment; filename="documents.zip"
Content-Transfer-Encoding:binary
Content-Type:application/zip
Date:Tue, 27 Sep 2016 06:58:51 GMT
Server:nginx/1.8.1 + Phusion Passenger 4.0.60
Status:200 OK
transfer-encoding:chunked
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-Powered-By:Phusion Passenger 4.0.60
X-Request-Id:6809ca4a-fad5-4cf6-8b98-a8176d26487a
X-Runtime:9.405918
X-XSS-Protection:1; mode=block
The above tells me that the file is sent from rails and was not handled by Nginx as it should.
The configuration I'm using:
config/environments/production.eb:
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
ebextensions/nginx.config:
files:
'/etc/nginx/conf.d/000_my_config.conf':
content: |
proxy_connect_timeout 1000;
proxy_send_timeout 1000;
proxy_read_timeout 1000;
send_timeout 1000;
server {
proxy_set_header X-Sendfile-Type X-Accel-Redirect;
proxy_set_header X-Accel-Mapping /var/app/current/tmp/zip/=/storage/;
#passenger_set_header X-Sendfile-Type "X-Accel-Redirect";
#passenger_env_var HTTP_X_ACCEL_MAPPING /var/app/current/=/storage/;
#passenger_pass_header X-Accel-Redirect;
location /storage/ {
root /var/app/current/tmp/zip/;
internal;
}
}
Still nginx doesnt handle the file transfer. Server logs show:
-------------------------------------
/var/app/support/logs/passenger.log
-------------------------------------
App 28302 stderr: X-Accel-Mapping header missing
I also tried the "passenger_" directives but they fail with:
>sudo service nginx restart
unknown directive "passenger_set_header" in /etc/nginx/conf.d/000_my_config.conf:17
My last try was to set the X-header from within the controller directly:
response.headers["X-Accel-Redirect"] = "/var/app/current/tmp/zip/#{filename}.zip"
This fails with:
production.log
INFO -- : Started GET "/var/app/current/tmp/zip/FxPNa3nB--Kqt8L51PT3sg.zip", FATAL -- :
ActionController::RoutingError (No route matches [GET] "/var/app/current/tmp/zip/FxPNa3nB--Kqt8L51PT3sg.zip"):
actionpack (4.2.4) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.4) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.4) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.4) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.4) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.4) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.4) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.4) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.4) lib/rails/engine.rb:518:in `call'
railties (4.2.4) lib/rails/application.rb:165:in `call'
passenger (4.0.60) lib/phusion_passenger/rack/thread_handler_extension.rb:74:in `process_request'
passenger (4.0.60) lib/phusion_passenger/request_handler/thread_handler.rb:141:in `accept_and_process_next_request'
passenger (4.0.60) lib/phusion_passenger/request_handler/thread_handler.rb:109:in `main_loop'
passenger (4.0.60) lib/phusion_passenger/request_handler.rb:455:in `block (3 levels) in start_threads'
I'm running on Amazon EBS 64bit Amazon Linux 2016.03 v2.1.3 with Ruby 2.2 (Passenger Standalone).
Any help much appreciated.