Nginx 中的 ngx_http_sub_module 模块是一个过滤器,它修改网站响应内容中的字符串,比如你想把响应内容中的 ‘haicoder’ 全部替换成 ‘HaiCoder‘。
这个模块已经内置在 nginx 中,但是默认未安装,需要安装需要加上配置参数 –with-http_sub_module。
sub_filter string replacement;
http,server,location
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
sub_filter 'nginx' 'haicoder';
}
}
我们直接在 server 下面的 location 里面配置 sub_filter 即可,这里,我们配置了将所有的 nginx 字符串替换为了 haicoder 字符串。
这里,默认只替换一次,如果需要全部替换,则需要使用下面的配置。
sub_filter_once on|off;
sub_filter_once on;
http,server,location
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
sub_filter 'nginx' 'haicoder';
sub_filter_once off;
}
}
将 sub_filter_once 配置设置为 off,则会进行字符串的全部替换。
我们首先使用 vim 打开 nginx 的默认配置路径,具体命令如下:
vim /etc/nginx/conf.d/default.conf
如下图所示:
我们执行如上命令,打开配置文件,接着,我们修改 server 下面的根路径的 location 配置,具体配置如下:
location / { root /usr/share/nginx/html; index index.html index.htm; sub_filter 'nginx' 'haicoder'; sub_filter_once off;}
配置完毕后,如下图所示:
现在,我们重新加载配置文件,具体命令如下:
nginx -s reload
现在,我们输入路由,访问配置好的路由,此时,浏览器输出如下:
我们看到,这次,网页中的所有的 nginx 都被替换为了 haicoder。
Nginx 中的 ngx_http_sub_module 模块是一个过滤器,它修改网站响应内容中的字符串,比如你想把响应内容中的 ‘haicoder’ 全部替换成 ‘HaiCoder‘。
这个模块已经内置在 nginx 中,但是默认未安装,需要安装需要加上配置参数 –with-http_sub_module。