flask-openapi3 icon indicating copy to clipboard operation
flask-openapi3 copied to clipboard

Path issues with Apache WSGI Module

Open marcusmohr opened this issue 9 months ago • 3 comments

Hey there, we are currently creating a Flask API and have the issue that the WSGI Script Alias from the Apache vHost configuration is only respected by the library in certain cases.

This is what we have configured in Apache: WSGIScriptAlias /flask_api "/var/www/Flask_API/wsgi.py"

For the URL to the docs, the alias is applied correctly: https://<server-url>/flask_api/openapi/

However, when we create the "search" endpoint, the WSGI alias is ignored, and the path looks like this: https://<server-url>/search

We need "search" to be available under the following path: https://<server-url>/flask_api/search

Our current code:

Flask_API_Blueprint = APIBlueprint("Flask_API", __name__, url_prefix="/", abp_tags=[Flask_API_tag])
[...]
@Flask_API_Blueprint.get("/search",
                         strict_slashes=False,
                         summary="Returns data",
                         responses={200: SearchResult})

When we set the Endpoint to @Flask_API_Blueprint.get("/flask_api/search/", the path in swagger doc is shown correct. But we get the following Error in response:

{
  "error": "Resource not found"
}

We also have already tried changing the url_prefix to "/flask_api", but then the URL to the docs results in the following path: https://<server-url>/flask_api/flask_api/openapi/

What settings do we need to apply so that the "search" endpoint also respects the WSGIScriptAlias? Is this a bug? We appreciate your help.

I need to point out that we currently have to use version 2.5.5 because we are using Debian, and the dependencies in the Debian repository are very outdated. However, I couldn't see any changes related to this in the changelog.

marcusmohr avatar Mar 07 '25 08:03 marcusmohr

This issue is somewhat similar to #206. but by default, Apache will strip off /flask_api from /flask_api/search, leaving only /search when passed to Flask.

Here are two solutions to address the issue:

WSGIScriptAliasMatch ^/flask_api(.*) /var/www/Flask_API/wsgi.py$1

WSGIScriptAlias /flask_api /var/www/Flask_API/wsgi.py
SetEnv SCRIPT_NAME /flask_api

then you can use @Flask_API_Blueprint.get("/flask_api/search/".

luolingchun avatar Mar 10 '25 02:03 luolingchun

Thanks for your quick response. Unfortunately, the issue persists. The first solution has no effect, while the second fixes the path in the Swagger documentation but adds an extra '/flask_api' to the data retrieval path, e.g.

Path as it appears in the Swagger documentation: /flask_api/search

"Real" path to retrieve the data: /flask_api/flask_api/search

For me it seems to be a bug.

marcusmohr avatar Mar 10 '25 09:03 marcusmohr

Actually, I don't quite understand Apache. There are related configurations in nginx that can achieve not stripping /flask_api, you can check if there are similar functions in Apache's configurations.

This problem is not something Flask can solve.

luolingchun avatar Mar 12 '25 01:03 luolingchun