欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

nginx使用replace-filter-nginx-module實現(xiàn)內(nèi)容替換的示例

 更新時間:2018年03月30日 10:43:15   作者:飛鴻影  
本篇文章主要介紹了nginx使用replace-filter-nginx-module實現(xiàn)內(nèi)容替換的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

有時候我們想對響應(yīng)(例如PHP接口)返回的內(nèi)容做些字符串,雖然可以使用各語言代碼相關(guān)方法(例如PHP的str_replace)進行替換,但是在nginx層面替換是更方便的,無需修改代碼。

約定:本文源代碼目錄統(tǒng)一放在:/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

這里采用靜態(tài)編譯的方式安裝模塊,需要重新編譯 nginx 。獲取 nginx 上一次編譯參數(shù):

$ 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 。

保存后檢查配置是否ok:

$ /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

然后熱重啟:

# 升級完成第一次需要重啟
$ service nginx restart 
 
# 熱重啟
$ /usr/local/nginx/sbin/nginx -s reload

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Prometheus監(jiān)控實戰(zhàn)篇Nginx、Hbase操作詳解

    Prometheus監(jiān)控實戰(zhàn)篇Nginx、Hbase操作詳解

    這篇文章主要介紹了Prometheus監(jiān)控實戰(zhàn)篇Nginx、Hbase,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-02-02
  • nginx提示502 頁面的解決方法

    nginx提示502 頁面的解決方法

    如果部分PHP程序的執(zhí)行時間超過了Nginx的等待時間,可以適當(dāng)增加nginx.conf配置文件中FastCGI的timeout時間
    2013-02-02
  • Nginx開啟Brotli壓縮算法實現(xiàn)過程詳解

    Nginx開啟Brotli壓縮算法實現(xiàn)過程詳解

    這篇文章主要介紹了Nginx開啟Brotli壓縮算法實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • Nginx開源可視化配置工具NginxConfig使用教程

    Nginx開源可視化配置工具NginxConfig使用教程

    這篇文章主要為大家介紹了Nginx開源可視化配置工具NginxConfig使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • nginx反向代理二級域名綁定方法及注意事項

    nginx反向代理二級域名綁定方法及注意事項

    本文介紹了利用nginx實現(xiàn)多域名和多站點的綁定的方法及相關(guān)注意事項,需要的朋友可以參考下
    2018-04-04
  • Nginx 502 Bad Gateway的原因及解決方法

    Nginx 502 Bad Gateway的原因及解決方法

    這篇文章主要給大家介紹了Nginx 502 Bad Gateway 錯誤的原因及解決方法,在php服務(wù)當(dāng)中,有兩個參數(shù)非常的重要:max_requestst和max_children,具體的原因必須要查看日志才可以弄明白,接下就和小編一起來看看具體原因及解決方法吧
    2023-08-08
  • Nginx訪問php文件直接下載的解決方法

    Nginx訪問php文件直接下載的解決方法

    本文主要給大家介紹了如何解決Nginx訪問php文件直接下載,這種情況通常是因為nginx沒有將PHP文件交給PHP解釋器處理,文中通過代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • nginx日志導(dǎo)入elasticsearch的方法示例

    nginx日志導(dǎo)入elasticsearch的方法示例

    這篇文章主要介紹了nginx日志導(dǎo)入elasticsearch的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • 使用nginx+tomcat+keepalived實現(xiàn)高可用的詳細(xì)步驟

    使用nginx+tomcat+keepalived實現(xiàn)高可用的詳細(xì)步驟

    這篇文章主要介紹了nginx+tomcat+keepalived實現(xiàn)高可用,包括安裝nginx服務(wù)的步驟,詳細(xì)介紹了安裝keepalived的方法,對nginx+tomcat+keepalived高可用相關(guān)知識感興趣的朋友一起看看吧
    2022-03-03
  • Nginx反向代理之proxy_redirect指令的實現(xiàn)

    Nginx反向代理之proxy_redirect指令的實現(xiàn)

    proxy_redirect指令是用來重置頭信息中的"Location"和"Refresh"的值,本文就來詳細(xì)的介紹一下如何使用,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08

最新評論