c# – webapi .net核心得到nginx connect()失败(111:拒绝连接)

将我的webapi dotnet核心项目部署到Ubuntu服务器
通过nginx配置kestrel和proxy

我在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和maded symlink中创建文件到/ 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工作很长时间,但几天前我开始出现这些错误

解决方法:

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

如何使项目再次伟大

使用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运行-d -p 5050:80 dockerprofile / project

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

ps如果您需要更多细节,请问我

上一篇:C# MessageBox(WPF弹出对话确认框)


下一篇:boot使用前端传来的JSON