Search code examples
nginxflaskswaggerswagger-uiflask-restplus

Swagger Nginx flask-restplus


I'm trying to get the Swagger UI from Flask-RESTplus working on a server using Nginx as a proxy.

Swagger served on /api and works locally using http://localhost:5000/api . I'm trying to setup the Nginx as a proxy so I can go to http://ServerIP/api and see the Swagger UI.

I've tried many configurations for Nginx and currently have

location /api {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://localhost:5000;
        proxy_redirect off;
        proxy_intercept_errors on;
        proxy_http_version 1.1;
}

However, I just see a blank page when going to http://ServerIP/api. In the Chrome dev tools there is an error:

Uncaught ReferenceError: SwaggerUIBundle is not defined
at window.onload (api:75)

which refers to:

<script src="/swaggerui/swagger-ui-bundle.js"></script>
<script src="/swaggerui/swagger-ui-standalone-preset.js"></script>
<script type="text/javascript">
    window.onload = function() {
        const ui = window.ui = new SwaggerUIBundle({

But I am able to get to (200 OK, serves up the javascript files) http://ServerIP/swaggerui/swagger-ui-bundle.js http://ServerIP/swaggerui/swagger-ui-standalone-preset.js.

Any ideas what could be the issue?


Solution

  • Similar question: Server response gets cut off half way through

    Nginx needs to have permissions to write to a temp folder when sending large upstream files. Usually you'll see it in the /var/log/nginx/error.log like

    2018/06/28 16:34:48 [crit] ... open() "<tmp folder>/x/y/00000000z" failed (13:
    Permission denied) while reading upstream, <request info...>
    

    to fix this you chown to nginx:nginx (unless you've changed the user manually) eg sudo chown -R nginx:nginx /var/lib/nginx. if the permissions aren't at least 7xx you may have to change that but that shouldn't be an issue.

    alternatively you can set proxy_buffering off; but that isn't recommended if you expect the connections to be longer than usual (speed or size).