-
-
Notifications
You must be signed in to change notification settings - Fork 16k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
url_for produces relative links with nontrivial APPLICATION_ROOT #3219
Comments
app.config["SERVER_NAME"] = "example.com"
app.config["APPLICATION_ROOT"] = "/app"
with app.app_context():
print(url_for("static", filename="test.txt", _external=True))
# http://example.com/app/static/test.txt If you're using Gunicorn to serve a Flask app under a non-root path, you need to configure Gunicorn to set the WSGI
If you're using the dev server, you can use
from werkzeug.exceptions import NotFound
from werkzeug.wsgi import DispatcherMiddleware
from myproject import app
app.wsgi_app = DispatcherMiddlware(NotFound(), {"/app": app.wsgi_app})
|
#2759 is open for better documentation about this. |
Not sure if gevent as wsgi server supports |
I'm not sure what part of Gunicorn that is, but it does indeed support |
@davidism I am actually talking about using gevent as standalone wsgi app server (as you allude to, it is more commonly used as a gunicorn worker class): from flask import Flask, redirect, url_for
from gevent.pywsgi import WSGIServer
app = Flask(__name__)
@app.route('/bar')
def hello_world():
return "hi"
@app.route("/favicon.ico")
def favicon():
return redirect(url_for("static", filename="favicon.ico"))
if __name__ == "__main__":
WSGIServer(('127.0.0.1', 5000), application=app).serve_forever() $ SCRIPT_NAME='/foo' python hello.py In this case It is the end of a long day and I am probably not thinking about this the right way, though. |
Oh, that's gevent. This sounds like an issue with gevent then, or with your setup of gunicorn and gevent. An old closed issue on the Flask issue tracker is not the best place to get answers about that. 😉 |
@davidism Please let me give another example here. Say that a template contains <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}"> In this case (using example above) you have |
When Please use Stack Overflow or https://discord.gg/pallets for questions about your own code. This tracker is for issues related to the project itself |
Expected Behavior
url_for
should consistently produce urls which are absolute with regard to the server root, i.e., they should start with a/
.Actual Behavior
When running under gnunicorn with
APPLICATION_ROOT='/myapp/'
, theurl_for
call produces the URLmyapp/some_static_file
, while when running under werkzeug withAPPLICATION_ROOT='/'
, theurl_for
call produces the URL/some_static_file
.This is a problem, because, combined with this werkzeug issue, there's no straightforward way to use
url_for
in templates for both development and production:{{ url_for('static', filename='some_static_file') }}
producesmyapp/some_static_file
in production, which fails, while/{{ url_for('static', filename='some_static_file') }}
produces//some_static_file
in development, which fails.Environment
The text was updated successfully, but these errors were encountered: