Simk0024
Simk0024

Reputation: 61

Error displaying Swagger UI of flask-restx in Deployment using uwsgi and nginx

I have implemented a flask rest server with swagger-ui using flask-restx. I could get the swagger-ui working when running the server using command, without nginx

flask run --host=0.0.0.0

or

uwsgi --ini app.ini

My app.ini:

[uwsgi]
module = wsgi:app

master = true
processes = 2

socket = /tmp/myproj.sock
chmod-socket = 666
vacuum = true

die-on-term = true

====================

However, with nginx, although my REST APIs are working, I couldn't get the swagger-UI. Error message I received on browser:

enter image description here

My nginx configuration in /etc/nginx/sites-available/default:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    
    server_name _;

    location /api {     
        include uwsgi_params;
        uwsgi_pass unix:/tmp/myproj.sock;
    }
}

Any idea how to configure nginx so that swagger-UI could be loaded? Thank you.

Upvotes: 1

Views: 575

Answers (2)

Arnab Mukherjee
Arnab Mukherjee

Reputation: 302

If you are using flask-swagger-ui module with flask.

Here is the nginx configuration working for me

    # endpoint for showing swagger
    location /api/docs {
            proxy_pass http://localhost:5001/docs/;
    }
    
    # nginx was redirection so added once more location handler(It might be required in your cases)
    location /docs {
            proxy_pass http://localhost:5001/docs/;
    }
    # for getting the resource
    location /static/swagger.yaml {
            proxy_pass http://localhost:5001/static/swagger.yaml;
    }

Its is assumed you kept the swagger file in static directory of the project root. project structure

Upvotes: 0

Simk0024
Simk0024

Reputation: 61

Update

I managed to make it works by adding /swaggerui in nginx configuration.

Example code:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        try_files $uri $uri/ /index.html;
    }
  
  location /api {
    include uwsgi_params;
    uwsgi_pass unix:/tmp/myproj.sock;
  }

  location /swaggerui {
    include uwsgi_params;
    uwsgi_pass unix:/tmp/myproj.sock;
  }

}

Upvotes: 1

Related Questions