当前位置 博文首页 > Python的Flask框架中SERVER_NAME域名项的配置教程

    Python的Flask框架中SERVER_NAME域名项的配置教程

    作者:digwtx 时间:2021-01-31 15:00

    Flask中的SERVER_NAME主要做两件事:

    • 协助Flask在活动的请求(request)之外生成绝对URL(比如邮件中嵌入网站URL)
    • 用于子域名支持

    很多人误以为它可以做这两件事之外的其它事情。

    一、第一件事:绝对URL
    我们知道,url_for默认情况下是生成相对URL,它有个参数_external,如果设置为真,则会生成一个绝对URL(就是HTTP开头带域名等信息的)。若不指定SERVER_NAME,默认使用当前活动的请求(request)来生成URL。

    下面举个例子演示一下:

    # filename myapp.py
    from flask import Flask, url_for
    
    app = Flask(__name__)
    
    @app.route('/')
    def index():
     return 'hello flask'
    
    @app.route('/test')
    def test():
     return url_for('index', _external=True)
    
    if __name__ == '__main__':
     app.run(debug=True)
    
    

    1.【情景1】通过浏览器访问

    app运行之后,在本地5000端口监听。

    (env) F:\tmp>python myapp.py
     * Running on http://127.0.0.1:5000/
     * Restarting with reloader
    

    若我们通过浏览器访问http://127.0.0.1:5000/test,则返回的内容是:http://127.0.0.1:5000/。

    若我们通过浏览器访问http://localhost:5000/test,则返回的内容是:http://localhost:5000/。

    可以看出,在未设置SERVER_NAME的情况下,url_for生成的绝对URL是依赖于请求的URL的。下面我们来看看不通过浏览器访问的情况。

    2.【情景2】非浏览器访问

    这个情景是指不存在request请求的情况。

    我们通过Python Shell来模拟:

    >>> from myapp import app
    >>> with app.app_context():
    ... print url_for('index', _external=True)
    ...
    
    Traceback (most recent call last):
     File "<stdin>", line 2, in <module>
     File "F:\tmp\env\lib\site-packages\flask\helpers.py", line 287, in url_for
     raise RuntimeError('Application was not able to create a URL '
    RuntimeError: Application was not able to create a URL adapter for request indep
    endent URL generation. You might be able to fix this by setting the SERVER_NAME
    config variable.
    

    上面的意思是说应用程序不能创建一个用于与request不相关的URL生成的URL适配器,可以通过设置SERVER_NAME来解决这个问题。

    好,下面我们为SERVER_NAME设置一个值之后再试试:

    >>> app.config['SERVER_NAME'] = 'example.com'
    >>> with app.app_context():
    ... print url_for('index', _external=True)
    ...
    
    http://example.com/
    

    PS: 一般SERVER_NAME设置为网站的域名。

    在Flask-Mail相关的文章中有这么一段话:

    许多Flask的扩展都是假定自己运行在一个活动的应用和请求上下文中,Flask-Mail的send函数使用到current_app这个上下文了,所以当mail.send()函数在一个线程中执行的时候需要人为的创建一个上下文,所有在send_async_email中使用了app.app_context()来创建一个上下文。

    因此,若要生成不依赖于request的绝对URL(比如异步发送邮件时在邮件中生成网站某个页面的URL),就必须要设置SERVER_NAME。

    二、第二件事:子域名支持
    SERVER_NAME键是用于子域名支持。因为 Flask 在得知现有服务器名之前不能猜测出子域名部分,所以如果你想使用子域名,这个选项必要的,并且也用于会话cookie。

    请牢记不只有 Flask 存在不知道子域名的问题,你的浏览器同样存在这样的问题。 大多数现代 web 浏览器不允许服务器名不含有点的跨子域名 cookie。因此如果你的服务器的 名称为 localhost,你将不能为 localhost 和所有它的子域名设置一个 cookie。 请选择一个合适的服务器名,像 'myapplication.local', 并添加你想要的服务器名 + 子域名 到你的 host 配置或设置一个本地 bind。
    Examples

       -------->http://book.muxistudio.com
       ||
    http://muxistudio.com-------->http://blog.muxistudio.com
       ||
       -------->http://share.muxistudio.com
    

    1.本地测试

    修改 /etc/hosts 文件

    注意:仅在本地测试中有效!

    将所有需要使用的子域名添加到其中,例:

    127.0.0.1 flask.dev  localhost # 域名
    127.0.0.1 test.flask.dev localhost # 子域名
    127.0.0.1 othertest.flask.dev localhost # 子域名
    

    在Flask应用的配置文件中添加'SERVER_NAME'

    在应用配置中将'SERVER_NAME'设置为指定的域名及默认监听的端口,例:

    #...
    app = Flask(__name__)
    app.config['SERVER_NAME'] = 'flask.dev:5000'
    #...
    
    

    2.配置蓝图

    蓝图中的subdomain为hosts文件中所添加的子域名

    #...
    # Blueprint declaration
    bp = Blueprint('subdomain', __name__, subdomain="<user>")
    
    #...
    
    # Register the blueprint into the application
    app.register_blueprint(bp)
    #...
    
    

    3.服务器端配置

    讲Flask应用设置中的'SERVER_NAME'修改为生产环境中注册的域名

    flask.dev:5000 ----> muxistudio.com
    
    

    4.Nginx配置

    配置监听端口,下面的例子中使用正则表达式获取用户访问的子域名,对于www,应该在正则表达式获取时将其过滤,在用户访问时对其进行重定向至www.yourdomain.com页面,否则www将会被视为子域名。

    配置实例:

     server {
     listen 80;
     listen 443 ssl;
    
     ssl_certificate /usr/local/nginx/ssl/nginx.crt;
     ssl_certificate_key /usr/local/nginx/ssl/nginx.key;
    
     server_name ~^www\.(?<user>.+\.)?markdownblog\.com$;
     return 301 "$scheme://${user}markdownblog.com$request_uri";
     }
    
     server {
     listen 80;
     listen 443 ssl;
    
     ssl_certificate /usr/local/nginx/ssl/nginx.crt;
     ssl_certificate_key /usr/local/nginx/ssl/nginx.key;
    
     server_name ~^.+\.markdownblog\.com$ markdownblog.com;
    
     location / {
      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass http://127.0.0.1:8085;
     }
    }
    

    js
    下一篇:没有了