RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
Nginx如何实现非套路镜像站

这篇文章主要介绍Nginx如何实现非套路镜像站,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

丰城网站制作公司哪家好,找成都创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站设计等网站项目制作,到程序开发,运营维护。成都创新互联于2013年创立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联

前几天发现一个电子书非常棒,但是是 github 上的,总是打不开,而正好我的服务器是在香港的,所以我想做一个镜像。

方案一

做了如下配置:

location ^~ /book-c/
{
 proxy_pass http://akaedu.github.io/book/;
 proxy_redirect off;
 proxy_http_version 1.1;
 proxy_set_header Upgrade $http_upgrade;
 proxy_set_header Connection "upgrade";
}

浏览了下,都 ok,但是有几点不太好

  • 如果以后我发现类似的书很好,都要增加一个 nginx 配置。

  • 如果原始网站完全无法访问了,我这边也挂了,不能缓存到我本地服务器。

  • 我想修改网页内容也不太好操作,比如我想加上原作者的版权和原始访问地址说明等。

完全采集过来,我也懒得写脚本去跑,最终走上了下面这段踩坑路。

尝试改进

所以尝试了如下做法

rewrite ^/book-(.*?)/  /index.php?m=Book&a=show&book=$1 last;
class BookAction extends Action{
 private $uri;
 public function show(){
 $book = $_GET['book'];
 if (!method_exists($this,$book)){
  $this->error404();
 }
 try{
  $this->$book();
 }catch (Exception $e){
  $this->error404();
 }
 }

 /**
 * http://akaedu.github.io/book/
 */
 private function c(){
 $baseUrl = "http://akaedu.github.io/book/";
 $url = $baseUrl.$this->uri;
 echo file_get_contents($url);
 }
}

又遇到了一个问题,当我访问 https://mengkang.net/book-c/styles.css 则无法 rewrite 匹配到了。

原因是 nginx 优先匹配了

location ~ .*\.(js|css)?$
{
 expires 12h;
}

正则匹配优先级关系:https://www.jb51.net/article/134233.htm

方案二

添加一条

location ~ /book-.*?/
{
 rewrite ^/book-(.*?)/ /index.php?m=Book&a=show&book=$1 last;
}

location ^~ 不支持正则的,所以没法用

采坑小记

如果是使用的 location ~ /book-.*/ ,根据正则就是贪婪模式,那么

/upload/otherpica43/67199.png

匹配到的就是 /book-c/images/ ,也就是说rewrite里面的 $1 就是 c/images ,这样和我们的预期相悖的。

故障:无法匹配到 css 文件

$ wget -S https://mengkang.net/book-c/styles.css -O /dev/null
--2018-02-01 13:13:36-- https://mengkang.net/book-c/styles.css
Resolving mengkang.net... 203.195.188.207
Connecting to mengkang.net|203.195.188.207|:443... connected.
HTTP request sent, awaiting response...
 HTTP/1.1 200 OK
 Server: nginx
 Date: Thu, 01 Feb 2018 05:13:38 GMT
 Content-Type: text/html; charset=UTF-8

所有内容的输出默认都是 text/html ,那么也就是我需要对文件的后缀判断咯。 感觉自己给自己挖坑,不如直接采集得了

book = $book;
 $this->uri = str_replace("/book-{$book}/","",$_SERVER['REQUEST_URI']);
 if (!method_exists($this,$book)){
  $this->error404();
 }
 try{
  $this->$book();
 }catch (Exception $e){
  $this->error404();
 }
 }
 /**
 * http://akaedu.github.io/book/
 */
 private function c(){
 $this->baseUrl = "http://akaedu.github.io/book/";
 $url = $this->baseUrl.$this->uri;
 $this->output($url);
 }
 private function output($url){
 $ext = pathinfo($url,PATHINFO_EXTENSION);
 if (!$ext) {
  $url = $url."/index.html";
  $ext = "html";
 }
 switch ($ext){
  case "css":
  header("Content-Type: text/css; charset=UTF-8");
  break;
  default:
  header("Content-Type: text/html; charset=UTF-8");
  break;
 }
 // 如果已经缓存
 $filename = self::BOOK_SAVE_DIR.$this->book."/".str_replace($this->baseUrl,"",$url);
 if (file_exists($filename)){
  $data = file_get_contents($filename);
 }else{
  $data = file_get_contents($url);
  $dir = dirname($filename);
  if (!file_exists($dir)){
  mkdir($dir,755,true);
  }
  file_put_contents($filename,$data);
 }
 // 增加原始版权说明
 echo $data;
 }
}

以上是“Nginx如何实现非套路镜像站”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!


当前题目:Nginx如何实现非套路镜像站
转载来源:http://sczitong.cn/article/iieied.html