后端代码配置
#在StartUp.cs文件中Configure方法中增加以下代码
app.UseSwagger(c =>
{
//代理路径访问
c.PreSerializeFilters.Add((doc, item) =>
{
//根据代理服务器提供的协议、地址和路由,生成api文档服务地址
doc.Servers = new List<OpenApiServer>
{
new OpenApiServer
{
//此处等同于设置接口访问路径前缀
//以192.168.1.33:5001/User/Login接口为例,以下代码生成内容为
//http://192.168.1.33:50001/oaapi/User/Login
//item.Scheme表示协议类型
//item.Host.Value表示IP地址/域名
//item.Headers["X-Forwarded-Port"]为nginx中配置的proxy_set_header X-Forwarded-Port $server_port内容
//item.Headers["X-Forwarded-Prefix"]为nginx中配置的proxy_set_header X-Forwarded-Prefix oaapi内容
//如果nginx配置的请求头中存在X-Forwarded-Port,则增加X-Forwarded-Port端口内容,否则则不增加
//如果nginx配置的请求头中存在X-Forwarded-Prefix,则增加X-Forwarded-Prefix虚拟路径内容,否则则不增加
Url = @$"{item.Scheme}://{item.Host.Value}
{(string.IsNullOrEmpty(item.Headers["X-Forwarded-Port"].ToString() ) ? "":":" + item.Headers["X-Forwarded-Port"])}
{ (string.IsNullOrEmpty(item.Headers["X-Forwarded-Prefix"].ToString()) ? "" : "/" + item.Headers["X-Forwarded-Prefix"])}"
}
};
});
//固定路径访问
//c.RouteTemplate = "api-doc/{documentName}/swagger.json";
});
app.UseSwaggerUI(c =>
{
//代理路径访问
c.ShowExtensions();
c.ValidatorUrl(null);
//GlobalContext.SystemConfig.VirtualDirectory为appsettings中设置的VirtualDirectory内容,如无代理则设置改字段为空
c.SwaggerEndpoint($"{GlobalContext.SystemConfig.VirtualDirectory}/swagger/{"v1"}/swagger.json", "和田玖信OA办公系统 Api v1");
c.RoutePrefix = string.Empty;
c.DocExpansion(DocExpansion.None);
//固定路径访问
//c.RoutePrefix = "api-doc";
//c.SwaggerEndpoint("swagger/v1/swagger.json", "和田玖信OA办公系统 Api v1");
});
Nginx配置 文章来源:https://www.uudwc.com/A/Nx5Zg/
#nginx代理设置
server {
listen 50001;#代理后访问端口
server_name 192.168.1.33;#服务器地址(IP/域名)
location /oaapi/ {
proxy_pass http://192.168.1.33:5001/;#需要代理的地址
#配置代理header(请求头)内容
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-Prefix oaapi; #------其中oaapi可以更换任何路由,即为代理路径内容
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
文章来源地址https://www.uudwc.com/A/Nx5Zg/