问题
When pserve starts by default it runs the pyramid application in http://0.0.0.0:6543 however how can I changed it to http://0.0.0.0:6543/myapp
In the settings I can change the port but I haven't found elsewhere where to change the root path
回答1:
In any WSGI application the environ['SCRIPT_NAME']
is very important here. It defines the root path for all urls in the app. The full path is environ['SCRIPT_NAME'] + environ['PATH_INFO']
. Assuming you have done things properly in your app (for example request.route_url(..)
will generate urls using this information) then you can simply remount your application elsewhere (the default SCRIPT_NAME
is ''
) by instructing it that it should be something else.
There are a couple things you can do based on how you're deploying your application (if it's behind a proxy then things are slightly more complex). Let's assume you're just using a simple pyramid app hosted with waitress. You can move your app using the rutter
[1] package which will match the /myapp/*
path and send all requests to your app with the appropriate SCRIPT_NAME
(myapp
) and PATH_INFO
.
The declarative config is the simplest for a pyramid app. Just install rutter
and then update your INI file to mount your application at /myapp
prefix:
[app:foo]
use = egg:myapp#main
[composite:main]
use = egg:rutter#urlmap
/myapp = foo
Note I renamed the app:main
to app:foo
because you can only have one wsgi component named main
and we want it to be the composite.
[1] http://rutter.readthedocs.io/en/latest/#declarative-configuration-using-paste-deploy-ini-files
来源:https://stackoverflow.com/questions/43502411/pyramid-pserve-in-different-root-path-than