我有以下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配置。前端是自己的云。
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;
}
}
答案 0 :(得分:41)
引用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/;
答案 1 :(得分:5)
尝试:
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;
}
答案 2 :(得分:0)
如果我将多余的内容更改为下面的/,则它将匹配邮件以及/ mail以外的所有内容。
location /mail/ {
}
NGINX 1.14.2