单个 Pyramid 实例上的多个域和子域
我希望在单个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Pyramid 只是一个 WSGI 应用程序。这意味着它依赖于 HTTP_HOST 环境键(由 Host 标头设置)来确定应用程序的主机。这都是相对的。重点是金字塔对其可以接受的内容没有限制,因此世界是你的牡蛎,你可以将其设置为将内容限制到不同的域,无论你想要什么。当然,这首先要从您的网络服务器配置为提供给您的应用程序的主机开始。
假设您使用 URL 调度,您可能需要设计一些自定义路由谓词来检查
request.host
值是否符合您的要求。从该谓词返回 False 将阻止该路由匹配对该主机的请求。这是一个很大的主题,因此如果您提供更多细节可能会有所帮助。例如,由于 Pyramid 是相对的,因此您可能想要从“example.com”生成以将某人重定向到“sub.example.com”的任何 URL 都需要通过预生成器来完成。
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. ReturningFalse
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.
如果您可以控制您的托管环境,我强烈建议将域内容保留在金字塔之外,并使用代理服务器(例如 apache mod proxy)处理它,路由到金字塔中的子域。然后,您可以轻松切换任何域名来查看路由,而无需在金字塔代码中添加任何脆弱的内容(例如域名)。这样您的应用程序代码将会更加简洁,并且以后更容易更改。
下面是一个 Apache 示例,其中两个域连接到一个金字塔应用程序,假设我们在端口 5001(gunicorn 或任何您想要的端口)上以某种方式为金字塔应用程序提供服务。
下面是一个域连接到多个金字塔实例的示例:
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).
And here's an example of one domain going to several pyramid instances: