Static files with Flask in production Static files with Flask in production flask flask

Static files with Flask in production


  1. Set another static folder (you will need also create empty folder on file system):

    app = Flask(__name__, static_url_path='static', static_folder='static_test')

    So if you get 404 then files serving by flask else apache.

  2. Add test folder and map apache as for static folder.

    So if you get content from test folder then files serving by apache else flask.

  3. Break any static endpoint requests:

    @app.before_requestdef break_static():    if request.endpoint == 'static':        abort(404)    return None

    or

    class YourApp(Flask):    def send_static_file(self, filename):        abort(404)

    So if you get 404 then files serving by flaks else apache.

But you enough once test this case if you realy not sure. If apache configuration will not changing then static serving behaviour will not changing too.


You can be sure as long as you have this:

if __name__ == '__main__':    app.run()

The app.run() call actually starts the local development server. In production, you will/should never have to call this and hence nothing can be served by the dev. server since it is never started.