【问题标题】:Nginx Rewrite URL to contain the wildcard subdomain and change domain suffix at the same timeNginx 重写 URL 以包含通配符子域并同时更改域后缀
【发布时间】:2017-09-18 21:12:38
【问题描述】:

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

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

举个例子

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

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

*.example.dev -> *.example.com

最后我需要附加图片网址,这是一个完整的例子

原创

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

任何帮助将不胜感激。

【问题讨论】:

    标签: url redirect nginx url-rewriting


    【解决方案1】:

    regular expression server name 可以在命名捕获中收集通配符子域。然后您可以使用try_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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-08-09
      • 2014-06-11
      • 1970-01-01
      • 2014-12-29
      • 2011-01-11
      • 2022-01-20
      • 2015-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多