gpt4 book ai didi

nginx - 如何在 nginx 代理后面使用 Unix 域套接字托管 ASP.NET Core 2.0 (Kestrel)?

转载 作者:行者123 更新时间:2023-12-04 13:22:06 32 4
gpt4 key购买 nike

我目前在 Ubuntu 16 中通过 HTTP 请求在 nginx 后面使用 ASP.NET Core 2.0。

我想切换到 Unix 域套接字。

在我的 Program.cs 我有:

var host = default(IWebHost);
var builder = new WebHostBuilder()
.UseKestrel(opt =>
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && settings.Config.ListenUnixSocket)
{
opt.ListenUnixSocket("/tmp/api.sock");
}
})
.Configure(app =>
{
app.Map("/health", b => b.Run(async context =>
{
context.Response.StatusCode = (int)HttpStatusCode.OK;
await context.Response.WriteAsync("Ok");
}));
});

if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || !settings.Config.ListenUnixSocket)
{
host = builder.UseUrls("http://0.0.0.0:5501").Build();
}
else
{
host = builder.Build();
}

host.Run();

并且,在 Nginx:

location /health {
#proxy_pass http://127.0.0.1:5501;
proxy_pass http://unix:/tmp/api.sock:/;
}

使用默认的 TCP 套接字运行它是可行的,但是切换到 Unix 域套接字时,我收到了 502 错误。

我需要 nginx 的任何特定模块吗?我做错了什么?

最佳答案

Aspnetcore 在运行时会创建 api.socket 但 Nginx 必须有写入权限。

所以,如果你不知道nginx使用的是什么用户,执行:

ps aux | grep nginx

你会在终端得到这样的东西:

root      5005  0.0  0.2 125116  1460 ?        Ss   20:12   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 5006 0.0 0.6 125440 3260 ? S 20:12 0:00 nginx: worker process
root 5173 0.0 0.1 14516 920 pts/0 S+ 20:17 0:00 grep --color=auto nginx

然后设置权限:

sudo chown www-data:www-data /tmp/api.sock

就是这样!

关于nginx - 如何在 nginx 代理后面使用 Unix 域套接字托管 ASP.NET Core 2.0 (Kestrel)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49905724/

32 4 0