NGINX通配符在同一服务器块中重写 [英] NGINX wildcard rewrite in same server block

查看:118
本文介绍了NGINX通配符在同一服务器块中重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I cannot find an example of the specific rewrite rule im attempting.

例如,我只想对输入的子域有一个重写规则.

I would like to have a rewrite rule for only subdomains entered for example.

https://sub.example.com/-> 从我的尝试来看,这似乎是我得到的最接近的代码.

From what ive been trying this looks like the closest code i have got.

server_name example.com;

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

推荐答案

看看文档:

  • http://nginx.org/r/server_name
  • http://nginx.org/r/if

这将是一种解决方案:

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


这是另一种解决方案:


This would be another solution:

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


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


Which solution is better? It's hard to tell — it depends on the patterns of the traffic you receive, as well as on what other servers are configured on the same listen handle.

这篇关于NGINX通配符在同一服务器块中重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆