【发布时间】:2020-08-05 09:22:17
【问题描述】:
我在 Nginx 上使用了 net core 3.1 和 angular 构建网站。 (Ubuntu 18.04)。
但是当我尝试使用 swaggerUI 时,它无法正常显示。屏幕是空白的。
调试工具报告找不到某些文件 (404)。
我的 swagger.json 和 html 可以加载,但似乎 js/css 文件无法加载。
浏览器开发工具
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 文件夹结构
var
|
|-->www Angular Flies(index/js/css)
|
|-->api Net Core Files
Nginx 配置
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;
}
}
Net core 上的 Swagger 设置。
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();
我在本地计算机上运行这个没有问题,只有在 nginx 上发布才有这个问题。 如何修改配置使 swaggerUI 工作?
【问题讨论】:
标签: c# nginx asp.net-core-webapi asp.net-core-3.1 nginx-reverse-proxy