本文介绍了nginx背后的aspnetcore 2.0红est unix套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在Ubuntu 16中通过http请求在nginx后面使用aspnet内核.

I am current using aspnet core behind nginx through http requests in Ubuntu 16.

我想切换到unix套接字.

And I'd like to switch to unix socket.

在我的program.cs中,我有:

In my program.cs I have:

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:/;
}

使用http运行它可以,但是,在更改为套接字时,出现了502错误.

Running it using http works, but, chaning to socket 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背后的aspnetcore 2.0红est unix套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 08:04