Rewrite and Proxy without trailing forward slash /

Hello,

Hoping to get a bit of help with my migration from nginx. I’m not having much luck converting the following nginx directive into Caddy format.

If I go to the url /proxy/ it works, but not /proxy

Thanks for any help :smile:

Nginx directive which works

        location /proxy/ {
                rewrite /proxy/(.*) /$1 break;
                proxy_pass http://192.168.1.3:61208/;
        }

Caddyfile

mysite.com {
        gzip
        log /opt/caddy/logs/access.log
        errors /opt/caddy/logs/errors.log
        rewrite /proxy {
                r (.*)
                to /proxy/{1}
        }
        proxy /proxy 192.168.1.3:61208 {
                transparent
                without /proxy
        }

Error logs (you can see where the rewrite doesn’t seem to trigger if I don’t put the forward slash - there are 404’s)

X.X.X.X - user@domain.com [24/Sep/2018:00:08:18 +0000] "GET /proxy/ HTTP/1.1" 200 353
X.X.X.X - user@domain.com [24/Sep/2018:00:08:18 +0000] "GET /proxy/api/3/all HTTP/1.1" 200 6827
X.X.X.X - user@domain.com [24/Sep/2018:00:08:19 +0000] "GET /proxy/api/3/all/limits HTTP/1.1" 200 854
X.X.X.X - user@domain.com [24/Sep/2018:00:08:19 +0000] "GET /proxy/api/3/args HTTP/1.1" 200 783
X.X.X.X - user@domain.com [24/Sep/2018:00:08:19 +0000] "GET /proxy/api/3/all/views HTTP/1.1" 200 2346
X.X.X.X - user@domain.com [24/Sep/2018:00:08:19 +0000] "GET /proxy/api/3/config HTTP/1.1" 200 1290
X.X.X.X - user@domain.com [24/Sep/2018:00:08:24 +0000] "GET /proxy HTTP/1.1" 200 353
X.X.X.X - user@domain.com [24/Sep/2018:00:08:24 +0000] "GET /api/3/config HTTP/1.1" 404 38
X.X.X.X - user@domain.com [24/Sep/2018:00:08:24 +0000] "GET /api/3/all/limits HTTP/1.1" 404 38
X.X.X.X - user@domain.com [24/Sep/2018:00:08:24 +0000] "GET /api/3/args HTTP/1.1" 404 38
X.X.X.X - user@domain.com [24/Sep/2018:00:08:24 +0000] "GET /api/3/all HTTP/1.1" 404 38
X.X.X.X - user@domain.com [24/Sep/2018:00:08:24 +0000] "GET /api/3/all/views HTTP/1.1" 404 38

I don’t think what you have is a literal translation of the nginx configuration.

In nginx, you’re actually removing the /proxy/ prefix, then proxying the result upstream.

In Caddy, you’re effectively rewriting to /proxy//proxy, then stripping the /proxy when you send it upstream (without /proxy).

A literal translation of the nginx configuration would simply be:

example.com {
  proxy /proxy/ http://192.168.1.3:61208/ {
    without /proxy
  }
}

If you’re still having issues, then you can add in a redirect to ensure that requests for /proxy (no trailing slash) are being correctly requested by the client:

  redir /proxy /proxy/

This will ensure that modern browsers properly request relative asset links - if they didn’t request a trailing slash, a relative link will indicate the directory above instead, which would match the behaviour we’re seeing in your logs.

1 Like

Thank you so much for the explanation @Whitestrake !

the redir is what was missing. Still getting used to the transition.

Thanks!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.