在 Debian 上“优化 Swagger”通常分两种场景:
下面按常见 Debian 生产环境给你一套实用优化方案。
server {
listen 80;
server_name swagger.example.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
}
location ~* \.(js|css|png|jpg|svg)$ {
expires 7d;
add_header Cache-Control "public";
}
}
tryItOut(可选)filter<script>
window.onload = function () {
window.ui = SwaggerUIBundle({
url: "/v3/api-docs",
dom_id: '#swagger-ui',
tryItOutEnabled: false,
filter: false
});
};
</script>
docker run -d -p 8080:8080 \
-e SWAGGER_JSON=/foo/api.yaml \
-v $(pwd):/foo \
swaggerapi/swagger-ui
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.3.0</version>
</dependency>
springdoc:
api-docs:
enabled: false
swagger-ui:
enabled: false
或只在 dev 开启:
@Profile("dev")
@Configuration
public class SwaggerConfig {}
springdoc:
packages-to-scan: com.example.api
paths-to-match: /api/**
swag init --parseDepth 2
if os.Getenv("ENV") != "prod" {
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
}
location /swagger/ {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
}
apt install certbot
certbot --nginx -d swagger.example.com
gzip on;
gzip_types application/json text/css application/javascript;
✅ 不要在公网直接暴露 Swagger
✅ 用 CI 自动生成 API 文档
✅ 大项目用 groupedOpenApi 拆分文档
❌ 不要在生产开启 try it out
如果你能告诉我:
我可以给你更精准的优化方案。