{"id":824,"date":"2025-12-20T15:48:12","date_gmt":"2025-12-20T07:48:12","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=824"},"modified":"2025-12-20T15:48:12","modified_gmt":"2025-12-20T07:48:12","slug":"net-%e6%9c%8d%e5%8a%a1%e5%99%a8-kestrel-%e9%85%8d%e7%bd%ae-nginx-%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86%e9%97%ae%e9%a2%98%e8%ae%b0%e5%bd%95","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/20\/net-%e6%9c%8d%e5%8a%a1%e5%99%a8-kestrel-%e9%85%8d%e7%bd%ae-nginx-%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86%e9%97%ae%e9%a2%98%e8%ae%b0%e5%bd%95\/","title":{"rendered":".NET \u670d\u52a1\u5668 Kestrel \u914d\u7f6e Nginx \u53cd\u5411\u4ee3\u7406\u95ee\u9898\u8bb0\u5f55"},"content":{"rendered":"\n<p>\u5728 Linux \u6216 Windows \u4e0b\u90e8\u7f72 <strong>ASP.NET Core \u5e94\u7528<\/strong> \u65f6\uff0c\u901a\u5e38\u4f7f\u7528 <strong>Kestrel<\/strong> \u4f5c\u4e3a\u5185\u7f6e Web \u670d\u52a1\u5668\uff0c\u518d\u901a\u8fc7 <strong>Nginx<\/strong> \u505a\u53cd\u5411\u4ee3\u7406\u3002<br>\u867d\u7136\u5b98\u65b9\u6587\u6863\u63a8\u8350\u8fd9\u79cd\u7ec4\u5408\uff0c\u4f46\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\uff0c\u5e38\u4f1a\u9047\u5230\u5404\u79cd\u95ee\u9898\u3002\u672c\u6587\u8bb0\u5f55\u6211\u9047\u5230\u7684\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u57fa\u672c\u914d\u7f6e\u56de\u987e<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1\ufe0f\u20e3 Kestrel \u542f\u52a8\u793a\u4f8b\uff08Program.cs\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nvar builder = WebApplication.CreateBuilder(args);\nbuilder.WebHost.ConfigureKestrel(options =&gt;\n{\n    options.ListenAnyIP(5000); \/\/ Kestrel \u76d1\u542c 5000 \u7aef\u53e3\n});\n\nvar app = builder.Build();\napp.MapGet(&quot;\/&quot;, () =&gt; &quot;Hello World&quot;);\napp.Run();\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2\ufe0f\u20e3 Nginx \u53cd\u5411\u4ee3\u7406\u914d\u7f6e\u793a\u4f8b<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nserver {\n    listen 80;\n    server_name example.com;\n\n    location \/ {\n        proxy_pass         http:\/\/127.0.0.1:5000;\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    }\n}\n\n<\/pre><\/div>\n\n\n<p>\u2705 \u8fd9\u662f\u6700\u57fa\u672c\u7684\u53cd\u5411\u4ee3\u7406\u914d\u7f6e\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u95ee\u9898 1\uff1a\u8bbf\u95ee Nginx \u663e\u793a 502 Bad Gateway<\/h3>\n\n\n\n<p><strong>\u539f\u56e0\u5206\u6790\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Kestrel \u672a\u542f\u52a8\u6216\u7aef\u53e3\u672a\u76d1\u542c<\/li>\n\n\n\n<li>\u9632\u706b\u5899\u963b\u6b62 5000 \u7aef\u53e3<\/li>\n\n\n\n<li>Nginx \u914d\u7f6e\u7684 <code>proxy_pass<\/code> \u5730\u5740\u9519\u8bef<\/li>\n<\/ul>\n\n\n\n<p><strong>\u89e3\u51b3\u65b9\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u786e\u8ba4 Kestrel \u542f\u52a8\uff1a<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nnetstat -tulnp | grep 5000\n\n<\/pre><\/div>\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u786e\u8ba4\u9632\u706b\u5899\u5141\u8bb8\u7aef\u53e3\uff1a<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsudo ufw allow 5000\/tcp\n\n<\/pre><\/div>\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li>\u68c0\u67e5 Nginx \u914d\u7f6e\u8bed\u6cd5\u5e76\u91cd\u542f\uff1a<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsudo nginx -t\nsudo systemctl restart nginx\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u95ee\u9898 2\uff1aHTTP 1.1 WebSocket \u5347\u7ea7\u5931\u8d25<\/h3>\n\n\n\n<p><strong>\u539f\u56e0\u5206\u6790\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>WebSocket \u9700\u8981 <code>Upgrade<\/code> \u548c <code>Connection<\/code> \u5934<\/li>\n\n\n\n<li>Nginx \u9ed8\u8ba4\u53ef\u80fd\u672a\u8f6c\u53d1\u8fd9\u4e9b\u5934<\/li>\n<\/ul>\n\n\n\n<p><strong>\u89e3\u51b3\u65b9\u6cd5\uff1a<\/strong><br>\u5728 Nginx \u914d\u7f6e\u4e2d\u52a0\u5165\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nproxy_set_header Upgrade $http_upgrade;\nproxy_set_header Connection &quot;Upgrade&quot;;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u95ee\u9898 3\uff1aHTTPS \u914d\u7f6e\u95ee\u9898<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Kestrel \u901a\u5e38\u76d1\u542c HTTP\uff085000\uff09<\/li>\n\n\n\n<li>Nginx \u8d1f\u8d23 HTTPS \u7ec8\u6b62<\/li>\n\n\n\n<li>\u5982\u679c\u76f4\u63a5\u7528 HTTPS \u5230 Kestrel\uff0c\u8bc1\u4e66\u914d\u7f6e\u53ef\u80fd\u590d\u6742<\/li>\n<\/ul>\n\n\n\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Kestrel \u53ea\u76d1\u542c HTTP<\/li>\n\n\n\n<li>Nginx \u914d\u7f6e SSL \u8bc1\u4e66\uff0c\u5e76\u8f6c\u53d1 HTTP \u8bf7\u6c42\u5230 Kestrel<\/li>\n<\/ol>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nserver {\n    listen 443 ssl;\n    server_name example.com;\n\n    ssl_certificate     \/etc\/ssl\/certs\/example.crt;\n    ssl_certificate_key \/etc\/ssl\/private\/example.key;\n\n    location \/ {\n        proxy_pass http:\/\/127.0.0.1:5000;\n        proxy_set_header Host $host;\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n        proxy_set_header X-Forwarded-Proto $scheme;\n    }\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u95ee\u9898 4\uff1a\u83b7\u53d6\u5ba2\u6237\u7aef\u771f\u5b9e IP<\/h3>\n\n\n\n<p><strong>\u539f\u56e0\u5206\u6790\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Kestrel \u770b\u5230\u7684\u90fd\u662f Nginx \u7684 IP<\/li>\n\n\n\n<li>\u9700\u8981\u4f7f\u7528 <code>X-Forwarded-For<\/code> \u5934<\/li>\n<\/ul>\n\n\n\n<p><strong>\u89e3\u51b3\u65b9\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<p>\u5728 <code>Program.cs<\/code> \u4e2d\u6dfb\u52a0\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\napp.UseForwardedHeaders(new ForwardedHeadersOptions\n{\n    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto\n});\n\n<\/pre><\/div>\n\n\n<p>\u5e76\u786e\u4fdd Nginx \u914d\u7f6e\u4e86\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\nproxy_set_header X-Forwarded-Proto $scheme;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u95ee\u9898 5\uff1a\u5927\u6587\u4ef6\u4e0a\u4f20\u5931\u8d25\u6216\u8d85\u65f6<\/h3>\n\n\n\n<p><strong>\u539f\u56e0\u5206\u6790\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Nginx \u9ed8\u8ba4 <code>client_max_body_size<\/code> \u4e3a 1M<\/li>\n\n\n\n<li>Kestrel \u4e5f\u6709\u8bf7\u6c42\u5927\u5c0f\u9650\u5236<\/li>\n<\/ul>\n\n\n\n<p><strong>\u89e3\u51b3\u65b9\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u4fee\u6539 Nginx \u914d\u7f6e\uff1a<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nserver {\n    ...\n    client_max_body_size 50M;\n}\n\n<\/pre><\/div>\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u4fee\u6539 Kestrel \u914d\u7f6e\uff1a<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nbuilder.WebHost.ConfigureKestrel(options =&gt;\n{\n    options.Limits.MaxRequestBodySize = 50 * 1024 * 1024; \/\/ 50MB\n});\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u90e8\u7f72\u4f18\u5316\u5efa\u8bae<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 Systemd \u7ba1\u7406 Kestrel \u670d\u52a1<\/strong><\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsudo nano \/etc\/systemd\/system\/kestrel-app.service\n\n<\/pre><\/div>\n\n\n<p>\u793a\u4f8b Service \u6587\u4ef6\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;Unit]\nDescription=Kestrel .NET App\nAfter=network.target\n\n&#x5B;Service]\nWorkingDirectory=\/var\/www\/myapp\nExecStart=\/usr\/bin\/dotnet \/var\/www\/myapp\/MyApp.dll\nRestart=always\nRestartSec=10\nSyslogIdentifier=kestrel-app\nUser=www-data\nEnvironment=ASPNETCORE_ENVIRONMENT=Production\n\n&#x5B;Install]\nWantedBy=multi-user.target\n\n<\/pre><\/div>\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u5f00\u542f Nginx \u7f13\u5b58\u6216\u538b\u7f29<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u51cf\u5c11\u9759\u6001\u8d44\u6e90\u4f20\u8f93<\/li>\n\n\n\n<li>\u63d0\u5347\u8bbf\u95ee\u901f\u5ea6<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u65e5\u5fd7\u76d1\u63a7<\/strong>\n<ul class=\"wp-block-list\">\n<li>Kestrel \u65e5\u5fd7\uff1a<code>dotnet MyApp.dll --urls http:\/\/*:5000<\/code><\/li>\n\n\n\n<li>Nginx \u65e5\u5fd7\uff1a<code>\/var\/log\/nginx\/access.log<\/code> \u548c <code>\/var\/log\/nginx\/error.log<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u603b\u7ed3<\/h2>\n\n\n\n<p>\u914d\u7f6e Kestrel + Nginx \u53cd\u5411\u4ee3\u7406\u867d\u7136\u5b98\u65b9\u63a8\u8350\uff0c\u4f46\u5b9e\u9645\u90e8\u7f72\u8fc7\u7a0b\u4e2d\u5e38\u89c1\u95ee\u9898\u5305\u62ec\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u95ee\u9898<\/th><th>\u539f\u56e0<\/th><th>\u89e3\u51b3\u65b9\u6cd5<\/th><\/tr><\/thead><tbody><tr><td>502 Bad Gateway<\/td><td>Kestrel \u672a\u542f\u52a8 \/ Nginx \u914d\u7f6e\u9519\u8bef<\/td><td>\u68c0\u67e5\u7aef\u53e3\u3001\u91cd\u542f\u670d\u52a1<\/td><\/tr><tr><td>WebSocket \u5347\u7ea7\u5931\u8d25<\/td><td>Upgrade\/Connection \u5934\u672a\u8f6c\u53d1<\/td><td>Nginx \u914d\u7f6e proxy_set_header<\/td><\/tr><tr><td>HTTPS \u914d\u7f6e\u590d\u6742<\/td><td>Kestrel \u76f4\u63a5 HTTPS \u4e0d\u65b9\u4fbf<\/td><td>\u8ba9 Nginx \u7ec8\u6b62 SSL<\/td><\/tr><tr><td>\u5ba2\u6237\u7aef\u771f\u5b9e IP \u4e0d\u53ef\u89c1<\/td><td>\u672a\u4f7f\u7528 X-Forwarded-For<\/td><td>\u914d\u7f6e ForwardedHeaders \u4e2d\u95f4\u4ef6<\/td><\/tr><tr><td>\u5927\u6587\u4ef6\u4e0a\u4f20\u5931\u8d25\u6216\u8d85\u65f6<\/td><td>Nginx &amp; Kestrel \u9650\u5236<\/td><td>\u589e\u52a0 client_max_body_size &amp; MaxRequestBodySize<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u6b63\u786e\u914d\u7f6e\u540e\uff0cKestrel + Nginx \u80fd\u63d0\u4f9b <strong>\u9ad8\u6027\u80fd\u3001\u7a33\u5b9a\u3001\u5b89\u5168\u7684 .NET Core Web \u670d\u52a1<\/strong>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Linux \u6216 Windows \u4e0b\u90e8\u7f72 ASP.NET Core \u5e94\u7528 \u65f6\uff0c&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/20\/net-%e6%9c%8d%e5%8a%a1%e5%99%a8-kestrel-%e9%85%8d%e7%bd%ae-nginx-%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86%e9%97%ae%e9%a2%98%e8%ae%b0%e5%bd%95\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-824","post","type-post","status-publish","format-standard","hentry","category-12"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=824"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/824\/revisions"}],"predecessor-version":[{"id":825,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/824\/revisions\/825"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}