Nginx变量用于子域? [英] Nginx variable for subdomain?

查看:87
本文介绍了Nginx变量用于子域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个大师的建议.

I need a Guru's advice.

在Nginx的conf文件上,我想将子域作为变量,以便按如下方式重定向访问.

On Nginx's conf file, I would like to get the subdomain as a variable in order to redirect accesses as follow.

  • 访问:http://userX.example.com/?hoo=bar
  • 重定向:http://example.com/userX/?hoo=bar
  • ACCESS: http://userX.example.com/?hoo=bar
  • REDIRECT: http://example.com/userX/?hoo=bar

但我明白了

  • 重定向:http://example.com/userX.example.com/?hoo=bar

我当前的_http.conf设置如下.显然,它是如此.

My current _http.conf settings are like below. So obviously it works so.

## default HTTP
server {
    listen       80;
    server_name  default_server;
    return       301 http://example.com/$host$request_uri;
}

下面有没有其他类似的物品或方式?

Are there any vaiables or ways to do like below?

## default HTTP
server {
    listen       80;
    server_name  default_server;
    return       301 http://example.com/$subdomain$request_uri;
}

我知道子域部分是否受限制,可以对其进行重定向,但是我每次都必须添加它们,并且我希望它保持尽可能简单.

I know if the subdomain part is limited it can be redirected, but I have to add them each time and I want it to keep as simple as possible.

有什么简单的方法吗?

[ENV] :CentOS:7.3.1611,nginx:nginx/1.13.3,*.example.com在NS设置中定位到同一服务器.

[ENV]: CentOS:7.3.1611, nginx: nginx/1.13.3, *.example.com is targetted to the same server in NS settings.

使用正则表达式:

server {
    listen       80;
    server_name  ~^(?<subdomain>.+)\.example\.com$;
    return       301 http://example.com/$subdomain$request_uri;
}

参考:评论推荐答案

您可以在server_name中使用正则表达式提取所需的部分作为命名捕获.例如:

You can use a regular expression in the server_name to extract the part you need as a named capture. For example:

server {
    listen       80;
    server_name  ~^(?<name>.+)\.example\.com$;
    return       301 http://example.com/$name$request_uri;
}

有关更多信息,请参见此文档.

See this document for more.

这篇关于Nginx变量用于子域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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