国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > Nginx > 正文

Nginx實現非套路鏡像站的踩坑記錄

2024-08-30 12:29:23
字體:
來源:轉載
供稿:網友

緣起

前幾天發現一個電子書非常棒,但是是 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;}

正則匹配優先級關系:http://www.survivalescaperooms.com/article/134233.htm

方案二

添加一條

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

location ^~ 不支持正則的,所以沒法用

采坑小記

如果是使用的 location ~ /book-.*/ ,根據正則就是貪婪模式,那么

https://mengkang.net/book-c/images/sortsearch.theta.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.cssResolving mengkang.net... 203.195.188.207Connecting 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 ,那么也就是我需要對文件的后綴判斷咯。 感覺自己給自己挖坑,不如直接采集得了

<?phpclass 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; }}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 静宁县| 祁门县| 仁布县| 永川市| 基隆市| 鹿邑县| 皮山县| 葫芦岛市| 金昌市| 修水县| 德钦县| 疏勒县| 林周县| 西乌珠穆沁旗| 大同县| 镇安县| 林州市| 海淀区| 吉木萨尔县| 嫩江县| 府谷县| 宝山区| 宁安市| 新建县| 湘潭县| 三台县| 斗六市| 城固县| 桂东县| 西丰县| 嘉定区| 华池县| 资源县| 洛阳市| 宁城县| 天祝| 安西县| 清水县| 军事| 普安县| 莱州市|