nginx配置ngx_http_sub_module

1 Introduction

The ngx_http_sub_module nginx module, may be used to modify the contents of the string in response to the site, such as filtration sensitive words. Third-party modules ngx_http_substitutions_filter_module, to make up for the lack of ngx_http_sub_module, you can replace the use of regular expressions.

2.安装ngx_http_sub_module

  • Check whether the installation module to change
    with the nginx -Vview, if it contains http_sub_module support, or not support.
    Here Insert Picture Description
    FIG is supported, it can be used directly, if the installation does not need to be recompiled Nginx support, translation parameters module coupled to -with-http_sub_module

3. Instructions

subs_filter_types

  subs_filter_types

    语法: subs_filter_types mime-type [mime-types]

    默认:subs_filter_types text/html (高版本的nginx如果有写着条,那么重启服务的时候会有警告信息,因为默认就是检查 text/html的信息,与程序有重复配置)

    适用区域:http, server, location

    subs_filter_types是用来指令需要替换的文件类型,默认是text/html类型。* 表示所有类型。

    此模块无法处理经过压缩的内容,虽然能与gzip filter模块兼容,但无法处理反向代理返回的内容。当需要处理反向代理的内容时,可以使用如下语句禁用压缩:

   proxy_set_header Accept-Encoding '';

subs_filter

 subs_filter

    语法;subs_filter 源字段串 目标字段串 [gior]

    默认:无

    适用区域:http, server, location

    subs_filter指令允许在nginx响应输出内容时替换源字段串(正则或固定)为目标字符串。第三个标志含意如下:

    g(默认): 替换所有匹配的字段串。(默认可省略)

    i: 执行区分大小写的匹配。

    o: 仅替换首个匹配字符串。

    r: 使用正则替换模式,默认是固定模式。


4. Example Configuration

The main purposes of this example:

  1. Replace the specified phone number for our phone number request
  2. All requests in the address of the page are replaced with a request to go Nginx address (HTML, there are many resources to address file, AJAVX request address must be replaced with the address of the current NGINX's go)
    Here Insert Picture Description

Note:
The above configuration means that the http: // IP: 9090 / travel / hotel / getReview / xxxx forward to http://m.elong.com/hotel/XXXX
URI will replace the location of the matching portion of the URI

5. Hang

  • Disable compression (phenomenon: web return data is not replaced, CURL returned successfully replace)
    all the time Nginx Anti-generation common problem is sub_filter invalid. Because the browser is allowed compression, the request is the first band Accept-Encoding: gzip's. And the Nginx sub_filter can not handle compressed request, Nginx itself will not be unpacked. In fact, in order to write a decompression plug-in it is not possible, because there is no input filtering Nginx relevant interface.
    General online solutions are proxy_set_header Accept-Encoding ""; upstream disable compression, the compression is not affected client side.
  • By default only replace HTML, you want to replace the use of all types *

6. References

Published 418 original articles · won praise 745 · Views 1.26 million +

Guess you like

Origin blog.csdn.net/u013467442/article/details/90680305
Recommended