网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

怎么在nginx中利用replace-filter-nginx-module替换内容

本篇文章为大家展示了怎么在nginx中利用replace-filter-nginx-module替换内容,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

创新互联成立于2013年,我们提供高端重庆网站建设公司网站制作成都网站设计公司、网站定制、营销型网站建设小程序设计、微信公众号开发、网站推广服务,提供专业营销思路、内容策划、视觉设计、程序开发来完成项目落地,为高空作业车租赁企业提供源源不断的流量和订单咨询。

安装

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

                    1

                    2

                    3

                    4

$ git clone https://github.com/agentzh/sregex

$ cd sregex

$ make

$ make install

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

                    1

                    2

                    3

                    4

$ 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 上一次编译参数:

                    1

                    2

                    3

                    4

                    5

                    6

$ 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 模块:

                    1

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

    最终编译命令:

                    1

                    2

                    3

                    4

                    5

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,不然就真的覆盖了。 接下来手动替换二进制文件:

                    1

                    2

$ cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

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

    测试是否可行:

                    1

                    2

$ nginx -v

nginx version: nginx/1.12.2

    配置是否正常:

                    1

                    2

                    3

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

                    1

                    2

                    3

                    4

                    5

                    6

                    7

                    8

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;

}

    为:

                    1

                    2

                    3

                    4

                    5

                    6

                    7

                    8

                    9

                    10

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:

                    1

                    2

                    3

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

    然后热重启:

                    1

                    2

                    3

                    4

                    5

# 升级完成第一次需要重启

$ service nginx restart

 

# 热重启

$ /usr/local/nginx/sbin/nginx -s reload

上述内容就是怎么在nginx中利用replace-filter-nginx-module替换内容,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。

原文链接:https://segmentfault.com/a/1190000014077313


网站名称:怎么在nginx中利用replace-filter-nginx-module替换内容
地址分享:http://bjjierui.cn/article/ghioej.html

其他资讯