The solution is the following (read the explanation after that) :
location ~* /proxy/(?<pschema>https?):/(?<phost>[\w.]+)(?<puri>\/.*) {
set $adr $pschema://$phost;
rewrite .* $puri break;
resolver 8.8.8.8;
proxy_pass $adr;
add_header X-debug-message "adr: $adr" always;
add_header X-debug-message "puri: $puri" always;
add_header X-debug-message "pschema: $pschema" always;
add_header X-debug-message "phost: $phost" always;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $phost;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
proxy_connect_timeout 1;
proxy_intercept_errors on;
expires 30;
}
The original regex expects a ://
after http(s). Nginx silently cuts away one /
in the URI, therefore it couldn't match. Also, sometimes redirects can happen instead of proxying. But that is done by the service you are requesting. In this case, trying to proxy google like curl -vv http://localhost:8080/proxy/http://google.com/
will lead to google trying to redirect you to www.google.com . But if you try curl -vv http://localhost:8080/proxy/http://www.google.com/
, you will be proxied.
This approach doesn't work for loading entire websites though, as their resources (images, .js, .css etc.) are linked relatively, so the browser will try fetching it from your server. But proxying individual files works fine, which is what my goal was.