Nginx編譯安裝第三方模塊http_substitutions_filter_module

1. >>ngx_http_substitutions_filter_module OR HttpSubModule ?

爲了應急處理或者一些須要,有時候須要使用Nginx的反向代理某站點,並經過 HttpSubModule 和ngx_http_substitutions_filter_module 模塊替換正文內容和URL。
可是一般LNMP套件安裝的webserver並無編譯安裝nginx官方模塊HttpSubModule(官方option),而且,官方自帶的模塊 HttpSubModule 只能匹配1條規則,可是使用第三方模塊ngx_http_substitutions_filter_module 能夠匹配多條規則。 javascript

備註:
ngx_http_substitutions_filter_module 是指第三方nginx模塊 substitutions4nginx (原:Google Code 現:github
HttpSubModule 是指Nginx官方的 with-http_sub_module模塊(option) css

Nginx自身帶的module並很少,這也是它爲何性能好,系統開銷較小的緣由之一,相比apache,它不能動態的加載module,若是以前編譯安裝了Nginx,這時候就須要從新編譯nginx添加模塊,並替換掉原先的nginx執行文件。 html

2. 1.下載須要的文件

substitutions4nginx github下載 java

  
  1. # 下載第三方模塊
  2. # cd ~
  3. # git clone git://github.com/yaoweibin/ngx_http_substitutions_filter_module.git

3. 2.查看以前Nginx編譯configure

  
  1. # nginx -V
  2. nginx version: nginx/1.2.7
  3. built by gcc 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
  4. TLS SNI support enabled
  5. configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with- http_ssl_module --with-http_gzip_static_module --with-ipv6

由於Nginx編譯安裝第三方模塊的時候須要添加上以前編譯的configure參數,而後從新設置configure編譯(可是不覆蓋安裝,只make不install): nginx

  
  1. ./configure --prefix=/你的安裝目錄 --add-module=/第三方模塊目錄

4. 3.從新編譯Nginx

  
  1. # 打開Nginx編譯目錄,版本號可能不一樣
  2. # cd ~/lnmp1.0-full/nginx-1.2.7
  3. # 從新configure
  4. # ./configure --prefix= --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --add-module=/root/ngx_http_substitutions_filter_module
  5. # make

備註:從新編譯的時候,記得必定要把之前編譯過的模塊一同加到configure參數裏面. git

5. 4.覆蓋原nginx文件

  
  1. # /etc/init.d/nginx stop
  2. # cd cd objs/
  3. # 覆蓋原文件
  4. # cp nginx /usr/local/nginx/sbin/
  5. # /etc/init.d/nginx start

6. 5.簡單配置

7. ① HttpSubModule 的 官方文檔 說的很清楚,這裏就不寫實例了,而且功能沒有substitutions4nginx的強大。

7.1. 在頭部引入指定JS

  
  1. location / {
  2. sub_filter </head>
  3. '</head><script language="javascript" src="$script"></script>';
  4. sub_filter_types text/html;
  5. sub_filter_once on;
  6. }
  • sub_filter 一行代碼前面是須要替換的內容,後面單引號內是替換成的內容。
  • sub_filter_once 意思是隻查找並替換一次。on是開啓此功能,off是關閉——默認值是on。
  • sub_filter_types 一行意思是選定查找替換文件類型爲文本型。也能夠不加此行,由於默認只查找text/html文件。
  • sub_filter模塊能夠用在http, server, location模塊中。主要做用就是查找替換文件字符。

8. ② substitutions4nginx

8.1. subs_filter

  
  1. 實例:
  2. location / {
  3. subs_filter_types text/html text/css text/xml;
  4. subs_filter st(\d*).example.com $1.example.com ir;
  5. subs_filter a.example.com s.example.com;
  6. }

g(default):替換全部匹配的字符串。
i: 執行不區分大小寫的匹配。
o: 只需將第一個。
r:該模式是做爲一個正則表達式處理,默認是固定的字符串。 github

8.2. subs_filter_types

  
  1. syntax: subs_filter_types mime-type [mime-types]
  2. default: subs_filter_types text/html
  3. context: http, server, location
  4. subs_filter ‘<(no?script.*?)>(.*?)<(\/no?script.*?)>’ gi; //替換掉所有的<noscript></noscript>
  5. subs_filter ‘<(s?cript.*?)>(?:\s|\S)*?<(\/s?cript.*?)>’ gi; //替換掉所有的<script>包換中間換行</script>
  6. subs_filter ‘<(i?frame.*?)>(.*?)<(\/i?frame.*?)>’ gi; //替換<iframe></iframe>

9. >>參考資料<<

ngx_http_sub_module web

substitutions4nginx 正則表達式

Nginx第三方模塊 apache

相關文章
相關標籤/搜索