3

I am running Django with Nginx in 8000 port. Here Nginx is exposed outside in 8000 port. But in Swagger UI the base_url is set as without port. So Swagger is trying to access endpoints without a port, then it gives a response as TypeError: Failed to fetch.

Kindly refer to the below image to know how base_url is set enter image description here

Kindly refer to the below image to know how a request is sent from Swagger enter image description here

When I access the endpoint (with port) using Postman it is working fine. The problem occurs only when I try using Swagger (because is sent a request without port).

Here I want Swagger to sent all the requests with the port. Please help me to resolve this issue. Thanks.

Note:

  1. I'm using the drf-yasg==1.17.1 package to generate Swagger UI
  2. Other requirements are Django==3.0.3 and djangorestframework==3.11.0

3 Answers3

7

You need to tell the Nginx to forward the server port as well. As swager gets the base url port from request header (if not hardcoded as mtshaikh proposed).

eg. something like this this should be in Nginx config

location / {
            proxy_set_header Host $host:$server_port;
    proxy_pass http://example.com:8000/;
}
Petr Synek
  • 323
  • 2
  • 7
1

Not a good solution but defining the complete url in the get_schema_view() in urls.py works!

schema_view = get_schema_view(
    openapi.Info( ... ),
    url='http://example.net:8080/', # Important bit
    public=True,
    permission_classes=(permissions.AllowAny,)
)

Source

mtshaikh
  • 618
  • 4
  • 16
1

Thanks, @Petr Synek for your answer

We need to tell the Nginx to forward the http_host.

location / {
     proxy_set_header Host $http_host;
}

This one resolved the above-mentioned issue