nginx html 替换,Nginx 服务内容替换功能(sub模块)
Nginx 服務提供修改返回客戶端內容的功能;也就是,當Nginx服務返回給瀏覽器內容時,可以修改返回的內容。
一、with-http_sub_module模塊
1.1、sub_filter string replacement;
Default: —
Context: http, server, location
允許使用一些其他的文本替換nginx應答內容中的一些文本。
匹配不區分大小寫,替換的文本可以包含變量。
當且僅當sub_filter在當前級別上沒有定義指令時,這些指令將從上一級繼承。
1.2、sub_filter_once on | off;
Default: on;
Context:http, server, location
是否查找每個字符串以替換一次或重復。
1.3、sub_filter_types mime-type …;
Default:sub_filter_types text/html;
Context: http, server, location
指定sub_filter指令將檢查那些內容類型,默認為text/html
二、ngx_http_substitutions_filter_module 增強功能
模塊substitutions4nginx
支持多次替換
支持正則替換
支持中文替換
不能使用到 if 判斷模塊內
2.1、subs_filter_types
語法: subs_filter_types mime-type [mime-types]
默認: subs_filter_types text/html
適用: http, server, location
用來指定替換文件類型的 默認僅僅替換text/html類型的文件。
2.2、subs_filter
語法: subs_filter source_str destination_str [gior]
默認: none
適用: http,server,location
用來替換文本的,可以使用正則
g:替換匹配項。
i:區分大小寫的匹配。
o:只匹配發現的第一個。
r:正則匹配。
2.3、subs_filter_once on | off;
Default:subs_filter_once on;
Context: http, server, location
on:只替換第一個匹配項
off:允許搜索并替換所有匹配行
三、使用示例
3.1、使用示例
location / {
subs_filter_types text/html text/css text/xml;
subs_filter st(\d*).example.com $1.example.com ir;
subs_filter a.example.com s.example.com;
subs_filter http://$host https://$host;
3.2、使用案例
1、將頁面中的http鏈接全部替換為https
location / {
subs_filter http:// https:// gr;
2、域名替換
location / {
subs_filter https://domain1.net/([^"\']*?)\.(jpg|png|js|css|jpeg|bmp) https://domain2.com/$1.$2 igr;
3、在使用正則模式時,不能使用nginx內置變量,比如:$host,否則會出現如下報錯:
nginx: [emerg] match part cannot contain variable during regex mode in ***
總結
以上是生活随笔為你收集整理的nginx html 替换,Nginx 服务内容替换功能(sub模块)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为蒙牛合作打造全场景健康生态圈 进一步
- 下一篇: Centos-Server-Nginx服