通过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/projectExecStart=/usr/bin/dotnet /var/aspnetcore/project/project.dllRestart=alwaysRestartSec=10SyslogIDentifIEr=dotnet-projectUser=rootEnvironment=ASPNETCORE_ENVIRONMENT=Production[Install]WantedBy=multi-user.target
对于部署使用此脚本,它一直工作到今天
dotnet restoredotnet builddotnet publishcp -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 restartsystemctl daemon-reloadsystemctl stop kestrel-project.servicesystemctl enable kestrel-project.servicesystemctl start kestrel-project.servicesystemctl status kestrel-project.service
现在,当我在我的本地主机上启动我的webAPI – webAPI运行良好并回答我的请求,但是从服务器我有http错误500和/var/log/Nginx/error.log我看到这个
2018/02/11 19:58:04 [alert] 10584#10584: aborting2018/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工作很长时间,但几天前我开始出现这些错误
@R_404_6120@ 这不是我的问题的答案,但我找到了另一种消除问题的方法如何使项目再次伟大
使用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:latestcopY . /appworkdir /appRUN ["dotnet","restore"]RUN ["dotnet","build"]EXPOSE 5050/tcpENV ASPNETCORE_URLS http://*5050ENV ASPNETCORE_ENVIRONMENT dockerENTRYPOINT ["dotnet","run","--server.urls","http://0.0.0.0:5050"]
我的文件Docker.Compose
FROM microsoft/dotnet:latestworkdir /appEXPOSE 5050:80FROM microsoft/aspnetcore-build:2.0 AS buildworkdir /srccopY *.sln ./copY project/project.csproj project/RUN dotnet restorecopY . .workdir /src/projectRUN dotnet build -c Release -o /appFROM build AS publishRUN dotnet publish -c Release -o /appFROM base AS finalworkdir /appcopY --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# – webapi .net核心得到nginx connect()失败(111:拒绝连接)全部内容,希望文章能够帮你解决c# – webapi .net核心得到nginx connect()失败(111:拒绝连接)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)