{"id":90,"date":"2022-09-14T20:07:21","date_gmt":"2022-09-14T12:07:21","guid":{"rendered":"http:\/\/124.221.75.24\/wordpress\/?p=90"},"modified":"2022-09-14T20:07:50","modified_gmt":"2022-09-14T12:07:50","slug":"dotnet6-core-webapi%e9%83%a8%e7%bd%b2%e5%88%b0linux%e5%b9%b6%e9%85%8d%e7%bd%aenginx%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.alonya.cn\/?p=90","title":{"rendered":"DotNet(6) Core WebAPI\u90e8\u7f72\u5230Linux\u5e76\u914d\u7f6eNginx\u53cd\u5411\u4ee3\u7406"},"content":{"rendered":"<p><html><br \/>\n<head><br \/>\n<meta charset='UTF-8'><meta name='viewport' content='width=device-width initial-scale=1'><br \/>\n<title>DotNet Core WebAPI\u90e8\u7f72\u5230Linux\u5e76\u914d\u7f6eNginx\u53cd\u5411\u4ee3\u7406<\/title><br \/>\n<\/head><br \/>\n<body><\/p>\n<ol start='' >\n<li>\n<p>\u521b\u5efaWebApi\u6a21\u677f\u5e76\u53d1\u5e03<\/p>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u4e2d\u95f4\u4ef6<\/strong><\/p>\n<pre><code class='language-c#' lang='c#'>\/\/app.UseHttpsRedirection();\/\/\u505c\u7528https\u91cd\u5b9a\u5411\nusing Microsoft.AspNetCore.HttpOverrides;\napp.UseForwardedHeaders(new ForwardedHeadersOptions\n{\n    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto\n});\napp.UseAuthentication();\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6eNginx<\/strong><\/p>\n<pre><code class='language-c#' lang='c#'>server {\n    listen        81;\n    server_name   example.com *.example.com;\n    location \/api\/ {\n        proxy_pass         http:\/\/127.0.0.1:5000\/api\/;\n        proxy_http_version 1.1;\n        proxy_set_header   Upgrade $http_upgrade;\n        proxy_set_header   Connection keep-alive;\n        proxy_set_header   Host $host;\n        proxy_cache_bypass $http_upgrade;\n        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;\n        proxy_set_header   X-Forwarded-Proto $scheme;\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u5b88\u62a4\u8fdb\u7a0b<\/strong><\/p>\n<pre><code class='language-c#' lang='c#'>\/\/\u65b0\u5efa\u6587\u4ef6\nvim \/etc\/systemd\/system\/kestrel-myapi.service \n\/\/\u7f16\u8f91   \n[Unit]\nDescription=Example .NET Web API App running on CentOS\n\n[Service]\nWorkingDirectory=\/root\/public\/linux-x64\nExecStart=\/usr\/bin\/dotnet \/root\/public\/linux-x64\/MyFirstDotnetAPI.dll\nRestart=always\n# Restart service after 10 seconds if the dotnet service crashes:\nRestartSec=10\nKillSignal=SIGINT\nSyslogIdentifier=dotnet-example\nUser=root\nEnvironment=ASPNETCORE_ENVIRONMENT=Production\nEnvironment=DOTNET_PRINT_TELEMETRY_MESSAGE=false\n\n[Install]\nWantedBy=multi-user.target\n\/\/wq\n\n\/\/systemctl start kestrel-myapi.service\n\/\/systemctl status kestrel-myapi.service\n\/\/systemctl enable kestrel-myapi.service\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u5173\u95edSeLinux<\/strong><\/p>\n<pre><code class='language-c' lang='c'>vim \/etc\/selinux\/config\n\/\/\n    SELINUX=disabled\n\/\/wq\ninit 6\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><code>\u5982\u679c\u4e0d\u4f7f\u7528\u53cd\u5411\u4ee3\u7406,\u53ef\u7ed1\u5b9a\u6240\u6709\u672c\u673aIP\u8fdc\u7a0b\u8bbf\u95ee<\/code><\/p>\n<pre><code class='language-c#' lang='c#'>builder.Services.AddControllers();\nbuilder.WebHost.UseUrls(new[] { &quot;http:\/\/*:5000&quot; });\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p><\/body><br \/>\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"<p>DotNet Core WebAPI\u90e8\u7f72\u5230Linux\u5e76\u914d\u7f6eNginx\u53cd\u5411\u4ee3\u7406 \u521b\u5efaWebApi\u6a21\u677f\u5e76\u53d1\u5e03 \u6dfb\u52a0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,17],"tags":[],"class_list":["post-90","post","type-post","status-publish","format-standard","hentry","category-net","category-linux"],"_links":{"self":[{"href":"https:\/\/www.alonya.cn\/index.php?rest_route=\/wp\/v2\/posts\/90","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.alonya.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.alonya.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.alonya.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.alonya.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=90"}],"version-history":[{"count":2,"href":"https:\/\/www.alonya.cn\/index.php?rest_route=\/wp\/v2\/posts\/90\/revisions"}],"predecessor-version":[{"id":92,"href":"https:\/\/www.alonya.cn\/index.php?rest_route=\/wp\/v2\/posts\/90\/revisions\/92"}],"wp:attachment":[{"href":"https:\/\/www.alonya.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=90"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.alonya.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=90"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.alonya.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}