服務(wù)器
有時(shí)候我們想對響應(例如PHP接口)返回的內容做些字符串,雖然可以使用各語(yǔ)言代碼相關(guān)方法(例如PHP的str_replace)進(jìn)行替換,但是在nginx層面替換是更方便的,無(wú)需修改代碼。
約定:本文源代碼目錄統一放在:/root/soft/src 里。
安裝
安裝此模塊需要先安裝 sregex 運行庫:
$ git clone http://wap.friendlycc.com.cn/host/ $ cd sregex $ make $ make install
然后安裝 replace-filter-nginx-module 模塊:
$ cd /root/soft/src $ git clone http://wap.friendlycc.com.cn/host/ $ wget http://wap.friendlycc.com.cn/host/ $ tar zxvf nginx-1.12.2.tar.gz
這里采用靜態(tài)編譯的方式安裝模塊,需要重新編譯 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,所以千萬(wàn)不要make install,不然就真的覆蓋了。 接下來(lái)手動(dòng)替換二進(jìn)制文件:
$ 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
以上就是本文的全部?jì)热?,希望對大家的學(xué)習有所幫助,也希望大家多多支持特網(wǎng)科技。
更多關(guān)于云服務(wù)器,域名注冊,虛擬主機的問(wèn)題,請訪(fǎng)問(wèn)特網(wǎng)科技官網(wǎng):wap.friendlycc.com.cn
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站