Nginx访问body_filter_by_lua_block时返回404

以下是Nginx配置

server {
  listen 80;

  location ~ \.(jpe?g|png|gif|ico|svg)$ {
      add_header Cache-Control "public,max-age=15552000";

      root /usr/share/nginx/html;
      index index.html index.htm;
      try_files $uri $uri/ /index.html =404;
  }

  location / {
      root /usr/share/nginx/html;
      index index.html index.htm;
      try_files $uri $uri/ /index.html =404;

      if ( $request_uri ~ '^/xxx/yyyy' ) {
           set $test  A;
      }
      if ( $arg_refId ) {
          set $test  "${test}B";
      }
      if ($test = AB) {
          header_filter_by_lua_block { ngx.header.content_length = nil }
          body_filter_by_lua_block
          {
              local ogurl = "ggggggggg"
              local ogtitle = "sdfsdfsdfsdfsdf"
              local environ = "${REACT_APP_ENVIRONMENT}"
              if environ == "development" then
                  ogurl = "ssssssss"
              end
              local body = ngx.arg[1]
              ngx.log(ngx.STDERR, body)
              if body  then
                local query_string = ngx.req.get_uri_args()
                local refid = query_string["refId"]
                body = ngx.re.gsub(body, "/ogimage.png", ogurl .. ngx.escape_uri(refid))
                body = ngx.re.gsub(body, "Swop.fi", ogtitle)
              end
              ngx.arg[1] = body
          }
      }
  }
  include /etc/nginx/extra-conf.d/*.conf;
}

当我尝试请求任何url,如/zzzz或/kkk时都没问题。但当我尝试访问url/xxx/yyyy?refId=1111时,我收到了404错误。为什么try_file指令与Lua不配合工作呢?我做错了什么?

原文链接 https://stackoverflow.com/questions/70280544

点赞