Reputation: 131
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
Kindly refer to the below image to know how a request is sent from Swagger
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:
Upvotes: 3
Views: 3973
Reputation: 21
I personally think its something to do with Https or Http request, i ran into the same issue a couple of days ago and for me the simple solution was to change the scheme from http when making requests using my local set up and to https ( on live server ) since i had installed ssl and only secure traffic was allowed.
Upvotes: 0
Reputation: 131
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
Upvotes: 2
Reputation: 323
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/;
}
Upvotes: 7