Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6758881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:51:28+00:00 2026-05-26T13:51:28+00:00

I am having trouble matching a specific word in nginx $request_body variable. I want

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:51:28+00:00Added an answer on May 26, 2026 at 1:51 pm

    Your code has a number of problems.

    1. “($request_body ~* (.*))” never matches anything as stated by someone else and so the the “other case” is always the outcome

    2. 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) …

    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 {    
        # test.proxy stuff 
        ... 
    }
    
    location @other_case {
        # other_case stuff 
        ... 
    }
    

    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


    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
        ';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to parse markdown style links, but I'm having some trouble matching the
I am having a trouble matching a string in a MySQL-query with the following
Im having trouble extracting only a matching string: OPER^ from a log4j file. I
I'm having trouble with matching some form input to a value in an array.
I have just started learning MySQL and am having trouble extracting matching flights from
I am having major trouble selecting the most current/matching row in my database. For
so I'm having trouble properly creating this query. Basically, I want to insert multiple
I am having trouble matching the height of a div after the loading of
I'm working in Java and having trouble matching a repeated sequence. I'd like to
I'm having trouble getting a Linq to Sql query to work. Basically, I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.