nginx 代理传递子路径未重定向 [英] nginx proxy pass subpaths not redirected

查看:16
本文介绍了nginx 代理传递子路径未重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 nginx 配置:

I have the following nginx config:

    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 的所有请求重定向到后端圆形立方体服务器.

So, I would like to redirect all requests from /mail to a backend roundcube server.

但是,只有匹配 /mail 的请求被重定向.所以,/mail/plugins 等...没有被重定向,这意味着我没有任何 CSS 或 JS 等,因为 nginx 试图在本地找到它们.

But, only the request which matches /mail is redirected. So, /mail/plugins, etc... are not redirected which means I don't have any CSS or JS, etc. as nginx is trying to find them locally.

如何正确重定向所有路径?

How can I have all the paths correctly redirected?

这是我完整的 nginx 配置.前端是 owncloud.

Here is my complete nginx config. The frontend is 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.

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

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

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).

我的建议是更换

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

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

这篇关于nginx 代理传递子路径未重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆