如何在Nginx代理后面的Unix域套接字中托管ASP.NET Core 2.0(Kestrel)?

编程入门 行业动态 更新时间:2024-10-12 01:22:26
本文介绍了如何在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("0.0.0.0:5501").Build(); } else { host = builder.Build(); } host.Run();

然后,在 Nginx :

location /health { #proxy_pass 127.0.0.1:5501; proxy_pass 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)?

本文发布于:2023-11-14 17:50:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1588141.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Unix   Nginx   ASP   Kestrel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!