robbles
robbles

Reputation: 2809

Bypass Flask's SERVER_NAME for a single route

I'm working on a Flask application with many routes under different subdomains. It uses the SERVER_NAME setting and sets subdomains on different blueprints to do initial routing based on the subdomain.

I'm trying to configure a route that bypasses the requirement for the host to match SERVER_NAME now, and can't figure it out. Is it possible to do this at all while keeping SERVER_NAME set?

i.e. I want a specific route to be accessible even if SERVER_NAME="mydomain.com" and the request is for http://localhost:80/my/special/route/

Upvotes: 4

Views: 2615

Answers (1)

Luke Exton
Luke Exton

Reputation: 3676

Any route is just an extension/implementation of Werkzeug's Rule. So just set subdomain=None for docs see: FLASK Route.

It's a pretty nice thing to do all the host name matching outside of your code base and in infrastructure like nginx. Giving you the flexibility to have different environments for development and testing etc and then you can forward the hostname as a header which you could extract in a custom manner. Digital Ocean gave a pretty nice intro and there is a great question that shows how to do this: nginx subdomain and domain rewrite w proxy pass

A great advantage of this strategy is that you don't need to bind to port 80 (as root) with your application which gives serious security benefits as well as not worrying about the implementation of SERVER_NAME, which leaves a lot to be desired. See: Why flask can suck.

Upvotes: 6

Related Questions