[英]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.我使用 net core 3.1 和 angular 在 Nginx 上构建网站。 (Ubuntu 18.04). (Ubuntu 18.04)。
But when I try use swaggerUI, it can't normal display.但是当我尝试使用 swaggerUI 时,它无法正常显示。 The screen is blank.屏幕是空白的。
Debug tools report some files can't found (404).调试工具报告找不到某些文件 (404)。
My swagger.json and html can loading, but seem js/css file can't loading.我的 swagger.json 和 html 可以加载,但似乎 js/css 文件无法加载。
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 Nginx 文件夹结构
var
|
|-->www Angular Flies(index/js/css)
|
|-->api Net Core Files
Nginx config 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;
}
}
Swagger setting on 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();
I run this on my local computer not problem, only publish on nginx has this issue.我在本地计算机上运行这个没有问题,只在 nginx 上发布有这个问题。 How can I modify config make swaggerUI work?如何修改配置使 swaggerUI 工作?
Ok, I found problem and fix this issue.好的,我发现了问题并解决了这个问题。
Nginx config has wrong. Nginx 配置错误。
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. angular 网站无法运行,无法加载 js/css 文件。
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.那是我的最后一个配置,我检查了 SwaggerUI 和网站可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.