前文
Nginx 本身是可以替換反向代理的內(nèi)容的
使用的是 sub_filter 這個函數(shù). 但是他不能替換多個相同的內(nèi)容. 這不能滿足我們的要求
substitutions 這是一個Nginx 的第三方Model, 他可以提供加強版的sub_filter, 使用的函數(shù)是subs_filter, 可以支持正則匹配. 批量替換
安裝我的Nginx軟件包
鏈接:https://pan.baidu.com/s/1NmFd4EAgpR6D0xj3vcKE3g 密碼:a834
注意!!! 把里面的內(nèi)容解壓到root 目錄. 因為我的安裝腳本地址是寫死的
文件內(nèi)容
-rw-r--r-- 1 root root 817599 Mar 6 22:06 nginx-1.7.5.tar.gz //Nginx 安裝包
-rw-r--r-- 1 root root 5262089 Mar 6 22:06 openssl-1.0.2a.tar.gz //Nginx SSL 依賴
-rw-r--r-- 1 root root 2041593 Mar 6 22:06 pcre-8.37.tar.gz //Nginx 依賴
-rw-r--r-- 1 root root 280 Mar 6 22:06 subs_filter.filter // 替換demo
-rw-r--r-- 1 root root 112850 Mar 6 22:06 substitutions4nginx-read-only.zip //subs_filter 插件
-rw-r--r-- 1 root root 287 Mar 6 22:06 t66y.conf //一個友好的網(wǎng)站
-rw-r--r-- 1 root root 1596 Mar 6 22:06 youtube.conf //YouTube的代理. 但是沒能解決被墻問題
-rw-r--r-- 1 root root 548499 Mar 6 22:06 zlib-1.2.5.1.tar.gz //Nginx 依賴
tar -zxvf nginx-1.7.5.tar.gz
tar -zxvf pcre-8.37.tar.gz
tar -zxvf openssl-1.0.2a.tar.gz
tar -zxvf zlib-1.2.5.1.tar.gz
unzip substitutions4nginx-read-only.zip
解壓完成后. 文件夾應(yīng)該是這樣的
[root@3xhub0fo4m nginx]# pwd
/root/nginx
[root@3xhub0fo4m nginx]# ll
total 8616
drwxr-xr-x 9 1001 1001 4096 Mar 6 22:15 nginx-1.7.5
-rw-r--r-- 1 root root 817599 Mar 6 22:06 nginx-1.7.5.tar.gz
drwxr-xr-x 22 root root 4096 Mar 6 22:17 openssl-1.0.2a
-rw-r--r-- 1 root root 5262089 Mar 6 22:06 openssl-1.0.2a.tar.gz
drwxr-xr-x 7 1169 1169 4096 Apr 28 2015 pcre-8.37
-rw-r--r-- 1 root root 2041593 Mar 6 22:06 pcre-8.37.tar.gz
-rw-r--r-- 1 root root 280 Mar 6 22:06 subs_filter.filter
drwxr-xr-x 5 root root 4096 May 30 2015 substitutions4nginx-read-only
-rw-r--r-- 1 root root 112850 Mar 6 22:06 substitutions4nginx-read-only.zip
-rw-r--r-- 1 root root 287 Mar 6 22:06 t66y.conf
-rw-r--r-- 1 root root 1596 Mar 6 22:06 youtube.conf
drwxr-xr-x 13 501 games 4096 Sep 10 2011 zlib-1.2.5.1
-rw-r--r-- 1 root root 548499 Mar 6 22:06 zlib-1.2.5.1.tar.gz
安裝必要的組件
yum -y install openssl-devel pcre-devel make gcc gcc-c++ unzip wget zip subversion
32 Centos 編譯命令
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i386 -mtune=generic -fasynchronous-unwind-tables' --add-module=/root/nginx/substitutions4nginx-read-only --with-pcre=/root/nginx/pcre-8.37 --with-openssl=/root/nginx/openssl-1.0.2a --with-zlib=/root/nginx/zlib-1.2.5.1
64 Centos 編譯命令
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --add-module=/root/nginx/substitutions4nginx-read-only --with-pcre=/root/nginx/pcre-8.37 --with-openssl=/root/nginx/openssl-1.0.2a --with-zlib=/root/nginx/zlib-1.2.5.1
安裝
make
make install
安裝完畢后來寫一個簡單的Demo來試試
此時Nginx 的安裝目錄在
/etc/nginx
修改nginx.conf
添加如下代碼
server {
listen 80;
#server_name bd.totzcc.com;
location / {
proxy_pass https://www.baidu.com;
proxy_set_header Accept-Encoding "";
subs_filter '//www.baidu.com/img/bd_logo1.png' 'https://www.sogou.com/images/logo/new/search400x150.png';
}
}
效果出來啦 我把百度的圖標(biāo)緩存了搜狗的
QQ20160307-0.png