1. 程式人生 > 實用技巧 >nginx編譯安裝WEB站點內容過濾功能模組(with-http_sub_module)

nginx編譯安裝WEB站點內容過濾功能模組(with-http_sub_module)

最近有個需求:需要過濾替換掉網站上部分內容,查了下資料NGINX自帶過濾功能模組,於是實踐了下,具體操作如下:
雖然是NGINX自帶了with-http_sub_module模組,但是需要編譯安裝NGINX,並指定選項才可以正常使用。
需要編譯安裝NGINX(with-http_sub_module):
nginx編譯安裝WEB站點內容過濾功能模組(with-http_sub_module)

./configure \
--prefix=/application/nginx-1.6.3 \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--without-http_gzip_module \

--with-http_sub_module

譯安裝NGINX:(ngx_http_substitutions_filter_module)
此模組需要先單獨下載
下載地址:git clone git://github.com/yaoweibin/ngx_http_substitutions_filter_module.git
nginx編譯安裝WEB站點內容過濾功能模組(with-http_sub_module)

./configure \
--prefix=/application/nginx-1.6.3 \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--without-http_gzip_module \

--with-http_sub_module \
--add-module=/application/ngx_http_substitutions_filter_module #指定模組路徑

測試過程:建立測試站
配置檔案如下:
[[email protected] conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

sub_filter nginx apache;
server {
listen 80;
server_name www.nginx.com;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

nginx編譯安裝WEB站點內容過濾功能模組(with-http_sub_module)
多站點測試:建立vhosts目錄(多站點配置incule vhosts/*匯入)

Bbs.conf :
server {
listen 80;
server_name bbs.nginx.com;
location / {
root /data/wwwroot/bbs;
index index.html index.htm;
}
}
Blogs.conf:
server {
listen 80;
server_name blogs.nginx.com;
location / {
root /data/wwwroot/blogs;
index index.html index.htm;
}
}

nginx編譯安裝WEB站點內容過濾功能模組(with-http_sub_module)
[[email protected] conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
subs_filter nginx apache;
subs_filter If apache;
subs_filter com cn;
subs_filter_types ;
include vhosts/
;
server {
listen 80;
server_name www.nginx.com;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
nginx編譯安裝WEB站點內容過濾功能模組(with-http_sub_module)

訪問測試效果:
nginx編譯安裝WEB站點內容過濾功能模組(with-http_sub_module)

測試結果:1、sub_filter只支援單行,功能有限(加多行會報錯)。
2、subs_filter支援多行過慮,且支援正則,功能較強大。

轉載於:https://blog.51cto.com/jdonghong/2331906