openresty 代替php,openresty替换响应内容中http为https–replace-filter-nginx-module的使用...

首先不知道openresty为何物的,可自行google!

问题:我们有一个网站,开始用的是http协议的,由于历史原因,路径写在各个地方,比较混乱。现在想启用https协议,可是现在的浏览器不充计https协议下加载http协议的样式,脚本等。

方案1: 将代码中的http全部修改成https,工作量比较大,并且风险太高

方案2: 在程序的输出到时,统一替换,这个虽然相对不错,不过感觉还是有点麻烦,把不属于业务的功能放到了业务代码上

方案3:   在web服务器上做统一替换,这也是我们最终决定的方案,我们选用openresty来做统一替换功能.

安装:

cd /opt/

yum install readline-devel pcre-devel openssl-devel gcc

wget https://openresty.org/download/ngx_openresty-1.9.7.1.tar.gz

git clone https://github.com/openresty/sregex.git

git clone https://github.com/openresty/replace-filter-nginx-module.git

cd sregex

make

make install

cd ..

tar -zxvf ngx_openresty-1.9.7.1.tar.gz

cd ngx_openresty-1.9.7.1

./configure --with-luajit --add-module=/opt/replace-filter-nginx-module/ --with-debug

gmake

gmake install

这样openresty就算安装完毕,默认安装在/usr/local/openresty。

配置如下:

mkdir /usr/local/openresty/test

cd /usr/local/openresty/test

mkdir logs/ conf/

//在conf目录下创建一个文本文件作为配置文件,命名为nginx.conf

worker_processes 1;

error_log logs/error.log debug;

events {

worker_connections 1024;

}

http {

server {

root /alidata/www/test;

index index.php;

charset utf-8;

server_name test.xxx.com;

listen 80;

rewrite ^/(.*) https://$server_name/$1 permanent; #跳转到Https

}

server {

root /alidata/www/test;

index index.php;

charset utf-8;

server_name test.xxx.com;

listen 443;

replace_filter 'http://test.xxx.com' 'https://test.xxx.com' 'ig';

replace_filter 'http://(\d+).gravatar.com' 'https://$1.gravatar.com' 'ig';

}

}

写一shell来start,stop,reload

start.sh

#!/bin/bash

/usr/local/openresty/nginx/sbin/nginx -p /usr/local/openresty/test -c conf/nginx.conf

stop.sh

#!/bin/bash

/usr/local/openresty/nginx/sbin/nginx -p /usr/local/openresty/test -c conf/nginx.conf -s stop

reload.sh

#!/bin/bash

/usr/local/openresty/nginx/sbin/nginx -p /usr/local/openresty/test -c conf/nginx.conf -s stop

总结:

通过这样简单的配置,我们就成功实现了将响应中的内容替换了,还是蛮方便的!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值