4

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/

robbles
  • 2,729
  • 1
  • 23
  • 30
  • Why do you want to do this? What about putting `127.0.0.1 mydomain.com` in `/etc/hosts`? – Alex Hall May 29 '16 at 22:57
  • @AlexHall I have a locally running daemon that doesn't know the hostname, and needs to make requests to the Flask app. `/etc/hosts` won't work in this case because the daemon will specifically make requests to `127.0.0.1:80`. – robbles May 30 '16 at 17:27
  • @AlexHall at this point I have a workaround for the specific problem I'm facing, but I would like to know a solution for this issue in case it comes up again. – robbles May 30 '16 at 17:28

1 Answers1

6

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.

Community
  • 1
  • 1
Luke Exton
  • 3,506
  • 2
  • 19
  • 33
  • 1
    I don't think setting `subdomain=None` is enough to avoid the requirement that SERVER_NAME must match the base domain. But I'll double-check to be sure. – robbles Jun 02 '16 at 18:55
  • You have a good point with NGINX being a better way to handle host matching and routing for subdomains, etc. That's probably the workaround I'll end up using here. – robbles Jun 02 '16 at 18:56
  • I am assuming that the default_subdomain is inherited from the blueprint you are loading. In the Werkzueg docs, it says: "The subdomain rule string for this rule. If not specified the rule only matches for the default_subdomain of the map. If the map is not bound to a subdomain this feature is disabled." So I hope it should be enough to get by, but yes I do definately prefer nginx/apache for this sort of thing. – Luke Exton Jun 02 '16 at 20:58