How to specify proxy URI (Path) for mitmproxy - Rewrite path


We are trying to analyze traffic between our Nginx reverse proxy and Web server:
Client --> Internet --> Nginx --> Webserver
Client --> Internet --> Nginx --> mitmproxy --> Webserver

If I understand correctly we should use a reverse mode:

mitmproxy --listen-port 8080 --mode reverse:http://webserver:8081

But our clients send requests with one URI which is routed by Nginx to Webserver to another URI:
client --> nginx/api/input --> webserver/api/output

Probably we may achieve this by using a script. We tried this one:

def request(context, flow):
if flow.request.path == '/api/input':
    flow.request.path = '/api/output'

mitmproxy --listen-port 8080 --mode reverse:http://webserver:8081 --script

But it doesn’t do the work, we still see:

POST http://webserver:8081/api/input

  1. How proxy URI can be specified in mitmproxy?
  2. If with the script only where we may find a sample?
  3. How mitmproxy may be started in reverse mode with the script, should we specify a mode in such case?

Thank you!

We did it on Nginx side:

    upstream webserver {;
    upstream mitmproxy {;
      location /api/input {

        proxy_pass http://webserver/api/output;

        set $upstream mitmproxy;
        if ($remote_addr ~ (| {

          proxy_pass http://$upstream/api/output;


All clients go to the webserver and only and via mitmproxy:

mitmweb --no-web-open-browser \
        --web-iface \
        --web-port 8081 \
        --listen-host \
        --listen-port 8080 \
        --mode reverse: