nginx代理传递子路径未重定向

oogrdqng  于 2023-10-17  发布在  Nginx
关注(0)|答案(4)|浏览(148)

下面是nginx的配置:

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;
    }

使用:

upstream roundcube-host {
          server roundcube-ip-address:443;
    }

因此,我想将所有来自/mail的请求重定向到后端的roundcube服务器。
但是,只有匹配/mail的请求才被重定向。所以,/mail/plugins,等等...没有被重定向,这意味着我没有任何CSS或JS等。Nginx正在尝试在本地找到它们。
我怎样才能让所有的路径都正确地重定向?
这是我的nginx配置。前端是owncloud。

upstream phpcgi {
    fair;
    server 127.0.0.1:9000;
    server 127.0.0.1:9001;
    keepalive 5;
}

upstream roundcube-host {
    server roundcube-ip-address:443;
}

server {
    listen 443 ssl;
    #server_name cloud.example.com;

    ssl_certificate /etc/ssl/certs/owncloud.crt;
    ssl_certificate_key /etc/ssl/private/owncloud.key;

    access_log /var/log/nginx/data_access.log;
    error_log /var/log/nginx/data_error.log info;

    # Path to the root of your installation
    root /var/www/;

    client_max_body_size 10G; # set max upload size
    fastcgi_buffers 64 4K;

    rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
    rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
    rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;

    index index.php;
    error_page 403 = /core/templates/403.php;
    error_page 404 = /core/templates/404.php;

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location ~ ^/(data|config|\.ht|db_structure\.xml|README) {
            deny all;
    }

    location / {
            # The following 2 rules are only needed with webfinger
            rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
            rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;

            rewrite ^/.well-known/carddav /remote.php/carddav/ redirect;
            rewrite ^/.well-known/caldav /remote.php/caldav/ redirect;

            rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;

            try_files $uri $uri/ index.php;
    }

    location ~ ^(.+?\.php)(/.*)?$ {
            try_files $1 = 404;

            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$1;
            fastcgi_param PATH_INFO $2;
            fastcgi_param HTTPS on;
            fastcgi_pass phpcgi;
            # Or use unix-socket with 'fastcgi_pass unix:/var/run/php5-fpm.sock;'
            fastcgi_param MOD_X_ACCEL_REDIRECT_ENABLED on;
    }

    # Optional: set long EXPIRES header on static assets
    location ~* ^.+\.(jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
            expires 30d;
            # Optional: Don't log access to assets
            access_log off;
    }

    # Change the path according to the data directory
    location ~ ^/var/data {
            internal;
            root /;
    }

    location ~ ^/tmp/oc-noclean/.+$ {
            internal;
            root /;
    }

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

xj3cbfub1#

引用nginx文档:HttpCoreModule#locationHttpProxyModule#proxy_pass
有一种比使用正则表达式(它很慢)更好的方法来进行位置匹配。在这种情况下,您可以使用^~告诉nginx在执行任何正则表达式匹配之前匹配给定的前缀/mail。您也不需要重写规则,因为proxy_pass可以自己完成简单的重写(通过在上游服务器url中添加一个尾随斜杠/)。
我的建议是

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

通过

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

vltsax252#

尝试:

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;
    }
k2arahey

k2arahey3#

如果我把它改为下面的额外的/它匹配邮件以外的一切以及/邮件直接。

location /mail/ {

}

Nginx 1.14.2

rn0zuynd

rn0zuynd4#

我也有类似的问题,最后这样做了。

location ^~ /api/profile {
     include common.conf;
     proxy_pass https://$PROFILE_URL:443/app/api/profile/;
}
location ^~ /api/profile/ {
    include common.conf;
    proxy_pass https://$PROFILE_URL:443/app/api/profile/;
}

我不确定它是否比正则表达式快:D问题是^~ /api/profile对我不起作用,因为它不包含子路径,^~ /api/profile/确实包含子路径,但调用/api/profile没有得到匹配。
我不想开始玩regex,所以我只是这样做。很有魅力。

相关问题