nginx proxy pass subpaths not redirected

后端 未结 3 612
迷失自我
迷失自我 2020-12-13 09:36

I have the following nginx config:

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


        
相关标签:
3条回答
  • 2020-12-13 10:03

    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

    0 讨论(0)
  • 2020-12-13 10:09

    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;
        }
    
    0 讨论(0)
  • 2020-12-13 10:17

    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/;
    
    0 讨论(0)
提交回复
热议问题