nginx-子域作为querystring参数? [英] nginx - Subdomain as querystring parameter?

查看:191
本文介绍了nginx-子域作为querystring参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的单页Web应用程序,该应用程序接受查询字符串参数name.该Web应用程序当前会打印参数的值.因此,在http://example.com/app/?name=person1的页面上显示文本person1.

I have a simple, single-page web application which accepts a query string parameter, name. This web application currently prints the parameter's value; so, the page at http://example.com/app/?name=person1 displays the text person1.

我想使用nginx在内部将请求从http://person1.example.com/路由到http://example.com/app/?name=person1,以便检索相同的文本.

I would like to use nginx to internally route requests to http://person1.example.com/ to http://example.com/app/?name=person1 so that the same text is retrieved.

理想情况下,我还想将子域的名称提供给PHP或Node.js进程,以便在不同的子域中重用相同的应用程序文件,从而允许应用程序本身根据内部处理请求的方式客户端当前正在访问的URL.

Ideally, I would also like to make the name of the subdomain available to either a PHP or a Node.js process in order to reuse the same application files across different subdomains, allowing the application itself to handle requests internally based on whichever URL the client is currently accessing.

但是,我想动态地执行此操作-无需为每个子域设置新的虚拟主机.

However, I would like to do this dynamically- without setting up a new virtual host for every subdomain.

可以使用nginx上的动态虚拟主机来完成此操作吗?如果可以,如何执行? 有人可以指出我正确的方向,还是可以帮助解释我在努力理解的内容?

Can this be done with dynamic virtual hosts on nginx, and if so, how? Can anyone point me in the right direction, or help to explain what I'm struggling to understand?

此外,还有什么比我尝试做的更好的选择了?

Additionally, is there a better alternative to what I am attempting to do?

推荐答案

如果外部重定向正常,我会尝试以下操作:

If an external redirect is alright, I would try something like the following:

map $host $subdomain {
    ~^(?<sub>.+)\.[^\.]+\.[^\.]+$ $sub;
}

server {
    listen  80  default_server;
    server_name _;

    if ($subdomain) {
        return 301 http://example.com/app/?name=$subdomain;
    }
}

如果需要内部重定向,则可能需要重写或proxy_pass.

If an internal redirect is required, a rewrite or proxy_pass may be necessary.

这篇关于nginx-子域作为querystring参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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