3

I have the following setup:

  • Main Server:
    nginx forwarding "https://example.com/machine1/*" -> "http://machine1/*"
  • Machine1:
    ngnix + uwsgi + flask mounting at /, e.g. /foobar.

That means, https://example.com/machine1/foobar will hit the Machine1 as /foobar. I have the mounting point /machine1 in an variable, NGINX_MOUNT_URL.

Problem is, the url_for does not resolves that.

I tried

app.config["APPLICATION_ROOT"] = NGINX_MOUNT_URL   # = "/machine1"

but it seems that only applies to cookie urls.

Have a simple view:

@app.route("/foobar")
def web_foobar():
    from flask import url_for
    return "The URL for this page is {}".format(url_for("web_foobar"))
# end def

So, browsing https://example.com/machine1/foobar (Main Server) will get proxied to http://machine1/foobar (Machine1) which displays The URL for this page is /foobar. I need it to be /machine1/foobar, for linking in templates to work.

I tried the offered solutions in https://stackoverflow.com/a/18967744/3423324, but it always ended up that I had to browse https://example.com/machine1/machine1/foobar to even get the page /foobar.

How to make flask.url_for aware of APPLICATION_ROOT?

Community
  • 1
  • 1
luckydonald
  • 5,976
  • 4
  • 38
  • 58
  • This is an old question, but on the off chance that someone else turns up, one way that I achieved this was by passing a `url_prefix='/machine1'` when registering the service module (`register_module` was later renamed to [`register_blueprint`](https://github.com/pallets/flask/blob/71b7c4f5f89ea5e95dc7881797ca7d3a06b97e2f/flask/app.py#L955) it seems). – Bahrom May 03 '17 at 19:52
  • I later used https://stackoverflow.com/a/47155246/3423324 – luckydonald Mar 01 '18 at 15:43

1 Answers1

-1

Have a look at How do I get the different parts of a Flask request's url?

The path and script_root request variables look correct for what you need.

Community
  • 1
  • 1
cyberspy
  • 1,023
  • 11
  • 13
  • The problem is for the template engine missing `/machine1` in front of the url when generating it with `url_for`. – luckydonald Dec 07 '16 at 15:50