-
Notifications
You must be signed in to change notification settings - Fork 265
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
SpectacularSwaggerView behind a proxy #211
Comments
Hi @maximebel, ahh a proxy, i see. so first of all, all URLs should be relative, but i guess this is not sufficient in your case. i'm assuming you have some kind of prefix from your proxy. if thats not the case, can you make a (anonymized) example to make the analysis easier? that would mean you are already using the i guess that other feature request generated more problems than it solved. |
Hello yes you're right but it is not enough. The first issue is with the <script src="{{script_url|safe}}"></script> in the template. script_url is request.get_full_path() which is not using FORCE_SCRIPT_NAME (or anything else) so the generated url lacks the proxy prefix. |
I have the same problem, urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('accounts.urls')),
path('upc/', include('upc.urls')),
path('schema/', SpectacularAPIView.as_view(), name='schema'),
path('docs/', SpectacularSwaggerView.as_view(template_name="swagger-ui.html", url_name="schema"), name="swagger-ui"),
] For backend endpoints was added /api prefix in nginx upstream backend {
server backend:8000;
}
server {
listen 80;
gzip on;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
location /static/ {
alias /var/www/static/;
autoindex on;
}
location /api {
rewrite /api/(.*) /$1 break;
proxy_pass http://backend;
}
} In Swagger Api calls doesn't have /api prefix |
@maximebel your problem should be fixed now as regarding your second point. @ronek22 your problem is a separate but related one i believe. @maximebel was not referring to a missing prefix in the schema, but a broken swagger UI. your problem is that django needs to be aware that it is prefixed. SPECTACULAR_SETTINGS = {
'SERVERS': [{'url': 'http://localhost:8081/api/'}],
...
} then Swagger UI should probably work as expected. |
this looks resolved to me. feel free to comment if not |
I'm facing a similar issue. I'm serving my backend through Nginx reverse proxy. The problem is when I try to hit |
If django does not know about your path, it cannot
You need to make Django aware of your routing as mentioned above: https://docs.djangoproject.com/en/4.2/ref/settings/#force-script-name |
Thanks for that! But now I get this when I hit /schema/swagger-ui:
|
Hello
SpectacularSwaggerView is using request.get_full_path() but it is not enough I think for servers behind a proxy.
It should either rely on x_forwarded headers or integrate FORCE_SCRIPT_NAME if defined.
My current issue is that Swagger can't load the script because the generated path is not complete.
No issue for redoc though
Regards
The text was updated successfully, but these errors were encountered: