消息“X-Accel-Mapping头丢失”在 Nginx 错误日志中

发布于 2024-11-13 19:12:18 字数 729 浏览 2 评论 0原文

我正在 Ubuntu 8.04 上使用 Nginx 1.0.0 和 Passenger 3.0.7 运行 Rails 3 站点。

在我的 Nginx error.log 中,我开始看到很多消息 X-Accel-Mapping header丢失 。谷歌搜索引导我找到了 Rack::Sendfile 以及 Nginx 文档

现在,我的应用程序可以通过多个域访问,并且我在应用程序中使用 send_file 来传送一些特定于它们所请求的域的文件,例如,如果您访问 domain1.com /favicon.ico 我在 public/websites/domain1/favicon.ico 中查找 favicon。 这工作正常,我认为我不需要/不想让 Nginx 参与并创建一些存储这些文件的私有区域,如 Rack::Sendfile 文档 建议。

我怎样才能摆脱错误消息?

I am running a Rails 3 site on Ubuntu 8.04 with Nginx 1.0.0 and Passenger 3.0.7.

In my Nginx error.log I started seeing the message X-Accel-Mapping header missing quite a lot. Googling lead me to the docs of Rack::Sendfile and to the Nginx docs.

Now, my app can be accessed through several domains and I am using send_file in my app to deliver some files specific to the domain they are requested from, e.g., if you come to domain1.com/favicon.ico I look up the favicon in at public/websites/domain1/favicon.ico.
This works fine and I don't think I need/want to get Nginx involved and create some private area where I store those files, as the samples in the Rack::Sendfile docs suggest.

How can I get rid of the error message?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仅此而已 2024-11-20 19:12:19

此消息意味着 Rack::Sendfile 为您禁用了 X-Accel-Redirect,因为您在 nginx.conf 中缺少它的配置...

我正在使用 < em>Nginx + Passenger 3 + Rails 3.1

从这个页面收集信息我已经弄清楚了:

http://wiki.nginx.org/X-加速

http://greenlegos.wordpress.com/2011/09/12/sending-files-with-nginx-x-accel-redirect

http://code.google.com/p/substruct/source/browse/trunk/gems/rack-1.1.0/lib/rack/sendfile.rb?r=355

通过 Rails 通过 Nginx 提供大文件2.3 使用 x-sendfile

我有一个控制器,它将 /download/1 请求映射到具有自己的目录结构的存储文件,如下所示:storage/00/00/1< /code>、storage/01/0f/15 等。所以我需要通过 Rails 传递它,但随后我需要使用 send_file 方法,该方法将使用X-Accel-Redirect 通过 nginx 直接将最终文件发送到浏览器。

在代码中,我有这样的内容:

send_file(
  '/var/www/shared/storage/00/00/01', 
  :disposition => :inline, 
  :filename => @file.name # an absolute path to the file which you want to send
)

为此示例目的,我替换了文件名

现在,我必须将这些行添加到我的 nginx.conf 中:

server {
    # ... 

    passenger_set_cgi_param HTTP_X_ACCEL_MAPPING /var/www/shared/storage/=/storage/; 
    passenger_pass_header X-Accel-Redirect;

    location /storage {
      root /var/www/shared;
      internal;
    }

    # ...
}

路径 /storage< /code> 从外部世界看不到,它仅在内部可见。

Rack::Sendfile 获取标头 X-Accel-Mapping,从中提取路径并将 /var/www/shared/storage 替换为/存储...。然后它会吐出修改后的标头:

X-Accel-Redirect: /storage/00/00/01

然后由nginx处理。

我可以看到它工作正常,因为文件下载速度比以前快了 100 倍,并且日志中没有显示错误。

希望这有帮助。

this message means that Rack::Sendfile disabled X-Accel-Redirect for you, because you have missing configuration for it in nginx.conf...

I'm using Nginx + Passenger 3 + Rails 3.1.

Gathered information from this pages I've figured it out:

http://wiki.nginx.org/X-accel

http://greenlegos.wordpress.com/2011/09/12/sending-files-with-nginx-x-accel-redirect

http://code.google.com/p/substruct/source/browse/trunk/gems/rack-1.1.0/lib/rack/sendfile.rb?r=355

Serving Large Files Through Nginx via Rails 2.3 Using x-sendfile

I have controller which maps /download/1 requests to storage files which have their own directory structure, like this: storage/00/00/1, storage/01/0f/15 etc. So I need to pass this through Rails, but then I need to use send_file method which will use X-Accel-Redirect to send the final file to the browser through nginx directly.

Within the code I have this:

send_file(
  '/var/www/shared/storage/00/00/01', 
  :disposition => :inline, 
  :filename => @file.name # an absolute path to the file which you want to send
)

I replaced the filename for this example purposes

Now I had to add these lines to my nginx.conf:

server {
    # ... 

    passenger_set_cgi_param HTTP_X_ACCEL_MAPPING /var/www/shared/storage/=/storage/; 
    passenger_pass_header X-Accel-Redirect;

    location /storage {
      root /var/www/shared;
      internal;
    }

    # ...
}

The path /storage is not visible from outside world, it is internal only.

Rack::Sendfile gets the header X-Accel-Mapping, extracts the path from it and replaces /var/www/shared/storage with /storage.... Then it spits out the modified header:

X-Accel-Redirect: /storage/00/00/01

which is then processed by nginx.

I can see this works correctly as the file is downloaded 100x faster than before and no error is shown in the logs.

Hope this helps.

吾家有女初长成 2024-11-20 19:12:19

我们使用了与 NoICE 描述的类似技术,但我用描述包含包含文件的文件夹的文件夹的正则表达式替换了包含所有文件的“硬编码”目录

听起来很难,是吗?只需看一下这些(/etc/nginx/sites-available/my.web.site):

location /assets/(.+-[a-z0-9]+\.\w+) {
    root /home/user/my.web.site/public/assets/$1;
    internal;
}

location /images/(.+)(\?.*)? {
    root /home/user/my.web.site/public/images/$1;
    internal;
}

这应该与此检查一起使用:

location / {
    # ...

    if (-f $request_filename) {
        expires max;
        break;
    }

    # ...
}

以防止 Rails 处理静态数据。

We used the similar technique as NoICE described, but i replaced the "hard-coded" directory containing all the files with the regular expression describing the folder containing the folders containing the files.

Sounds hard, yeah? Just take a look on these (/etc/nginx/sites-available/my.web.site):

location /assets/(.+-[a-z0-9]+\.\w+) {
    root /home/user/my.web.site/public/assets/$1;
    internal;
}

location /images/(.+)(\?.*)? {
    root /home/user/my.web.site/public/images/$1;
    internal;
}

This should be used with this check:

location / {
    # ...

    if (-f $request_filename) {
        expires max;
        break;
    }

    # ...
}

to prevent the statics from Rails processing.

眼前雾蒙蒙 2024-11-20 19:12:19

我通过本手册

https://mattbrictson.com/accelerated-rails-downloads

我的服务器发送文件路径 /private_upload/file/123/myfile.txt,文件位于/data/myapp-data/private_upload/file/123/myfile.txt

    # Allow NGINX to serve any file in /data/myapp-data/private_upload
    # via a special internal-only location.
    location /private_upload {
      internal;
      alias /data/myapp-data/private_upload;
    }

    # ---------- BACKEND ----------
    location @backend
    {
        limit_req zone=backend_req_limit_per_ip burst=20 nodelay;
        proxy_pass http://backend;
        proxy_set_header X-Sendfile-Type X-Accel-Redirect;
        proxy_set_header X-Accel-Mapping /=/; # this header is required, it does nothing
        include    /etc/nginx/templates/myapp_proxy.conf;
    }

I did by this manual

https://mattbrictson.com/accelerated-rails-downloads

my server sends file path /private_upload/file/123/myfile.txt, the file is in /data/myapp-data/private_upload/file/123/myfile.txt

    # Allow NGINX to serve any file in /data/myapp-data/private_upload
    # via a special internal-only location.
    location /private_upload {
      internal;
      alias /data/myapp-data/private_upload;
    }

    # ---------- BACKEND ----------
    location @backend
    {
        limit_req zone=backend_req_limit_per_ip burst=20 nodelay;
        proxy_pass http://backend;
        proxy_set_header X-Sendfile-Type X-Accel-Redirect;
        proxy_set_header X-Accel-Mapping /=/; # this header is required, it does nothing
        include    /etc/nginx/templates/myapp_proxy.conf;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文