关于建站主机的坑(二)
钢翼
编程
由于服务器上的网站有大量图片和pdf,1m小水管实在是顶不住。所以把ftp自动同步工具搞定了。
https://github.com/wmz46/filesync
然后通过nginx重定向到建站主机上,这下应该可以了吧,结果发现pdf.js加载的pdf出现跨域问题。
建站主机又无法配置跨域。最简方案只能是我做一个php页面,专门读取本地文件,然后加上跨域返回头了
在建站主机上传file.php。内容如下
<?php
$file = $_REQUEST["file"];
if(file_exists($file)){
echo "文件不存在";
}else{
//获取当前的物理路径进行拼接,php拼接字符串用.号
$file = dirname(__FILE__).$file;
//获取文件后缀,不带点
$ext = substr(strrchr($file, '.'), 1);
//允许跨域
header("Access-Control-Allow-Origin: *");
//比对字符串用strcmp
if(strcmp($ext, "pdf") === 0){
header('Content-Type:application/pdf');
echo file_get_contents($file);
}else if(strcmp($ext, "png") === 0){
header('Content-type:image/png');
echo file_get_contents($file);
}else if(strcmp($ext, "gif") === 0){
header('Content-type:image/gif');
echo file_get_contents($file);
}else if(strcmp($ext, "jpg") === 0){
header('Content-type:image/jpeg');
echo file_get_contents($file);
}else{
//只支持常见图片格式和pdf
echo "不支持的文件类型";
}
}
?>
然后把nginx配置上重定向,重启nginx搞定
location /upload {
rewrite ^/upload/(.*)$ http://yourdomain.com/file.php?file=/upload/$1 redirect;
}