There are a number of problems in your code.
"($ request_body ~ * (. *))" never matches anything as indicated by someone else, and therefore the "other case" is always the result
More importantly, it uses "proxy_pass" with "if", which is classically evil. http://wiki.nginx.org/IfIsEvil .
To get what you want, use the third-party module ngx_lua (v0.3.1rc24 and higher) ...
location ~ \.php$ { rewrite_by_lua ' ngx.req.read_body() local match = ngx.re.match(ngx.var.request_body, "target") if match then ngx.exec("@proxy"); else ngx.exec("@other_case"); end '; } location @proxy {
You can get ngx_lua at https://github.com/chaoslawful/lua-nginx-module/tags .
PS. Keep in mind that rewrite by lua is always executed after the nginx rewrite directives, so if you put any such directives in another case, they will be executed first and you will get funnies.
You must put all your rewrites in correspondence using the lua context to get consistent results. This is the reason that "if..sese..end" is for a "different case."
You may need this longer version.
location ~ \.php$ { rewrite_by_lua ' --request body only available for POST requests if ngx.var.request_method == "POST" -- Try to read in request body ngx.req.read_body() -- Try to load request body data to a variable local req_body = ngx.req.get_body_data() if not req_body then -- If empty, try to get buffered file name local req_body_file_name = ngx.req.get_body_file() --[[If the file had been buffered, open it, read contents to our variable and close]] if req_body_file_name then file = io.open(req_body_file_name) req_body = file:read("*a") file:close() end end -- If we got request body data, test for our text if req_body then local match = ngx.re.match(req_body, "target") if match then -- If we got a match, redirect to @proxy ngx.exec("@proxy") else -- If no match, redirect to @other_case ngx.exec("@other_case") end end else -- Pass non "POST" requests to @other_case ngx.exec("@other_case") end '; }
Dayo
source share