Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx proxy pass subpaths not redirected

Tags:

nginx

proxy

References to nginx docs: HttpCoreModule#location, HttpProxyModule#proxy_pass.

There is a better way than using regex (which is slow) for location match. In this case, you could use ^~ to tell nginx to match the given prefix /mail before doing any regex match. You also don't need that rewrite rule because proxy_pass can do that simple rewrite by itself (by adding a trailing slash / in the upstream server url).

My suggestion is to replace

    location ~ ^/mail(.*)$ {
        rewrite           ^/mail/(.*) /$1 break;
        proxy_pass https://roundcube-host;

by

    location ^~ /mail {
        proxy_pass https://roundcube-host/;

try:

location ~ ^/mail(.*)$ {
           rewrite           ^/mail/(.*) /$1 break;
           proxy_pass https://roundcube-host;
           proxy_connect_timeout 1;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header Host $http_host;
           proxy_set_header X-Forwarded-Proto https;
    }

If I change it to the below with the extra / it matches everything beyond mail as well as /mail directly.

location /mail/ {

}

NGINX 1.14.2