如何在Nginx代理后面的Unix域套接字中托管ASP.NET Core 2.0(Kestrel)? [英] How to host ASP.NET Core 2.0 (Kestrel) with Unix domain socket behind a nginx proxy?

查看:34
本文介绍了如何在Nginx代理后面的Unix域套接字中托管ASP.NET Core 2.0(Kestrel)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am current using ASP.NET Core 2.0 behind nginx through HTTP requests in Ubuntu 16.

我想切换到Unix域套接字.

And I'd like to switch to Unix domain socket.

在我的 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错误.

Running it using the default TCP socket works, but switching to Unix domain sockets, I got a 502 error.

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

Do I need any specific module at nginx? What I am doing wrong?

推荐答案

Aspnetcore在运行时将创建api.socket,但Nginx必须具有写权限.

Aspnetcore will create api.socket when its running but Nginx must have permission to write.

因此,如果您不知道nginx用户使用什么,请执行:

So, if you don't know what user nginx uses, execute:

ps aux | grep nginx

您将在终端中得到一些东西

You'll get something this in the terminal:

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代理后面的Unix域套接字中托管ASP.NET Core 2.0(Kestrel)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆