Skip to content

Instantly share code, notes, and snippets.

@rduplain
Created April 10, 2012 15:58
Show Gist options
  • Save rduplain/2352388 to your computer and use it in GitHub Desktop.
Save rduplain/2352388 to your computer and use it in GitHub Desktop.
Flask: serving static file from a separate domain
# http://flask.pocoo.org/mailinglist/archive/2012/4/10/serving-static-file-from-a-separate-domain-in-production/
from flask import Flask, url_for
# Uncomment to set server name.
# SERVER_NAME = 'mianos.com'
app = Flask(__name__, static_folder=None)
app.config.from_object(__name__)
app.add_url_rule('/<path:filename>', endpoint='static',
view_func=app.send_static_file, subdomain='static')
@app.route('/')
def index():
return url_for('static', filename='style.css')
if __name__ == '__main__':
app.run(debug=True)
@ajford
Copy link

ajford commented Jul 28, 2012

Is there a way to make this work in both development and deployment? I seem to have issues with this locally...

@rduplain
Copy link
Author

Given that there are multiple domains here, you need a DNS record pointing at your development instance, since 'localhost' does not support subdomains. I typically set local.example.com and *.local.example.com to 127.0.0.1 on my example.com DNS manager. Then SERVER_NAME is local.example.com in this gist.

@jamesingham
Copy link

If I use "app.debug = True" and "app.run(host='0.0.0.0', port=5000)", the static content will be given the port 5000 while testing. Is there any way around this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment