温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Nginx怎么实现一个非套路镜像站

发布时间:2021-03-09 15:40:34 来源:亿速云 阅读:195 作者:Leah 栏目:服务器

使用Nginx怎么实现一个非套路镜像站?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

方案一

做了如下配置:

1

2

3

4

5

6

7

8

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 配置。

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

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

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

尝试改进

1

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

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 优先匹配了

1

2

3

4

location ~ .*\.(js|css)?$

{

 expires 12h;

}

方案二

添加一条

?

1

2

3

4

location ~ /book-.*?/

{

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

}

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

采坑小记

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

https://cache.yisu.com/upload/information/20210309/289/47891.jpg

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

故障:无法匹配到 css 文件

?

1

2

3

4

5

6

7

8

9

$ 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 ,那么也就是我需要对文件的后缀判断咯。 感觉自己给自己挖坑,不如直接采集得了

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

<?php

class BookAction extends Action{

 const BOOK_SAVE_DIR = "/data/book/";

 private $uri;

 private $baseUrl;

 private $book;

 private $bookname;

 public function show(){

 $book = $_GET['book'];

 $this->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怎么实现一个非套路镜像站的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

原文链接:http://mengkang.net/1152.html

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI