2

I pass the request to https://mydomain/subserverA/foo to the backend server A through the reverse proxy server and the flask app runs on the server A. The URL is rewritten on the proxy server, the serverA can see the request's URL as https://foo.

So when I use the flask by default and use the url_for("static", "hoge") function, the url becomes "/static/hoge". I want the url to be "/subserverA/static/hoge". This problem can't be solved by only using static_url_path and static_folder options.

I solved this issue by following two codes.

Code A

view = Flask(__name__, static_url_path=‘/subdir/static',static_folder='static')
view.add_url_rule('/static/<filename>', 'static')
print(view.url_map)
## Map([<Rule '/subdir/static/<filename>' (GET, HEAD, OPTIONS) -> static>,
## <Rule '/static/<filename>' (GET, HEAD, OPTIONS) -> static>])

this code works well. But I don't know this is good. That's because I couldn't find the priority rule when the "static" endpoint has multiple url routes. (I found the rule about the normal endpoint.)

Code B

view = Flask(__name__)
static_url = Blueprint('static_url', __name__, url_prefix='/subdir/', static_folder='static')
view.register_blueprint(static_url)

This code uses Blueprint's url prefix. But this makes me write url_for('static_url.static', 'foo'). I want to write in the same way as without a reverse proxy.

Does anyone have a good idea?

Wataru
  • 36
  • 3

0 Answers0