Linux入门教程:使用nginx replace-filter-nginx-module模块替换网页中的http链接为https,replacefilter现在想启用https
首先不知道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
总结:
通过这样简单的配置,我们就成功实现了将响应中的内容替换了,还是蛮方便的!