I am developing a Flask app running on a sub-route of a server.
Here's a minimal version of the app:
from flask import *
app = Flask(__name__, root_path="/test")
app.config["APPLICATION_ROOT"] = "/test"
@app.route("/")
@app.route("/index")
def index():
return url_for("index")
@app.route("/test")
def test():
return url_for("test")
if __name__ == "__main__":
app.run(port=5000, debug=True)
Here's the relevant part of /etc/nginx/nginx.conf
:
http {
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
location /test {
return 302 /test/;
}
location /test/ {
proxy_pass http://127.0.0.1:5000/;
}
}
}
This way, when I visit http://localhost/test/, the Flask application will get an incoming request at /
, as proxied by nginx. This is configured at nginx level and cannot be changed, so what the application receives will always have the /test
prefix truncated.
However I need url_for
to generate URLs with that prefix. I am currently seeing /index
at /test/
, but what I expect is /test/index
.
As shown in the code, APPLICATION_ROOT
didn't work for me.
I have latest Flask (1.0.2) and dependencies.