Nginx重写URL以包含通配符子域,同时更改域后缀

Nginx重写URL以包含通配符子域,同时更改域后缀,url,redirect,nginx,url-rewriting,Url,Redirect,Nginx,Url Rewriting,需要一些帮助。。。我需要在nginx中重写本地图像的url。这里有一些规则 如果映像不在文件系统上,则重写url。 url可以是通配符子域 域后缀需要更改为.com 例如 a.example.dev -> a.example.com b.example.dev -> b.example.com *.example.dev -> *.example.com 最后,我需要附加图像url,这里是一个完整的示例 原创 http://www.engineering.example.

需要一些帮助。。。我需要在nginx中重写本地图像的url。这里有一些规则

如果映像不在文件系统上,则重写url。 url可以是通配符子域 域后缀需要更改为.com

例如

a.example.dev -> a.example.com

b.example.dev -> b.example.com

*.example.dev -> *.example.com
最后,我需要附加图像url,这里是一个完整的示例

原创

http://www.engineering.example.dev/files/2015/05/filename.gif
http://www.engineering.example.com/files/2015/05/filename.gif
http://www.example.dev/files/2015/05/filename.gif
http://www.example.com/files/2015/05/filename.gif
决赛

http://www.engineering.example.dev/files/2015/05/filename.gif
http://www.engineering.example.com/files/2015/05/filename.gif
http://www.example.dev/files/2015/05/filename.gif
http://www.example.com/files/2015/05/filename.gif
原创

http://www.engineering.example.dev/files/2015/05/filename.gif
http://www.engineering.example.com/files/2015/05/filename.gif
http://www.example.dev/files/2015/05/filename.gif
http://www.example.com/files/2015/05/filename.gif
决赛

http://www.engineering.example.dev/files/2015/05/filename.gif
http://www.engineering.example.com/files/2015/05/filename.gif
http://www.example.dev/files/2015/05/filename.gif
http://www.example.com/files/2015/05/filename.gif
任何帮助都将不胜感激。

可以在命名捕获中收集通配符子域。然后可以使用
try\u files
有条件地重定向到
.com

例如:

server {
    server_name  "~^(?<name>.+)\.dev$";

    root /path/to/root;

    location / {
        try_files $uri @redirect;
    }
    location @redirect {
        return 301 $scheme://$name.com$request_uri;
    }
}
服务器{
服务器名称“~^(?.+)\.dev$”;
根/路径/到/根;
地点/{
尝试_文件$uri@redirect;
}
位置@重定向{
返回301$scheme://$name.com$request\u uri;
}
}