Matching a href spec with Lua regex

Hi,

I am trying to modify the response body in the following way :

If there href=“http://www.google.com”, I will convert it to
href="http://nginx-ip/?_url_={url-encoded-form-of www.google.com}

This is what I have in my nginx.conf

location / {


body_filter_by_lua '
    local escUri = function (m)
        return "href=\\"http://10.0.9.44/?_url_=" ..

ngx.escape_uri(“$1”) … “\”"
end

    local newStr, n, err = ngx.re.gsub(ngx.arg[1], "href=\\"(.-)\\",

escUri, “i”)
';
}

But I cannot see absolutely any change in the href part of the response

Can someone help me understand why this is not matching ? What am I
doing
wrong ?

-Vamshi

Posted at Nginx Forum:

As usual, found my error.

The following is properly matcing the regex :

            local escUri = function (m)
                local _str = "href=\\"http://10.0.9.44/?_redir_="
                _str = _str .. ngx.escape_uri(m[1]) .. "\\""
                return _str
            end

            local newStr, n, err = ngx.re.gsub(ngx.arg[1],

“href=\”(.*)\“”, escUri, “ijox”)
print(ngx.arg[1]) → still original text

From my debug logs, I can see that the regex susbstitution is happening
properly. But the href still remains the same. Probably because of
chunked
data.

I will try to aggregate the response body and then do the substitution.
But
if someone sees anything wrong, your help would be appreciated.

-Vamshi

Posted at Nginx Forum:

Sorry for the double post, but wanted to post the complete conf … just
in
case there was a mistake

server {

    listen       80;
    server_name  127.0.0.1 10.0.9.44;

    set $_ActualTarget "";

    location / {

        rewrite_by_lua '
            local _args = ngx.req.get_uri_args()
            ngx.var._ActualTarget = _args["_url_"];
            _args["_url_"] = nil
            print(ngx.var._ActualTarget)
            ngx.req.set_uri_args(_args);
        ';

        resolver 8.8.8.8;
        proxy_pass_request_body on;
        proxy_pass_request_headers on;
        proxy_pass $scheme://$_ActualTarget;

        header_filter_by_lua '
            ngx.header.content_length = nil
            ngx.header.set_cookie = nil

            if ngx.header.location then
                local _location = ngx.header.location
                _location = ngx.escape_uri(_location)
                _location = "http://10.0.9.44/?_url_=" .. _location
                ngx.header.location = _location
            end
        ';

        body_filter_by_lua '
            local escUri = function (m)
                local _str = "href=\\"http://10.0.9.44/?_url_="
                _str = _str .. ngx.escape_uri(m[1]) .. "\\""
                return _str
            end

            local newStr, n, err = ngx.re.gsub(ngx.arg[1],

“href=\”(.*)\“”, escUri, “ijox”)
print(ngx.arg[1])
';
}

    #
    # redirect server error pages to the static page /50x.html
    #

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /var/www/html;
    }
}

Posted at Nginx Forum: