ホームページ  >  記事  >  バックエンド開発  >  nginx は replace-filter-nginx-module を使用してコンテンツの置換を実装します

nginx は replace-filter-nginx-module を使用してコンテンツの置換を実装します

不言
不言オリジナル
2018-03-30 13:40:192561ブラウズ

この記事で共有しているのは、nginx が replace-filter-nginx-module を使用してコンテンツの置換を実装しているということです。必要な場合は参照してください。

応答 (PHP インターフェイスなど) 文字列は各言語コードに関連するメソッド (PHP の str_replace など) を使用して置換できますが、コードを変更せずに nginx レベルで置換する方が便利です。 str_replace)进行替换,但是在nginx层面替换是更方便的,无需修改代码。

约定:本文源代码目录统一放在:/root/soft/src 里。

安装

安装此模块需要先安装 sregex 运行库:

$ git clone https://github.com/agentzh/sregex
$ cd sregex
$ make
$ make install

然后安装 replace-filter-nginx-module 模块:

$ cd /root/soft/src
$ git clone https://github.com/agentzh/replace-filter-nginx-module
$ wget http://nginx.org/download/nginx-1.12.2.tar.gz
$ tar zxvf nginx-1.12.2.tar.gz

这里采用静态编译的方式安装模块,需要重新编译 nginx 。获取 nginx 上一次编译参数:

$ nginx -V
nginx version: nginx/1.12.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
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 --with-http_sub_module --with-ld-opt=-ljemalloc --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module

这里添加 replace-filter-nginx-module 模块:

--add-module=/root/soft/src/replace-filter-nginx-module

最终编译命令:

cd nginx-1.12.2

$  ./configure --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 --with-ld-opt=-ljemalloc --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --add-module=/root/soft/src/replace-filter-nginx-module
 
$ make

由于这里是升级nginx,所以千万不要make install,不然就真的覆盖了。 接下来手动替换二进制文件:

$ cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
$ cp -rfp ./objs/nginx /usr/local/nginx/sbin/

测试是否可行:

$ nginx -v
nginx version: nginx/1.12.2

配置是否正常:

$ /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

配置

这里测试的是:把接口返回的 img.test.com 换成 media.test.com

修改: /usr/local/nginx/conf/vhost/test.com.conf

location ~ [^/]\.php(/|$)
{
    # comment try_files $uri =404; to enable pathinfo
    try_files $uri =404;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi.conf;
}

为:

location ~ [^/]\.php(/|$)
{
    replace_filter 'img.test.com' 'media.test.com' g;
    replace_filter_types application/json;
    
    # comment try_files $uri =404; to enable pathinfo
    try_files $uri =404;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi.conf;

注意需要添加replace_filter_types

慣例: この記事のソース コード ディレクトリは /root/soft/src にあります。

インストール

このモジュールをインストールするには、まず sregex ランタイム ライブラリをインストールする必要があります:

$ /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

次に、replace-filter-nginx-module モジュールをインストールします:

# 升级完成第一次需要重启
$ service nginx restart 
 
# 热重启
$ /usr/local/nginx/sbin/nginx -s reload

静的コンパイルここでは が使用されています モジュールをインストールするには、nginx を再コンパイルする必要があります。 nginx を取得します 最終コンパイル パラメーター: rrreeeここに replace-filter-nginx-module を追加します モジュール:
rrreee
最終コンパイル コマンド: rrreeeここにあるためnginx をアップグレードしているため、make install を行わないように注意してください。そうしないと上書きされます。 次に、バイナリ ファイルを手動で置き換えます:
rrreee
実行可能かどうかをテストします: rrreee 設定が正常かどうか:
rrreee
設定 ここでテストされる内容は次のとおりです: img.test.comインターフェイスを返す > は <code>media.test.com に置き換えられます。

変更: /usr/local/nginx/conf/vhost/test.com.confrrreee を次のように変更します:

rrreee

replace_filter_types を追加する必要があることに注意してください。

保存後に設定が正しいかどうかを確認します: rrreeeその後、ホットリスタート:

rrreee

参考1. nginx動的追加モジュールがインストールされています

http://coolnull.com/4245.html2. LNMP による nginx のスムーズなアップグレードと ngx_lua モジュールのインストールのチュートリアル

http://www.mamicode.com/info-...
3. openresty/replace-filter-nginx-module: 応答本文のストリーミング正規表現置換🎜🎜https :// github.com/openresty/...🎜🎜4. ngx_http_substitutions_filter_module よりも強力な置換モジュール、sregex の replace-filter-nginx-module 🎜🎜https://www.cnblogs.com/archo...🎜🎜 🎜オリジナルバージョン: 🎜http://www.cnblogs.com/52yahoo/...🎜🎜🎜 関連する推奨事項: 🎜🎜🎜nginx アップロードファイルサイズを変更する方法 🎜🎜🎜🎜nginx PHP インスタンスを構成する 🎜🎜🎜🎜 PHP + Nginx 環境 Curl アクセスのローカルタイムアウト🎜🎜🎜🎜🎜

以上がnginx は replace-filter-nginx-module を使用してコンテンツの置換を実装しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。