我有一个在端口5000上运行的Flask应用程序.我的服务器管理员已配置nginx将其转发到端口5001(对不起,如果我有错误的术语:Flask应用程序在端口5000上运行,但应用程序可公开访问http://the_url:5001).
在浏览器中直接访问的所有路由都可以正常工作,但是使用任何重定向url_for()似乎都会导致URL丢失端口 - 即redirect(url_for('index'))重定向到http://the_url/而不是http://the_url:5001/(@app.route("/")触发函数的地方index()).
如何确保Flask在重定向时添加正确的端口?如果我将默认端口更改为5001,nginx配置将无法正常工作,因为它期望应用程序在端口5000上运行?
我在 Heroku 上部署了一个应用程序,但问题是当我的应用程序发送电子邮件时,它没有在 URL 中附加我的服务器的名称:
content = Content("text/html", verification_email.format(user["first_name"],
url_for("register.display_register_form",
token=token.decode("utf-8"), external=True)))
Run Code Online (Sandbox Code Playgroud)
但是我在电子邮件中收到的链接是:
http:///register_account/DnsJpXw_QIcPYeDHEg_fipB2kRiJBUj2RI6I9cI4Yl4w6K9ohbZRMVqBInuV0aOsBT4Zqt69X8MfhNfnys4s-DAQmgu1OPBwmSQnzAELvdcCyiZtkJGSY8_dQ799FOewtBDkvqR1D8XHmvVxgaVqbwSjdEBnvFsBBHMQCic%3D/verify?external=True
Run Code Online (Sandbox Code Playgroud)
我对这个 URL 有问题:
///当服务器名称为空时,服务器名称才不会出现在 URL 中我该怎么做才能获得正确的 URL https://my-server-name/register_account...?
编辑
我尝试在我的 config.py 文件中设置以下变量:
SERVER_NAME = " http://my-server-58140.herokuapp.com "
它在我的路径中生成了错误,例如我无法访问任何 URL,之前可以访问以下 URL,但在定义我的SERVER_NAME时不再访问它:
http://my-server-58140.herokuapp.com/home
Run Code Online (Sandbox Code Playgroud)
编辑
我的烧瓶应用程序已配置:
SERVER_NAME = os.environ.get('SERVER_NAME')
DEBUG = True
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
Run Code Online (Sandbox Code Playgroud)
在0.0.0.0:5000我的本地主机和:my-server-58140.herokuapp.com在我的生产服务器上将环境变量设置为
假设我有一个资源,它不执行任何操作,但将 url 返回到控制台
from app import api
class StaticFiles(Resource):
def get(self):
return api.url_for(self) # this only provides the resource url
Run Code Online (Sandbox Code Playgroud)
如果请求是http://localhost:5000/static上面的代码返回 /static 我正在寻找http://localhost:5000/static
通常我使用请求,但资源中没有请求。我正在寻找几乎相当于request.base_url