单个 Pyramid 实例上的多个域和子域

发布于 2024-12-07 02:48:52 字数 190 浏览 1 评论 0原文

我希望在单个 Pyramid 实例上拥有多个域和子域。但是,我似乎找不到任何有关它的文档。最后一个问题引用了一个词汇表,信息很少,也没有示例。你们中有人有任何例子或者可以指导我更好的文档吗?

I'm looking to have multiple domains and subdomains on a single Pyramid instance. However, I can't seem to find any documentation on it. The last question referred to a glossary with very little information and no examples. Do any of you have any examples or can direct me to better documentation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

找回味觉 2024-12-14 02:48:52

Pyramid 只是一个 WSGI 应用程序。这意味着它依赖于 HTTP_HOST 环境键(由 Host 标头设置)来确定应用程序的主机。这都是相对的。重点是金字塔对其可以接受的内容没有限制,因此世界是你的牡蛎,你可以将其设置为将内容限制到不同的域,无论你想要什么。当然,这首先要从您的网络服务器配置为提供给您的应用程序的主机开始。

假设您使用 URL 调度,您可能需要设计一些自定义路由谓词来检查 request.host 值是否符合您的要求。从该谓词返回 False 将阻止该路由匹配对该主机的请求。

这是一个很大的主题,因此如果您提供更多细节可能会有所帮助。例如,由于 Pyramid 是相对的,因此您可能想要从“example.com”生成以将某人重定向到“sub.example.com”的任何 URL 都需要通过预生成器来完成。

def pregen(request, elements, kw):
    kw['_app_url'] = 'http://sub.example.com'
    return elements, kw

def req_sub(info, request):
    return request.host.startswith('sub')

config.add_route('sub_only', '/',
                 custom_predicates=(req_sub,),
                 pregenerator=pregen)
config.add_route('foo', '/foo')
config.add_view(view, route_name-'foo')

def view(request):
    # redirect the user to "http://sub.example.com", regardless of whether
    # request.host is "example.com" or "sub.example.com"
    return HTTPFound(request.route_url('sub_only'))

Pyramid is just a WSGI application. This means it's dependent on the HTTP_HOST environ key (set by the Host header) to determine the host of the application. It's all relative. Point-being that Pyramid has no restrictions on what it can accept, thus the world is your oyster and you can set it up to limit content to various domains however you'd like. This of course starts with what hosts your webserver is configured to feed to your application.

Assuming you're using URL dispatch, you might want to design some custom route predicates that check the request.host value for whatever you'd like. Returning False from that predicate will prevent that route from ever matching a request to that host.

This is a large topic, so it might help if you give some more specifics. For example, since Pyramid is relative, any URL you may want to generate from 'example.com' to redirect someone to 'sub.example.com' will need to be done via a pregenerator.

def pregen(request, elements, kw):
    kw['_app_url'] = 'http://sub.example.com'
    return elements, kw

def req_sub(info, request):
    return request.host.startswith('sub')

config.add_route('sub_only', '/',
                 custom_predicates=(req_sub,),
                 pregenerator=pregen)
config.add_route('foo', '/foo')
config.add_view(view, route_name-'foo')

def view(request):
    # redirect the user to "http://sub.example.com", regardless of whether
    # request.host is "example.com" or "sub.example.com"
    return HTTPFound(request.route_url('sub_only'))
爱的故事 2024-12-14 02:48:52

如果您可以控制您的托管环境,我强烈建议将域内容保留在金字塔之外,并使用代理服务器(例如 apache mod proxy)处理它,路由到金字塔中的子域。然后,您可以轻松切换任何域名来查看路由,而无需在金字塔代码中添加任何脆弱的内容(例如域名)。这样您的应用程序代码将会更加简洁,并且以后更容易更改。

下面是一个 Apache 示例,其中两个域连接到一个金字塔应用程序,假设我们在端口 5001(gunicorn 或任何您想要的端口)上以某种方式为金字塔应用程序提供服务。

<VirtualHost *:80>
    ServerName domain_2.com

    ProxyPreserveHost On

    # send all request to our app at /app1/*
    ProxyPass / http://127.0.0.1:5001/app_1/
    ProxyPassReverse / http://127.0.0.1:5001/app_1/

</VirtualHost>

<VirtualHost *:80>
    ServerName domain_2.com

    ProxyPreserveHost On

    # send all request to our app at /app2/*
    ProxyPass / http://127.0.0.1:5001/app_2/
    ProxyPassReverse / http://127.0.0.1:5001/app_2/

</VirtualHost>

下面是一个域连接到多个金字塔实例的示例:

<VirtualHost *:80>
    ServerName mydomain.com

    ProxyPreserveHost On

    # admin go to manager app on 5001
    ProxyPass /media/manager/ http://127.0.0.1:5001/ retry=5
    ProxyPassReverse /media/manager/ http://127.0.0.1:5001/

    # downloads from server app on 5002
    ProxyPass /media/server/ http://127.0.0.1:5002/ retry=5
    ProxyPassReverse /media/server/ http://127.0.0.1:5002/

</VirtualHost>

If you have control over your hosting environment, I would strongly suggest keeping the domain stuff out of pyramid and handling it with a proxy server such as apache mod proxy, routing to subdomains in pyramid. Then you can easily switch any of the domain name to view routing without having anything fragile (like domain names) in your pyramid code. Your app code will be much cleaner this way, and far easier to change later.

Here's an Apache example of two domains going to one pyramid app, assuming we are serving the pyramid app somehow or another on port 5001 (gunicorn or whatever you want).

<VirtualHost *:80>
    ServerName domain_2.com

    ProxyPreserveHost On

    # send all request to our app at /app1/*
    ProxyPass / http://127.0.0.1:5001/app_1/
    ProxyPassReverse / http://127.0.0.1:5001/app_1/

</VirtualHost>

<VirtualHost *:80>
    ServerName domain_2.com

    ProxyPreserveHost On

    # send all request to our app at /app2/*
    ProxyPass / http://127.0.0.1:5001/app_2/
    ProxyPassReverse / http://127.0.0.1:5001/app_2/

</VirtualHost>

And here's an example of one domain going to several pyramid instances:

<VirtualHost *:80>
    ServerName mydomain.com

    ProxyPreserveHost On

    # admin go to manager app on 5001
    ProxyPass /media/manager/ http://127.0.0.1:5001/ retry=5
    ProxyPassReverse /media/manager/ http://127.0.0.1:5001/

    # downloads from server app on 5002
    ProxyPass /media/server/ http://127.0.0.1:5002/ retry=5
    ProxyPassReverse /media/server/ http://127.0.0.1:5002/

</VirtualHost>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文