简体   繁体   中英

Swagger can't display and get not found, .Net Core 3.1 + Angular on Nginx

I used net core 3.1 and angular build website on Nginx. (Ubuntu 18.04).

But when I try use swaggerUI, it can't normal display. The screen is blank.

Debug tools report some files can't found (404).

My swagger.json and html can loading, but seem js/css file can't loading.

Browser dev tool

Failed to load resource: the server responded with a status of 404 (Not Found)
swagger-ui-bundle.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
swagger-ui-standalone-preset.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
index.html:66 Uncaught ReferenceError: SwaggerUIBundle is not defined at window.onload (index.html:66)
swagger-ui.css:1 Failed to load resource: the server responded with a status of 404 (Not Found)

Nginx folder structure

var
|
|-->www  Angular Flies(index/js/css)
|
|-->api  Net Core Files

Nginx config

server {

    listen        80;
    server_name  domainname.com *.domainname.com;

    location / {
        root /var/www/;
        try_files /index.html =404;
    }

    location ~* .(js|jpg|png|css|woff|eot|svg|ttf|ico)$ {
        root /var/www/;
        expires 30d;
    }

    location /api/ {
        proxy_pass         http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }

}

Swagger setting on Net core.

app.UseSwagger(options=>
{
    options.RouteTemplate = "/api/swagger/{documentName}/swagger.json";
});

app.UseSwaggerUI(options =>
{
    options.RoutePrefix = "api";
    foreach (var desc in provider.ApiVersionDescriptions)
        options.SwaggerEndpoint($"swagger/{desc.GroupName}/swagger.json",
        desc.GroupName.ToUpperInvariant());
});
app.UseStaticFiles();

I run this on my local computer not problem, only publish on nginx has this issue. How can I modify config make swaggerUI work?

Ok, I found problem and fix this issue.

Nginx config has wrong.

I comment out this part, and it can work.

location ~* .(js|jpg|png|css|woff|eot|svg|ttf|ico)$ {
        root /var/www/;
        expires 30d;
    }

I found other problem by my answer.

The angular website will not work, it can't loading js/css files.

Finally I found best way to fix this issue.

server {

    listen        80;
    server_name  domainname.com *.domainname.com;

    location /api/ {
        proxy_pass         http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }

    location / {
        root /var/www/;
        try_files /index.html =404;
    }

    location ~* ^/api/.+\.(xml|js|jpg|png|css|html|otf|eot|svg|ttf)$ {
        proxy_pass         http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;

        expires 90d;
    }

    location ~* \.(xml|js|jpg|png|css|html|otf|eot|svg|ttf)$ {
        root /var/www/;
        expires 30d;
        index index.html;
    }
}

That is my last config, and I checked SwaggerUI and Website can work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM