{"id":1087,"date":"2010-11-22T03:11:08","date_gmt":"2010-11-21T18:11:08","guid":{"rendered":"http:\/\/ecpplus.net\/weblog\/?p=1087"},"modified":"2020-06-15T22:06:21","modified_gmt":"2020-06-15T13:06:21","slug":"nginx-%e3%81%ae-x-accel-redirect-%e3%82%92-rails-%e3%81%a7%e4%bd%bf%e3%81%86","status":"publish","type":"post","link":"https:\/\/ecpplus.net\/weblog\/nginx-%e3%81%ae-x-accel-redirect-%e3%82%92-rails-%e3%81%a7%e4%bd%bf%e3%81%86\/","title":{"rendered":"Nginx \u306e X-Accel-Redirect \u3092 Rails \u3067\u4f7f\u3046"},"content":{"rendered":"<p>\u3000Nginx \u3067\u8a8d\u8a3c\u3064\u304d\u3067\u5927\u5bb9\u91cf\u30d5\u30a1\u30a4\u30eb\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u305b\u305f\u3044\u6642\u306f\u3001X-Accel-Redirect \u3092\u4f7f\u3044\u307e\u3059\u3002Rails \u306e\u30d5\u30a1\u30a4\u30eb\u64cd\u4f5c\u3092\u4ecb\u3055\u305a\u3001Nginx\u3067\u76f4\u63a5\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3078\u30c7\u30fc\u30bf\u3092\u9001\u308c\u307e\u3059\u3002Lighttpd \u3068\u304b Apache \u3067\u3044\u3046\u3068\u3053\u308d\u306e\u3001X-sendfile \u3068\u540c\u3058\u3088\u3046\u306a\u611f\u3058\u3067\u3057\u3087\u3046\u304b\u3002<br \/>\nLighttpd \u306e\u5834\u5408\u306f\u3053\u3061\u3089 -> <a href=\"http:\/\/ecpplus.net\/weblog\/railslighttp%E3%81%A7%E5%A4%A7%E5%AE%B9%E9%87%8F%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%82%92%E3%83%80%E3%82%A6%E3%83%B3%E3%83%AD%E3%83%BC%E3%83%89\/\">Rails&amp;Lighttpd\u3067\u5927\u5bb9\u91cf\u30d5\u30a1\u30a4\u30eb\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3010\u5909\u306a\u30e4\u30d0\u3044\u3082\u3093\u30ed\u30b0\u5185\u3011<\/a><\/p>\n<p>\u3000\u30d8\u30c3\u30c0\u306b\u3001X-Accel-Redirect \u3067\u30d1\u30b9\u3092\u6307\u5b9a\u3057\u3066\u3001\u305d\u306e\u30d1\u30b9\u3092 Nginx \u5074\u3067\u53d7\u3051\u308b\u3060\u3051\u3067\u3059internal \u3063\u3066\u4ed8\u3051\u308b\u306e\u304c\u30dd\u30a4\u30f3\u30c8\u3067\u3059\u3002\u305d\u3046\u3059\u308b\u3068\u3001\u3053\u306e\u3088\u3046\u306b\u5185\u90e8\u304b\u3089\u3057\u304b\u53c2\u7167\u3067\u304d\u306a\u304f\u306a\u308a\u307e\u3059\u3002\u5916\u304b\u3089\u305d\u306e\u30d1\u30b9\u3092\u53e9\u3044\u3066\u3082\u898b\u308c\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3000Rails2\u4ee5\u524d\u306e\u5834\u5408\u306f\u81ea\u5206\u3067\u30d8\u30c3\u30c0\u306b\u5024\u3092\u8a2d\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001Rails3\u4ee5\u964d\u306f\u8a2d\u5b9a\u3092\u5909\u66f4\u3059\u308b\u3060\u3051\u3067\u3044\u3051\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n<pre lang=\"rails\"># Rails2\u4ee5\u524d\ndef download\n  # \u8a8d\u8a3c\u51e6\u7406\u306a\u3069\u3092\u631f\u3080\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002  \n  path = \"\/bigfiles\/long_movie.mp4\"\n  response.headers['X-Accel-Redirect'] = path\n  response.headers['Content-Type'] = \"application\/force-download\" \n  response.headers['Content-Disposition'] = \"attachment; filename=\\\"#{File.basename(path)}\\\"\" \n  response.headers['Content-length'] = File.size(RAILS_ROOT + path)\n  render :nothing => true\nend\n\n# Rails3\ndef download\n  # \u8a8d\u8a3c\u51e6\u7406\u306a\u3069\u3092\u631f\u3080\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002  \n  path = \"\/bigfiles\/long_movie.mp4\"\n  send_file path, \n             :type => \"application\/force-download\",\n             :disposition => \"attachment; filename=\\\"#{File.basename(path)}\\\"\"\nend\n\n# config\/environments\/production.rb\n# \u4e0b\u8a18\u884c\u3092\u30b3\u30e1\u30f3\u30c8\u30a2\u30a6\u30c8 (Apache, lighttpd \u306e\u5834\u5408\u306f\u3053\u306e\u884c\u3092\u4f7f\u3046)\n# config.action_dispatch.x_sendfile_header = \"X-Sendfile\"\n# \u4e0b\u8a18\u306e\u884c\u3092\u6709\u52b9\u306b\u3059\u308b (nginx\u306e\u5834\u5408\u306f\u3053\u306e\u884c\u3092\u4f7f\u3046)\nconfig.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'<\/pre>\n<pre lang=\"\">location \/ {\n    client_max_body_size    200m;\n    proxy_read_timeout 60;\n    proxy_connect_timeout 60;\n    proxy_pass http:\/\/railsapp;\n}\n\nlocation \/bigfiles {\n    # -> \/path\/to\/railsroot\/bigfiles\n    root \/path\/to\/railsroot\/;\n    internal;\n}<\/pre>\n<p>gist \u306b\u4e0a\u3052\u3066\u3042\u308a\u307e\u3059\u3002<a href=\"https:\/\/gist.github.com\/708959\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/gist.github.com\/708959<\/a><\/p>\n<h3>\u88dc\u8db3<\/h3>\n<p>\u3000\u9006\u306b\u3001\u3061\u3087\u3063\u3068\u3057\u305f\u30a2\u30d7\u30ea\u3092\u52d5\u304b\u3059\u3068\u304d\u306b\u3001\u3044\u3061\u3044\u3061 nginx \u306e\u8a2d\u5b9a\u3068\u304b\u305b\u305a\u306b Unicorn \u3068\u304b\u304b\u3089\u76f4\u63a5\u30d5\u30a1\u30a4\u30eb\u3092\u9001\u308a\u305f\u3044\u5834\u5408\u3082\u3042\u308a\u307e\u3059\u3002Rails3\u306b\u306a\u3063\u3066\u304b\u3089\u3001config\/environments\/productioin.rb \u304c\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u307e\u307e\u3060\u3068 send_file \u3059\u308b\u3068 0bytes \u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<p>\u3000\u4e0b\u8a18\u306e2\u884c\u3092\u30b3\u30e1\u30f3\u30c8\u30a2\u30a6\u30c8\u3057\u3066\u304a\u304f\u3068\u3001Unicorn \u306a\u308a\u3067\u76f4\u63a5\u30d5\u30a1\u30a4\u30eb\u9001\u4fe1\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre lang=\"rails\"># config.action_dispatch.x_sendfile_header = \"X-Sendfile\"\n# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'<\/pre>\n<p>\u3000\u3064\u3044\u3067\u306b\u3001\u9759\u7684\u30d5\u30a1\u30a4\u30eb\u3082 Unicorn \u306a\u308a\u3067\u8868\u793a\u3057\u305f\u3044\u3088\u3046\u306a\u624b\u629c\u304d\u69cb\u6210\u306e\u3068\u304d\u306f\u3001\u4e0b\u8a18\u306e\u8a2d\u5b9a\u3082\u304a\u5fd8\u308c\u306a\u304f\u3002<\/p>\n<pre lang=\"rails\">config.serve_static_assets = false<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u3000Nginx \u3067\u8a8d\u8a3c\u3064\u304d\u3067\u5927\u5bb9\u91cf\u30d5\u30a1\u30a4\u30eb\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u305b\u305f\u3044\u6642\u306f\u3001X-Accel-Redirect \u3092\u4f7f\u3044\u307e\u3059\u3002Rails \u306e\u30d5\u30a1\u30a4\u30eb\u64cd\u4f5c\u3092\u4ecb\u3055\u305a\u3001Nginx\u3067\u76f4\u63a5\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3078\u30c7\u30fc\u30bf\u3092\u9001\u308c\u307e\u3059\u3002Lighttpd \u3068\u304b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2521,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[20,10],"class_list":["post-1087","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","tag-nginx","tag-rails"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/1087","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/comments?post=1087"}],"version-history":[{"count":2,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/1087\/revisions"}],"predecessor-version":[{"id":2523,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/1087\/revisions\/2523"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/media\/2521"}],"wp:attachment":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/media?parent=1087"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/categories?post=1087"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/tags?post=1087"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}