gunicornwsgipyramidwaitress

Setting application URL prefix with Gunicorn


I am currently moving Pyramid WSGI based application from the Waitress web server to Gunicorn web server due to multiprocess pooling support.

Currently, the application is being served with Waitress as:

from waitress import serve

serve(app,
      host='127.0.0.1',
      trusted_proxy="127.0.0.1",
      threads=32,
      port=port,
      trusted_proxy_headers="forwarded",
      url_scheme=scheme,  # HTTPS 
      url_prefix='/api')

Now, I would like to serve the same application with Gunicorn. I managed to find all matching options on Gunicorn, but not url_prefix. What is Gunicorn equivalent for setting the application URL prefix (I think this is sometimes also called application root)?


Solution

  • One can use SCRIPT_NAME environment variable in Gunicorn to configure the WSGI application root.

    Here is an example:

    # name is for Datadog agent
    # bind matches one in Caddyfile config
    # give enough worker processes, threads will have GIL issues
    # use SCRIPT_NAME to prefix our API
    gunicorn --name backend --bind 127.0.0.1:3456 --workers 8 --threads 2 "backend.server.server:gunicorn_entry_point()" --env "SCRIPT_NAME=/api"
    

    See also related Caddfyfile.