Configure multiple subdomains in the Python Flask framework

Source: Internet
Author: User
Tags subdomain subdomain name
In Fask, you can deploy multiple subdomains by using wildcard subdomains. here we will explain how to configure multiple subdomains in a Python Flask framework. For more information, see Flask subdomain name

It is generally used for a small number of subdomain names. a module corresponds to a subdomain name. 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')

Now you can use public.example.com/to publish a publiclistener.

Wildcard subdomains
Wildcard subdomains, that is, a module is used to match multiple subdomains. For example, some websites provide personalized domain name functions in this form.

Let's take a look at the sample code:

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='
 
  ')
 

The difference between this code and the first section is that subdomain uses dynamic parameters. (The URL variable in the route is also in this way ). We can use this parameter to obtain relevant users by using the combined url processor before the callback function is requested. In this way, we can access the member module in the form of * .example.com.

The following is a convenient function for adding sub-domain names to any Flask or Blueprint objects:

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)

Then you can use the before_request callback function to process subdomain names:

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: replace the blueprint here with the actual object.

Note: Debugging of wildcard subdomains is not very convenient and requires wildcard domain name resolution. It is not feasible to modify the hosts file to specify the domain name. (when there are few sub-domain names, you can add them one by one. if there are more sub-domain names, it is unrealistic ). During local debugging, you can install the DNS server (such as the linux bind service), complete wildcard domain name resolution, and then perform debugging. Of course, the use of public network domain names and servers for debugging is not a problem.

PS:
1. if a blueprint needs to use a.domain.com by default, when defining blueprint:

a=Blueprint(‘a' ,__name__,subdomain='a')

In this way, all the URLs under the bp routing go through a.domain.com/xxx.

2. when defining a specific url routing, if you need to use a.domain.com, write as follows:

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

3. In practice, the default routing is www, which is in _ init _. py:

app.url_map.default_subdomain='www'

In fact, it is to set the default sub-domain name. if this is not set by default, the route goes through www.
At this time, if you access domain.com, that is, without www, 404 will be reported. what should I do? I solve this problem at the nginx layer and add a server in nginx. conf:

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

For more articles about configuring multiple subdomains in the Python Flask framework, refer to the PHP Chinese website!

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.