• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >ruby > Python的Flask框架中SERVER_NAME域名项的配置教程

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

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python flask教程,python flask,python3 flask,python3 flask教程,python flask框架等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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)

</div>

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

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

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

若我们通过浏览器访问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)
...
</div>
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.
</div>

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

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

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

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
</div>

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 # 子域名
</div>

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

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

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

</div>

2.配置蓝图

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

#...
# Blueprint declaration
bp = Blueprint('subdomain', __name__, subdomain="<user>")

#...

# Register the blueprint into the application
app.register_blueprint(bp)
#...

</div>

3.服务器端配置

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

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

</div>

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;
 }
}
</div>

</div>

您可能想查找下面的文章:

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

相关文章

  • 初步讲解Ruby编程中的多线程
  • Ruby中创建字符串的一些技巧小结
  • Ruby中使用多线程队列(Queue)实现下载博客文章保存到本地文件
  • 浅谈Ruby on Rails的国际化
  • 详解Ruby中的代码块对象Proc
  • ruby 面向对象思维 概念
  • Ruby中使用mechanize批量下载校内网相册照片
  • 初步了解一下什么是ruby
  • Ruby rails 页面跳转(render和redirect_to)
  • rails "No route matches" 错误的解决方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • ruby安装gem包失败的通用解决方法
    • Ruby 多线程的潜力和弱点分析
    • Ruby中的Socket编程简单入门
    • Ruby元编程之梦中情人method_missing方法详解
    • Ruby升级后no such file to load -- readline解决办法
    • Ruby中的public、private、protected区别小结
    • 详解Lua中的表的概念及其相关操作方法
    • Ruby中的block代码块学习教程
    • Ruby常量查找路径问题深入研究
    • ruby 正则表达式详解及示例代码

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有