在 Debian 上跑 Swagger(一般指的是 Swagger UI / swagger-editor,或者后端集成 springfox、knife4j、fastapi 的 docs 等)“慢”通常不是 Debian 本身的问题,而是网络、依赖、配置或运行方式导致的。下面按常见场景给你一套排查 + 优化方案。
swagger-ui-bundle.jsswagger-ui.cssvalidator-urlSwagger UI 默认从 unpkg / jsdelivr / cdn 加载前端资源,Debian 服务器或浏览器访问外网慢。
使用本地 Swagger UI
# 安装 nginx
apt update
apt install -y nginx
# 下载 swagger-ui
wget https://github.com/swagger-api/swagger-ui/archive/refs/tags/v5.x.x.zip
unzip v5.x.x.zip
cp -r swagger-ui-5.x.x/dist /var/www/swagger-ui
修改 /var/www/swagger-ui/swagger-initializer.js:
url: "http://你的IP/api-docs",
Nginx 配置:
server {
listen 80;
root /var/www/swagger-ui;
index index.html;
}
✅ 效果:秒开
validatorUrlspringfox:
documentation:
swagger:
v2:
path: /api-docs
或(knife4j):
knife4j:
enable: true
setting:
language: zh-CN
@EnableSwagger2
@Configuration
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.xxx.controller"))
.paths(PathSelectors.any())
.build();
}
}
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
</dependency>
/docs 默认从 CDN 加载from fastapi import FastAPI
from fastapi.openapi.docs import get_swagger_ui_html
app = FastAPI(docs_url=None)
@app.get("/docs", include_in_schema=False)
async def custom_swagger_ui():
return get_swagger_ui_html(
openapi_url="/openapi.json",
title="API",
swagger_js_url="/static/swagger-ui-bundle.js",
swagger_css_url="/static/swagger-ui.css",
)
把 swagger-ui 文件放本地。
top
free -h
df -h
curl -I https://unpkg.com
ping unpkg.com
# apt 镜像
sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list
Debian 跑 Swagger 慢,99% 是“前端资源走外网”或“文档实时生成”导致的。
✅ 最稳方案:
如果你能告诉我:
我可以直接给你针对性配置。