【问题标题】:NGINX wildcard rewrite in same server blockNGINX 通配符在同一服务器块中重写
【发布时间】:2017-07-27 17:25:54
【问题描述】:

我找不到我正在尝试的特定重写规则的示例。

例如,我想为仅输入的子域设置一个重写规则。

https://sub.example.com/ -> https://example.com/directory1/directory2/sub

根据我的尝试,这看起来是我得到的最接近的代码。

server_name example.com;

if ($host = *.example.com) 
    return 301 https://example.com/directory1/directory2/$1;   
}

【问题讨论】:

标签: nginx url-rewriting rule


【解决方案1】:

查看文档:


这是一种解决方案:

server_name .example.com;
if ($host ~ ^(?<sub>.+)\.example\.com$) {
    return 301 http://example.com/directory1/directory2/$sub;
}

这将是另一种解决方案:

server_name ~^(?<sub>.+)\.example\.com$;
return 301 http://example.com/directory1/directory2/$sub;

哪种解决方案更好?很难说 — 这取决于您收到的流量模式,以及在同一侦听句柄上配置的其他服务器。

【讨论】:

  • 以上工作完美,我尝试将其更改为重写而不是返回,这样我就不会获得对子域无效的 ssl 证书。有没有办法绕过这个问题?
  • 我认为你不应该在这里使用rewrite(如果你需要请求URI,使用$request_uri);但是对于 SSL,这两种方式都没有区别;您显然必须为所有子域拥有适当的证书才能在没有警告的情况下工作(或者只是不使用 SSL!)。
猜你喜欢
  • 1970-01-01
  • 2023-03-13
  • 2014-06-11
  • 1970-01-01
  • 2020-10-07
  • 2019-01-27
  • 1970-01-01
  • 2012-08-09
  • 1970-01-01
相关资源
最近更新 更多