Search code examples
amazon-web-servicesflaskgunicorn

Configuring a Flask application running on gunicorn to run behind a file path


I am trying to configure a Flask application hosted with gunicorn to run behind a file path. The current app is designed to run at www.example.com, but I would like to configure it to run at example.com/foo/bar

The difficulty I am having is that I can't seem to get the requests to be sent to the application. It currently works fine if you go to example.com, but going to example.com/foo/bar results in nothing. Everything is done through AWS. The networking has been setup to pass example.com/foo/bar requests to the server. The Flask init is as follows:

import os
from functools import wraps

import requests
from flask import Flask, render_template, make_response, redirect, Response, g

app = Flask(__name__)
app.config['VERSION'] = '3.7.2'
app.config['SERVER_ADDRESS'] = os.environ.get('SERVER_ADDRESS', 'https://example.com/')
app.config['APPLICATION_ROOT'] = '/foo/bar/'

@app.before_request
def set_server_address():
    g.server_address = app.config['SERVER_ADDRESS']

@app.route('/')
def index(*args, **kwargs):
    return redirect('/app/example')

When I check the logs after visiting example.com/foo/bar, there is no traffic. Any light which could be shed on this would be very much appreciated.


Solution

  • You can solve this problem by adding the /foo/bar to your flask route.

    @app.route('/foo/bar')
    def make_homepage():
        return 'success'
    

    But it's not best practice because each time the response will be as a redirect and with high traffic it's could be pretty resource intensive, so i will recommend to you to check this question: Python - Flask Default Route possible?