讯息;X-Accel-Mapping标头缺失“;在Nginx错误日志中
我在Ubuntu8.04上运行一个Rails 3站点,使用Nginx 1.0.0和Passenger 3.0.7 在我的Nginx error.log中,我开始看到消息讯息;X-Accel-Mapping标头缺失“;在Nginx错误日志中,nginx,rack,sendfile,x-sendfile,Nginx,Rack,Sendfile,X Sendfile,我在Ubuntu8.04上运行一个Rails 3站点,使用Nginx 1.0.0和Passenger 3.0.7 在我的Nginx error.log中,我开始看到消息X-Accel-Mapping头丢失了很多。通过谷歌搜索,我找到了的文档和 现在,我的应用程序可以通过几个域访问,我正在我的应用程序中使用send_file来提供一些特定于他们所请求的域的文件,例如,如果你来到domain1.com/favicon.ico我可以在public/websites/domain1/favicon.ic
X-Accel-Mapping头丢失了很多。通过谷歌搜索,我找到了的文档和
现在,我的应用程序可以通过几个域访问,我正在我的应用程序中使用send_file
来提供一些特定于他们所请求的域的文件,例如,如果你来到domain1.com/favicon.ico
我可以在public/websites/domain1/favicon.ico
中查找favicon。
这很好,我不认为我需要/不想让Nginx参与进来,并创建一些私人区域来存储这些文件,正如本文中的示例所建议的那样
如何消除错误消息?此消息意味着您禁用了Rack::Sendfile
X-Accel-Redirect
,因为您在nginx.conf中缺少它的配置
我使用的是Nginx+乘客3+Rails 3.1
从这几页中收集到的信息我已经弄明白了:
我有一个控制器,它将/download/1
请求映射到具有自己目录结构的存储文件,比如:存储/00/00/1
,存储/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
,它只是内部路径
Rack::Sendfile
获取标题X-Accel-Mapping
,从中提取路径,并将/var/www/shared/storage
替换为/storage…
。然后,它吐出修改后的标题:
X-Accel-Redirect: /storage/00/00/01
然后由nginx处理
我可以看出这是正确的,因为文件下载速度比以前快了100倍,并且日志中没有显示任何错误
希望这能有所帮助。我们使用了与前面描述的NoICE类似的技术,但是我用正则表达式替换了包含所有文件的“硬编码”目录,其中包含包含文件的文件夹 听起来很难,是吗?只要看看这些(
/etc/nginx/sites available/my.web.site
):
这应与此检查一起使用:
location / {
# ...
if (-f $request_filename) {
expires max;
break;
}
# ...
}
防止Rails处理中的静态数据。我按照本手册执行了此操作
我的服务器发送文件路径/private\u upload/file/123/myfile.txt
,文件位于/data/myapp data/private\u 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;
}
嗨@zoopzoop-你有没有想过?我在heroku上托管的rails 3应用程序(使用ngnix)也有同样的问题。这里也有同样的问题。乘客3.0.7,nginx 1.0.0,Ubuntu。不,我还没有找到解决方案。这听起来不错,但对我没有帮助,因为正如我在问题中提到的,我既没有也不想有一个包含所有文件的文件夹。。。有些存储在我的Rails应用程序中的一个目录中,在部署之间共享,有些正在从其他地方动态下载并存储在tmp文件夹中,等等。我只想使用send_file,而不必设置包含所有文件的单独文件夹。可能吗?是的,在这种情况下,你必须为它们中的每一个创建一个内部位置。。。但我不知道它的X-Accel映射格式是什么。。。或者,您可以添加内部位置,如“/=/”。。。
# 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;
}