My use case requires nginx to rewrite the target url, converting the last segment of the request uri to the target service port which is accessed by a wireguard vpn. This next config version works fine. That is, my backend hosted app is published ok, but the auth_request directive is ignored =>
location /publish {
auth_request /auth;
rewrite ^/publish/([0-9]+) http://10.11.2.3:$1;
proxy_http_version 1.1;
}
I've proven the auth service is working fine. I'm stuck trying to resolve the target url which requires a port but no path.
location ~ ^/publish/([0-9]+)$ {
auth_request /auth;
proxy_pass http://10.11.2.3:$1/;
proxy_http_version 1.1;
}
This config fails because my backend service rejects the trailing slash. Error log =>
(111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: api.myservice.com, request: "GET /publish/5500 HTTP/1.1", upstream: "http://10.11.2.3:5500/"
If change the above config to proxy_pass http://10.11.2.3:$1;
then, as expected, the full uri path is appended. Error detail => upsteam: http://10.11.2.3:5500/publish/5500
I've also tried combining rewrite with break followed by proxy_pass =>
location /publish/ {
auth_request /auth;
rewrite ^/publish/([0-9]+)$ $1 break;
proxy_pass http://10.11.2.3:$1;
proxy_http_version 1.1;
}
This almost works except that the numeric uri is appended because I can't apply a trailing slash on the proxy_pass url. Error log =>
(111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: api.myservice.com, request: "GET /publish/5500 HTTP/1.1", upstream: "http://10.11.2.3:55005500"
Any ideas? proxy_redirect?
/
, so what you are trying to configure looks strange. Further, all your configurations snippets fail due to a connection failure, note(111: Connection refused) while connecting to upstream
, and not due to an incorrect path being used. First of all, you may want to clarify what you are trying to do. Do you want nginx to return a redirect, as in your first config snippet? Or do you want to configure proxying, as in other snippets?location = /pub/ { auth_request /auth; proxy_pass http://10.11.2.3:$1; }
Typo correction above => apparently that's the only way auth_request will work.auth_request
, since rewrites happen when selecting a location, before any access checks. If you want nginx to return a redirect after checking authentication, you can do so by any content module - proxying to a backend which will return a redirect is a valid way to do this, but you need a backend to return a redirect. Note though that clients will access returned URL without any checks on nginx side.