webapi .net core得到nginx connect()失败(111:拒绝连接)

时间:2018-02-11 18:01:50

标签: c# ubuntu nginx .net-core kestrel

将我的webapi dotnet核心项目部署到Ubuntu服务器 使用kestrel和代理通过nginx进行配置

我的代码在文件Program.cs

public class Program {
  public static void Main(string[] args) {
    var config = new ConfigurationBuilder()
      .SetBasePath(Directory.GetCurrentDirectory())
      .AddJsonFile("hosting.json", optional: true)
      .Build();

    var host = new WebHostBuilder()
      .UseKestrel()
      .UseConfiguration(config)
      .UseContentRoot(Directory.GetCurrentDirectory())
      .UseUrls("http://localhost:5050")
      .UseStartup<Startup>()
      .Build(); 
    host.Run();
  }
}

/etc/nginx/sites-available/default中创建文件,并将符号链接到/etc/nginx/sites-enable/default

server {
  listen 80;
  location / {
    proxy_pass http://localhost:5050;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection keep-alive;
    proxy_set_header Host $http_host;
    proxy_cache_bypass $http_upgrade;
  }
}

我在/etc/systemd/system/kestrel-project.service中的文件

[Unit]
Description=WebApi .NET Core running on Ubuntu

[Service]
WorkingDirectory=/var/aspnetcore/project
ExecStart=/usr/bin/dotnet /var/aspnetcore/project/project.dll
Restart=always
RestartSec=10
SyslogIdentifier=dotnet-project
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production

[Install]
WantedBy=multi-user.target

对于部署使用此脚本,它一直工作到今天

dotnet restore
dotnet build
dotnet publish

cp -r /root/src/project/bin/Debug/netcoreapp2.0/publish/* /var/aspnetcore/project/

cp /root/src/project/bin/Debug/netcoreapp2.0/project.dll /var/aspnetcore/project/

service nginx restart
systemctl daemon-reload
systemctl stop kestrel-project.service
systemctl enable kestrel-project.service
systemctl start kestrel-project.service
systemctl status kestrel-project.service

现在当我在我的本地主机上启动webapi时,webapi运行良好并回答我的请求,但是从服务器我得到HTTP错误500并在/var/log/nginx/error.log我看到了这个

2018/02/11 19:58:04 [alert] 10584#10584: aborting
2018/02/11 20:01:15 [error] 11717#11717: *1 connect() failed (111:Connection refused) while connecting to upstream, client: *.*.*.*, server: , request: "GET /list HTTP/1.1", upstream: "http://127.0.1.1:5050/list", host: "*.*.*.*.*"

请帮我理解这个问题并解决它

如果没有变化,webapi工作很长时间,但几天前我开始出现这些错误

1 个答案:

答案 0 :(得分:1)

这不是我的问题的答案,但我找到了另一种消除问题的方法

如何让项目再次成为伟大

使用Docker支持创建新的.net核心项目Web-Api

使用Docker

将项目代码复制/粘贴到新项目

现在我的代码在Program.cs

public static void Main(string[] args) {
  BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args) =>
  WebHost.CreateDefaultBuilder(args)
    .UseKestrel()
    .UseStartup<Startup>()
    .Build();
}

我在/etc/nginx/sites-available/default

中的文件
server {
  listen 80;
  server_name api.mydomain.com;

  location / {
    proxy_pass http://localhost:5050;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $http_connection;
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
}

删除文件/etc/systemd/system/kestrel-project.service

在项目中配置Docker

我的Docker文件 - 用于在Docker Hub上自动构建

FROM microsoft/dotnet:latest
COPY . /app
WORKDIR /app

RUN ["dotnet", "restore"]
RUN ["dotnet", "build"]

EXPOSE 5050/tcp

ENV ASPNETCORE_URLS http://*5050
ENV ASPNETCORE_ENVIRONMENT docker

ENTRYPOINT ["dotnet", "run", "--server.urls", "http://0.0.0.0:5050"]

我的文件Docker.Compose

FROM microsoft/dotnet:latest
WORKDIR /app
EXPOSE 5050:80

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY *.sln ./
COPY project/project.csproj project/
RUN dotnet restore
COPY . .
WORKDIR /src/project
RUN dotnet build -c Release -o /app

FROM build AS publish
RUN dotnet publish -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "project.dll"]

注册Docker Hub 免费

连接到我的github帐户并为我的项目创建自动构建

然后在服务器端我连接到docker hub

制作docker pull dockerprofile/project

然后docker run -d -p 5050:80 dockerprofile/project

之后一切都运行良好4个月

ps如果您需要更多详细信息,请向我索取