Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewrite request on Nginx from GET to POST with body (for tracking pixel)

I'm trying to figure out if there's an easy way to convert a tracking pixel request that gets to Nginx into a POST that will go to an upstream with some added body.

for exmaple, if I get a GET request for http://domain.com/track/mail-id.gif, I'd like to configure Nginx to convert it to a POST that goes to http://upstream/mail-id with some body (let's say status:opened).

how can it be done?

like image 793
benyamin_d Avatar asked Feb 28 '17 11:02

benyamin_d


People also ask

What does NGINX rewrite do?

NGINX rewrite rules are used to change entire or a part of the URL requested by a client. The main motive for changing an URL is to inform the clients that the resources they are looking for have changed its location apart from controlling the flow of executing pages in NGINX.

What is Proxy_pass in nginx?

The proxy_pass setting makes the Nginx reverse proxy setup work. The proxy_pass is configured in the location section of any virtual host configuration file. To set up an Nginx proxy_pass globally, edit the default file in Nginx's sites-available folder.

What is $scheme in nginx?

The rewritten URL uses two NGINX variables to capture and replicate values from the original request URL: $scheme is the protocol (http or https) and $request_uri is the full URI including arguments. For a code in the 3xx series, the url parameter defines the new (rewritten) URL.


1 Answers

Just wanted to add a more detailed example:

location /track/mail-id.gif {
    proxy_pass http://upstream/mail-id;
    proxy_method POST;
    proxy_set_body "status:opened";
    # if needed
    # proxy_set_header Some-Header value;
}

Provided a url for proxy_pass here, so to ensure the exact behaviour requested.

like image 113
Azat Khadiev Avatar answered Oct 09 '22 21:10

Azat Khadiev