sub_filter 指令的使用

配置:

location / {
       sub_filter 'https://static.xxxx.hk/partner/assets' '/build';
       sub_filter_once off;
       sub_filter_types *;
       proxy_set_header Accept-Encoding "";
       proxy_redirect off;
       proxy_set_header Host $host;
       proxy_set_header X-Forwarded-Host $host;
       proxy_set_header X-Forwarded-Server $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       include vhost/ipdb;
       proxy_buffering on;
       proxy_pass http://192.168.8.100:3343;
}
  1. sub_filter
    • 将响应内容中的字符串 https://static.sonkwo.hk/partner/assets 替换为 /build
    • 场景:修改后端返回的 HTML/CSS/JS 中的静态资源路径,使其指向代理服务器本地路径或其他地址。
  2. sub_filter_once off
    • 默认情况下,sub_filter 只替换第一次匹配的内容。设为 off 会替换所有匹配项。
  3. sub_filter_types \*
    • 默认仅替换 text/html 类型的内容。通过 * 允许对所有类型的响应(如 CSS/JS/JSON)进行替换。
  4. proxy_set_header Accept-Encoding ""
    • 禁止客户端使用压缩(如 gzip),因为压缩后的内容无法直接替换字符串。

用途:

  1. 静态资源路径重写
  1. CDN/代理层适配
  1. 调试或本地开发

limit_except 限制某些请求访问

配置:

location / {
    limit_except GET HEAD OPTIONS{
        deny all;
    }
    proxy_redirect     off;
    proxy_set_header   Host $host;
    proxy_set_header   X-Forwarded-Host $host;
    proxy_set_header   X-Forwarded-Server $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    include vhost/ipdb;
    proxy_buffering    on;
    proxy_pass         http://xxxx;
}

用途:

  1. limit_except 是 Nginx 的 ngx_http_access_module 模块的一部分,用于对除指定 HTTP 方法外的其他方法应用访问控制规则;
  2. 上述配置结果:只有 GET、HEAD 和 OPTIONS 请求可以通过,其他方法的请求会返回 403 Forbidden 错误;
🍺转载文章请注明出处,谢谢!🍺