Nginx将对subdomain.tld/files/*的请求重写到外部域

Nginx将对subdomain.tld/files/*的请求重写到外部域,nginx,url-rewriting,Nginx,Url Rewriting,我想将所有请求重写到Nginx matchinghttp://*.examle.tld/files/*到http://$1.otherdomain.tld/files/?file=$2。我还想重写没有子域的相同请求,即http://example.tld/files/*至http://otherdomain.tld/files/?file=$1 这样做的原因是使用本地开发的生产文件,而不必同步文件夹 这就是我迄今为止所得到的,但是没有成功: location / { ... re

我想将所有请求重写到Nginx matching
http://*.examle.tld/files/*
http://$1.otherdomain.tld/files/?file=$2
。我还想重写没有子域的相同请求,即
http://example.tld/files/*
http://otherdomain.tld/files/?file=$1

这样做的原因是使用本地开发的生产文件,而不必同步文件夹

这就是我迄今为止所得到的,但是没有成功:

location / {
    ...
    rewrite ^http://(\w+)\.(.*)/files/(.*) http://$1.otherdomain.tld/inc/reader.php?file=$3;
    rewrite ^.*/files/(.*) http://$1.otherdomain.tld/inc/reader.php?file=$1;
    ...
}

谢谢你的帮助

不能将服务器名称用作
rewrite
指令正则表达式的一部分。如果您的
服务器
块具有通配符
服务器名称
,则可以使用命名的捕获,以便稍后在块中使用

例如:

server {
    server_name ~^(?<sub>\w+\.)?example\.tld$;

    location /files/ {
        rewrite ^/files(.*)$ http://${sub}otherdomain.tld/files/?file=$1 permanent;
    }
}
服务器{
服务器名称^(?\w+\)?示例\.tld$;
地点/档案/{
重写^/files(.*)$http://${sub}otherdomain.tld/files/?file=$1永久;
}
}
有关详细信息,请参阅