I am having trouble matching a specific word in nginx $request_body variable.
I want to proxy pass if the body request has a special word in it,
So my approach is this:
location ~ \.php$ {
if ($request_body ~* (.*)) {
proxy_pass http://test.proxy;
break;
}
# other case...
}
This matches everything and the if statement works,
but if I change the regexp in any way, I can’t get a hit.
So my question now is:
How do i need to define the regexp in nginx correctly to match against, “target” for example?
Thanks in advance!
Your code has a number of problems.
“($request_body ~* (.*))” never matches anything as stated by someone else and so the the “other case” is always the outcome
More importantly, it uses “proxy_pass” with “if” which is classically evil. http://wiki.nginx.org/IfIsEvil.
To get what you want, use the 3rd party ngx_lua module (v0.3.1rc24 and above) …
You can get ngx_lua at https://github.com/chaoslawful/lua-nginx-module/tags.
PS. Bear in mind that rewrite by lua is always executed after the nginx rewrite directives so if you put any such directives in the other case, they will be executed first and you will get funnies.
You should put all your rewrites within the rewrite by lua context for consistent results. This is the reason for the “if ..else..end” arrangement for the “other case”.
You may need this longer version