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

Hello,

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 rewrite_path.py

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 {
      192.168.0.2:8080;
    }
    upstream mitmproxy {
      192.168.0.3:8080;
    }
   
      location /api/input {

        proxy_pass http://webserver/api/output;

        set $upstream mitmproxy;
        if ($remote_addr ~ (1.1.1.1|8.8.8.8)) {

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

        }
      }

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

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