我正在使用xsendfile和我的Rails 3应用程序遇到问题.
我使用capistrano来管理部署,在每个版本中,都有一个指向shared/assets目录的符号链接(例如/ var/www/site/releases/1234/assets =>/var/www/site/shared/assets ).问题是XSendFile似乎没有遵循符号链接.在我的apache日志中,我看到以下错误:
The given path was above the root path: xsendfile: unable to find file: /var/www/site/releases/20110406205607/assets/pdfs/2/original/test.pdf
Run Code Online (Sandbox Code Playgroud)
我将XSendFilePath配置设置为
XSendFilePath /var/www/site/shared/assets
Run Code Online (Sandbox Code Playgroud)
如果我将配置切换到:
XSendFilePath /var/www/site/releases
Run Code Online (Sandbox Code Playgroud)
一切正常.所以我有几个问题:
1)有没有办法让XSendFilePath遵循符号链接?
2)将XSendFilePath设置为我的发布版dir是否存在安全风险?换句话说,我是否打开了对所有目录的访问权限?
前段时间我写了一个关于在rails应用程序中使用临时文件的问题.在特定情况下,我决定使用tempfile
如果我还想使用该x-sendfile指令(作为Rails 2中的参数,或作为Rails 3中的配置选项),这会导致问题,因此文件发送由我的Web服务器直接处理,而不是我的rails应用程序.
所以我想做这样的事情:
require 'tempfile'
def foo()
# creates a temporary file in tmp/
Tempfile.open('prefix', "#{Rails.root}/tmp") do |f|
f.print('a temp message')
f.flush
send_file(f.path, :x_sendfile => true) # send_file f.path in rails 3
end
end
Run Code Online (Sandbox Code Playgroud)
此设置有一个问题:文件在发送之前被删除!
一方面,tempfile一旦Tempfile.open块结束就会删除文件.另一方面,x-sendfile使send_file调用异步 - 它返回非常快,因此服务器几乎没有时间发送文件.
我现在最好的解决方案是使用非临时文件(File而不是Tempfile),然后是定期擦除temp文件夹的cron任务.这有点不优雅,因为:
有更好的设置吗?或者,异步上至少有一个"成功"回调send_file,所以我可以在完成后删除f吗?
非常感谢.
我正在寻找一种方法来确认X-Sendfile是否正确处理由脚本(PHP)返回给Web服务器的请求.图像正在正确提供,但我想我会在curl请求中看到标题.
$ curl -I http://blog2.stageserver.net/wp-includes/ms-files.php?file=/2011/05/amos-lee-feature.jpg
HTTP/1.1 200 OK
Date: Wed, 04 Jan 2012 17:19:45 GMT
Server: Cherokee/1.2.100 (Arch Linux)
ETag: "4dd2e306=9da0"
Last-Modified: Tue, 17 May 2011 21:05:10 GMT
Content-Type: image/jpeg
Content-Length: 40352
X-Powered-By: PHP/5.3.8
Content-Disposition: inline; filename="amos-lee-feature.jpg"
Run Code Online (Sandbox Code Playgroud)
在FastCGI中使用PHP-FPM 5.3.8的Cherokee 1.2.100 :(
cherokee.conf: vserver!20!rule!500!handler!xsendfile = 1
由vServer> Behavior> Extensions设置php> Handler:允许X-Sendfile [check Enabled])
Wordpress网络/ WPMU 3.3.1:
define('WPMU_SENDFILE',true);在包含wp-config.php之前设置如下wp-settings.php.这将触发以下代码在WP的wp-includes/ms-files.php中执行:50为特定博客提供文件:
header( 'X-Sendfile: ' . $file );
exit;
Run Code Online (Sandbox Code Playgroud)
我已经确认上面的代码片段正在通过在exit();调用之前添加一个用于处置的附加标头来执行.Content-Disposition与curl结果一起出现,而不是最初在ms-files.php代码中.添加的代码是:
header('Content-Disposition: inline; filename="'.basename($file).'"');
我有: …
当我将Lion服务器升级到Mountain Lion时,它似乎从apache中删除了我的mod_xsendfile.
我按照本网站的说明进行操作,但遇到了"没有这样的文件或目录"错误.在谷歌的帮助下,我发现这个页面给了我解决方案:"sudo ln -s /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/ /Applications/Xcode.app/Contents/Developer/工具链/ OSX10.8.xctoolchain"
看起来不错,但是当我尝试运行"sudo apxs -cia mod_xsendfile.c"时,我收到"致命错误:'apr.h'文件未找到"错误.
有任何想法吗?
我正在尝试在Windows(7)x64(使用Apache 2.2)上安装mod_xsendfile Apache模块 - 是的,我从一开始就注定了,我知道:-).显然有:
a)没有用于mod_xsendfile的Win x64二进制文件,只是来自模块网站的Win32二进制文件
b)没有来自ApacheLounge的 apxs的Win x64二进制文件
我已经尝试了平常LoadModule xsendfile_module modules/mod_xsendfile.so但是出现了半明显的错误(httpd: Syntax error on line 127 of C:/Apache/conf/httpd.conf: Cannot load C:/Apache/modules/mod_xsendfile.so into
server: The specified module could not be found.),它不是Win x64兼容的.
问题仍然存在 - 如何为x64构建模块,甚至是可能的?我有VS和任何可能需要的工具.
我只是想看看这是否会提高我的Rails保护附件下载速度 - 目前对简单图像的速度非常可怕.
先感谢您 !
我是python的新手,仍在学习。我在 pythonanwhere 上创建了一个小的 python 3.6 Flask webapp,发现 send_file() 在 pythonanywhere 服务器上不起作用。我正在积极寻找直接在用户计算机上下载 excel 文件的替代方法。我也试过Response但它没有提供所需的输出。我在网上阅读了很多关于它的内容,发现如果我们在下面设置,send_file 可以正常工作
wsgi-disable-file-wrapper = True
但是,我不知道在哪里设置它,因为我找不到可以更新此行的 uWsgi.ini 文件。
以下是我尝试过但失败的方法,请帮助
SEND_FILE() 配置: ->>> 未运行..
output = BytesIO()
writer = pd.ExcelWriter(output, engine='xlsxwriter')
workbook = writer.book
output.seek(0)
return send_file(output,attachment_filename="testing.xlsx",as_attachment=True)
Run Code Online (Sandbox Code Playgroud)
输出错误:
return environ.get('wsgi.file_wrapper', FileWrapper)(file, buffer_size)
SystemError: <built-in function uwsgi_sendfile> returned a result with an error set
Run Code Online (Sandbox Code Playgroud)
使用响应配置:
writer = pd.ExcelWriter("abc.xlsx", engine='xlsxwriter')
return Response(writer,mimetype="text/csv",headers={"Content-disposition":"attachment; filename=myplot.csv"})
Run Code Online (Sandbox Code Playgroud)
输出错误:
Error running WSGI application
TypeError: '_XlsxWriter' object is not iterable
File "/home/hridesh1987/.virtualenvs/myproject/lib/python3.6/site-packages/werkzeug/wsgi.py", line 870, in …Run Code Online (Sandbox Code Playgroud) 我安装了mod_xsendfile,它似乎已经成功了; xsendfile.load出现在/ etc/apache2/mods-enabled中,我在运行测试脚本时没有发现任何错误.但是,每次运行它时,我都会得到一个0B文件.
这是我的测试脚本:
$file = 'sample.mp4';
$path = '/var/storage/media/'.$file;
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header("Content-type: application/octet-stream");
header("X-Sendfile: $path");
Run Code Online (Sandbox Code Playgroud)
显然我有一个存储在/var/storage/media/sample.mp4中的文件,它只有25MB,并且如果我这样做的话,它的服务非常好:
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($path));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
ob_clean();
flush();
readfile($path);
exit;
Run Code Online (Sandbox Code Playgroud)
我也在/ var/storage和/ var/www的.htaccess文件中有这个(所有这些文件都存储在/var/www/files/index.php中):
XSendFile on
XSendFileAllowAbove on
Run Code Online (Sandbox Code Playgroud)
就像我说的,我没有错误,并且PHP可以认证访问该文件,但我必须丢失一些x-sendfile配置......这让我想起,我注意到mods-enabled几乎每个mod都有.加载和.conf,但xsendfile只有.load,但其他一些,所以它与它有什么关系?
谢谢.
基本上,我想将一个标题X-Sendfile发送到浏览器发送文件,但如果X-Sendfile不可用或安装在服务器上,我不想调用它.我怎样才能在PHP中检查这个?或者,如果无法检查PHP,那么如何检查它是否已安装PERIOD?我宁愿在PHP中检查是否存在X-Sendfile,因为这样做会更容易,因为这是一个可以在其他站点和服务器上运行的包的一部分...也许如果我只是使用它与PHP header函数,如果没有安装它会返回一些东西?
多谢你们 :)
我mod_xsendfile用来使用Apache提供静态文件.但是,我无法找到一种方法让Apache Content-type根据它使用xsendfile提供的文件的扩展名来设置标头.这似乎有点奇怪,因为Apache能够在正常提供静态文件时执行此操作.有没有办法让Apache在使用文件时做同样的事情mod_xsendfile?
我使用Vhost通过apache服务我的django.conf文件如下
WSGIPythonPath /srv/www/myproject/testproject/
<VirtualHost *:80>
ServerAdmin admin@betarhombus.com
ServerName www.betarhombus.com
WSGIScriptAlias / /srv/www/testproject/testproject/testproject/wsgi.py
<Directory /srv/www/testproject/testproject/testproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static/ /srv/www/testproject/testproject/static/
Alias /media/ /srv/www/testproject/testproject/media/
<Directory /srv/www/testproject/testproject/static>
Require all granted
</Directory>
<Directory /srv/www/testproject/testproject/media>
Require all granted
</Directory>
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)
我想限制媒体文件仅在特定的登录用户上提供.所以我遇到了XsendFile.如果我正确理解它的作用是什么,当你让django对你想要服务的媒体文件进行所有检查时,它将由Apache作为静态文件提供.如果我猜对了,那么程序就是以下
然后我可以使用`并且将正常工作,就像使用初始媒体文件URL一样.我理解正确吗?我的问题如下:
关于1.激活XSendFile.这应该在我的Vhost标签内的conf文件中完成吗?设置XsendFile足够吗?我应该删除媒体指令的Alias以及媒体文件的部分吗?我希望媒体文件只能由我的视图提供?
还有什么我应该知道的吗?
编辑:我的设置是
<VirtualHost *:80>
ServerAdmin admin@betarhombus.com
ServerName www.betarhombus.com
WSGIScriptAlias / /srv/www/testproject/testproject/testproject/wsgi.py
XSendFile On
XsendFilePath /srv/www/testproject/testproject/media/
<Directory /srv/www/testproject/testproject/testproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static/ /srv/www/testproject/testproject/static/
<Directory /srv/www/testproject/testproject/static>
Require all granted
</Directory>
</VirtualHost> …Run Code Online (Sandbox Code Playgroud) x-sendfile ×10
apache2 ×3
php ×3
apache ×2
python ×2
apr ×1
capistrano ×1
cherokee ×1
content-type ×1
django ×1
download ×1
fastcgi ×1
fatal-error ×1
flask ×1
header ×1
httpd.conf ×1
mime-types ×1
python-3.x ×1
ruby ×1
symlink ×1
wordpress ×1