简体   繁体   中英

How to use CherrPy as Web server and Bottle as Application to support multiple virtual hosts?

I have a website (which running in Amazon EC2 Instance) running Python Bottle application with CherryPy as its front end web server.

Now I need to add another website with a different domain name already registered. To reduce the cost, I want to utilize the existing website host to do that.

Obviously, virtual host is the solution.

I know Apache mod_wsgi could play the trick. But I don't want to replace CherryPy.

I've googled aa lot, there are some articles showing how to make virtual hosts on CherryPy, but they all assume Cherrypy as Web Sever + Web application, Not CherrPy as Web server and Bottle as Application.

How to use CherrPy as Web server and Bottle as Application to support multiple virtual hosts?

As you mentioned, use VirtualHost . In the example cherrypy.Application instances are used, but any WSGI callable (eg Bottle app) will do.

也许您可以简单地将nginx用作反向代理,并将其配置为将流量发送到正确上游的两个域(cherryPy Web服务器)。

Another idea would be to use Nginx ( http://wiki.nginx.org/Main ) with uWsgi( http://projects.unbit.it/uwsgi/ ) & (uWsgi-python) plug-in

uWsgi has a module named emperor that you can link vhosts(vassals) in, sort of.

i'm a newbie at this myself, so not necessarily an answer but rather a suggestion to check it out. just a heads up, uWsgi and Nginx can be a hassle to get it to work, depending on your linux distro. Does work nicely with bottle, tested it myself. hope it helps

jwalker's answer is pretty clear. In case any CherryPy newbie need whole script for reference, I post one below.

import cherrypy
from bottle import Bottle
import os


app1 = Bottle() 
app2 = Bottle()

@app1.route('/')
def homePage():    
    return "=========  home1 ==============="


@app2.route('/')
def homePage_2():    
    return "=========  home2 ==============="

vhost = cherrypy._cpwsgi.VirtualHost(None, 
                                     domains={
                                              'www.domain1.com': app1, 
                                              'www.domain2.com': app2,
                                             }
                                     )

cherrypy.tree.graft(vhost) 

cherrypy.config.update({
                        'server.socket_host': '192.168.1.4',
                        'server.socket_port': 80,
                      })
cherrypy.engine.start()
cherrypy.engine.block()

you could make www.domain1.com and www.domain1.com point to one IP adress of you server, so it servers for 2 domain in one Web Server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM