Configure multiple subdomains in the flask framework of Python

Source: Internet
Author: User
Tags sub domain subdomain to domain
Flask Sub Domain

Generally used for a small number of sub-domain names, a module corresponding to a subdomain. Let's take a look at the following example:

modules.py:

From flask Import blueprintpublic = Blueprint (' public ', __name__) @public. Route ('/') def Home ():  return ' Hello ' Flask ' App.py:app = Flask (__name__) app.config[' server_name '] = ' example.com ' from modules import Publicapp.register_ Blueprint (Public, subdomain= ' public ')

The public module can now be accessed through public.example.com/.

Wildcard domain
Wildcard domain, which is a module that matches many subdomains. For example, some websites provide a personalized domain name function, this is the form.

Let's look at the sample code for the snippet:

modules.py:

From flask Import Blueprintmember = Blueprint (' member ', __name__) @member. Route ('/') def Home ():  return G.subdomainapp.py:app = Flask (__name__) app.config[' server_name '] = ' example.com ' from modules import Memberapp.register_blueprint (Member, subdomain= ' <subdomain> ')

This code and the previous section of the image, the difference is that the subdomain used in the dynamic parameters <subdomain> (URL variables in the route is also this way). We can use this parameter to get the associated user from the combined URL processor that was used before the callback function was requested. This allows us to access the member module in the form of *.example.com.

Here are the handy functions for adding sub-domain support for any flask or Blueprint object:

def add_subdomain_to_global (endpoint, values):  g.subdomain = Values.pop (' subdomain ', None) def add_subdomain_to_ Url_params (endpoint, values):  if not ' subdomain ' in values:    values[' subdomain '] = g.subdomaindef add_subdomain _support (APP):  app.url_value_preprocessor (Add_subdomain_to_global)  app.url_defaults (add_subdomain_to_ Url_params)

You can then use the Before_request callback function to process the subdomain:

Add_subdomain_support (Blueprint) @blueprint. Before_requestdef Add_user_to_global ():  g.user = None  If G.subdomain:    g.user = User.query.filter_by (Username=g.subdomain). first_or_404 ()

Note: Please change the blueprint to the actual object.

Special Note: The wildcard domain debugging is not inconvenient, you need to do a pan-domain name resolution. Modifying the Hosts file to specify the domain name method is not feasible (sub-domain name can be added one after the other, more sub-domain name is not too realistic). When native debugging, you can install DNS server (such as Linux bind service, etc.), and do the pan domain name resolution, and then Debug. Of course, the use of public domain name and server to debug also can not be.

Ps:
1. If a blueprint requires a utility a.domain.com by default, then when defining Blueprint:

A=blueprint (' A ', __name__,subdomain= ' a ')

In this way, all the URLs under the BP routing are going to be a.domain.com/xxx.

2. In the case of a specific URL routing definition, if practical a.domain.com is required, then write:

@www. Route ('/hello ', methods=[' GET ', ' POST '],subdomain= ' a ') def xxx (): .....

3. In my specific practice, the default routing are to go to www, which is in __init__.py:

app.url_map.default_subdomain= ' www '

In fact, is to set the default sub-domain name, so the default is not set, the route is the www.
At this time if access to domain.com, that is, without the WWW, will be reported 404, how to do it, I was in the nginx level to solve the problem, in nginx.conf add a server:

server {server_name Domain.com;rewrite ^ (. *) http://www.domain.com$1 Permanent;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.