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

标签 nginx proxy

我有以下 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;
    }
}

最佳答案

对 nginx 文档的引用:HttpCoreModule#location , HttpProxyModule#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/;

关于nginx 代理传递子路径未重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17423414/

相关文章:

javascript - 无法复制 nginx/etc/nginx/conf.d/default.conf 中的配置文件

php - 以相同的响应时间向用户显示实时数据

R Shiny - 用户管理

nginx - 在入口主机名中使用通配符

c# - 每个控制台应用程序使用代理

nginx - 如何在 nginx 中进行 Web 服务调用?

ssl - nginx 代理传递给 sslv3 上游

ruby-on-rails - 通过 Nginx 代理时使用 OmniAuth 时出现 CallbackError

javascript - 使用 Proxy 对象时如何将参数捕获到目标方法?

debugging - Chromecast 和 Charles 代理 - 如何连接