{"id":617,"date":"2025-12-07T15:30:18","date_gmt":"2025-12-07T07:30:18","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=617"},"modified":"2025-12-07T15:30:18","modified_gmt":"2025-12-07T07:30:18","slug":"net%e6%9c%8d%e5%8a%a1%e5%99%a8kestrel%e9%85%8d%e7%bd%aenginx%e4%bd%9c%e4%b8%ba%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86%e7%9a%84%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\/07\/net%e6%9c%8d%e5%8a%a1%e5%99%a8kestrel%e9%85%8d%e7%bd%aenginx%e4%bd%9c%e4%b8%ba%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86%e7%9a%84%e9%97%ae%e9%a2%98%e8%ae%b0%e5%bd%95\/","title":{"rendered":".net\u670d\u52a1\u5668Kestrel\u914d\u7f6eNginx\u4f5c\u4e3a\u53cd\u5411\u4ee3\u7406\u7684\u95ee\u9898\u8bb0\u5f55"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u6574\u7406\u4e00\u4efd <strong>\u201c.NET \u670d\u52a1\u5668 Kestrel \u914d\u7f6e Nginx \u4f5c\u4e3a\u53cd\u5411\u4ee3\u7406\u7684\u95ee\u9898\u8bb0\u5f55\uff08\u907f\u5751\u6307\u5357\uff09\u201d<\/strong>\uff0c\u9002\u5408\u4f60\u505a\u5b66\u4e60\u7b14\u8bb0\u3001\u535a\u5ba2\u6587\u7ae0\u6216 YouTube \u53e3\u64ad\u6280\u672f\u5185\u5bb9\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">.NET \u670d\u52a1\u5668 Kestrel \u914d\u7f6e Nginx \u4f5c\u4e3a\u53cd\u5411\u4ee3\u7406\u7684\u95ee\u9898\u8bb0\u5f55\uff08\u907f\u5751\u6307\u5357\uff09<\/h1>\n\n\n\n<p>Kestrel \u662f ASP.NET Core \u9ed8\u8ba4\u5185\u7f6e\u7684 Web \u670d\u52a1\u5668\uff0c\u4f46\u5b83\u4e0d\u8d1f\u8d23\u5904\u7406\u9759\u6001\u6587\u4ef6\u3001SSL\u3001\u591a\u7ad9\u70b9\u7b49\uff0c\u56e0\u6b64\u751f\u4ea7\u73af\u5883\u4e00\u822c\u4f7f\u7528 <strong>Nginx \u4f5c\u4e3a\u53cd\u5411\u4ee3\u7406<\/strong>\u3002<br>\u4f46\u5b9e\u9645\u90e8\u7f72\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u8e29\u5751\u3002\u4e0b\u9762\u5c31\u662f\u5e38\u89c1\u95ee\u9898 + \u89e3\u51b3\u65b9\u6848\u603b\u7ed3\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e00\u3001Kestrel + Nginx \u6807\u51c6\u914d\u7f6e<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">1.1 Nginx \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 yourdomain.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        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<h3 class=\"wp-block-heading\">1.2 Kestrel \u7a0b\u5e8f\u76d1\u542c<\/h3>\n\n\n\n<p><code>appsettings.json<\/code>\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&quot;Kestrel&quot;: {\n  &quot;Endpoints&quot;: {\n    &quot;Http&quot;: {\n      &quot;Url&quot;: &quot;http:\/\/127.0.0.1:5000&quot;\n    }\n  }\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e8c\u3001\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848\uff08\u771f\u5b9e\u8e29\u5751\u8bb0\u5f55\uff09<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u95ee\u9898 1\uff1a\u8bbf\u95ee\u540e\u63d0\u793a 502 Bad Gateway<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u5e38\u89c1\u539f\u56e0\uff1a<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Kestrel \u6839\u672c\u6ca1\u542f\u52a8<\/li>\n\n\n\n<li>Kestrel \u6ca1\u76d1\u542c 127.0.0.1:5000<\/li>\n\n\n\n<li>\u76d1\u542c\u7684\u662f <code>localhost<\/code> \u800c\u4e0d\u662f <code>127.0.0.1<\/code>\uff08Nginx \u4e0d\u8ba4\uff09<\/li>\n\n\n\n<li>\u7a0b\u5e8f\u88ab\u5360\u7528\u7aef\u53e3<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u7aef\u53e3\uff1a<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsudo lsof -i:5000\n\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">\u624b\u52a8\u6d4b\u8bd5 Kestrel\uff1a<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncurl http:\/\/127.0.0.1:5000\n\n<\/pre><\/div>\n\n\n<p>\u80fd\u901a \u2192 \u8bf4\u660e\u662f Nginx \u914d\u7f6e\u95ee\u9898<br>\u4e0d\u901a \u2192 \u7a0b\u5e8f\u6ca1\u542f\u52a8\u6216\u7ed1\u5b9a\u9519\u8bef<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u95ee\u9898 2\uff1aNginx \u4ee3\u7406 WebSocket \u5931\u6548<\/strong><\/h2>\n\n\n\n<p>\u8868\u73b0\uff1aSignalR \/ WebSocket \u65e0\u6cd5\u8fde\u63a5\uff0c\u4e00\u76f4\u62a5\u9519\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u539f\u56e0\uff1aNginx \u9ed8\u8ba4\u4e0d\u652f\u6301 WebSocket\uff0c\u9700\u8981\u624b\u52a8\u5f00\u542f<\/strong><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u89e3\u51b3\uff1a\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e<\/strong><\/h3>\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<h2 class=\"wp-block-heading\"><strong>\u95ee\u9898 3\uff1aHttps \u53cd\u4ee3\u540e Request.Scheme \u8fd8\u662f http<\/strong><\/h2>\n\n\n\n<p>\u4f8b\u5982\u751f\u6210 URL \u65f6\u53d8\u6210\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nhttp:\/\/yourdomain.com\/login\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>\u539f\u56e0\uff1a\u672a\u8bbe\u7f6e X-Forwarded-Proto<\/strong><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u89e3\u51b3\uff1a<\/strong><\/h3>\n\n\n\n<p>Nginx\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nproxy_set_header X-Forwarded-Proto $scheme;\n\n<\/pre><\/div>\n\n\n<p>Program.cs \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.XForwardedProto | ForwardedHeaders.XForwardedFor\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\"><strong>\u95ee\u9898 4\uff1a\u9759\u6001\u6587\u4ef6\u3001\u56fe\u7247\u8bbf\u95ee\u4e0d\u5230 \u6216 404<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u539f\u56e0\uff1aNginx \u9ed8\u8ba4\u4e0d\u4f1a\u4ee3\u7406\u9759\u6001\u6587\u4ef6\u8def\u5f84<\/strong><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848 1\uff08\u63a8\u8350\uff09<\/h3>\n\n\n\n<p>\u8ba9 <strong>ASP.NET Core \u81ea\u5df1\u63d0\u4f9b\u9759\u6001\u6587\u4ef6<\/strong><br>Program.cs\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\napp.UseStaticFiles();\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848 2\uff1a\u8ba9 Nginx \u63d0\u4f9b\u9759\u6001\u6587\u4ef6<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nlocation \/images\/ {\n    alias \/var\/www\/app\/images\/;\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\"><strong>\u95ee\u9898 5\uff1aNginx \u8fd4\u56de\u7684\u6587\u4ef6\u53d8\u4e3a\u4e0b\u8f7d\uff08MIME \u9519\u8bef\uff09<\/strong><\/h2>\n\n\n\n<p>\u8868\u73b0\uff1a<br>\u524d\u7aef\u8bbf\u95ee <code>.json<\/code>\u3001<code>.wasm<\/code>\u3001<code>.js<\/code> \u4f1a\u76f4\u63a5\u5f3a\u5236\u4e0b\u8f7d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5e38\u89c1\u539f\u56e0<\/h3>\n\n\n\n<p>\u7f3a\u5c11 MIME \u914d\u7f6e<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u89e3\u51b3<\/h3>\n\n\n\n<p>\u5728 nginx.conf \u52a0\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ninclude \/etc\/nginx\/mime.types;\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\"><strong>\u95ee\u9898 6\uff1a\u53d1\u5e03\u540e\u65e0\u6cd5\u8bbf\u95ee \/ Kestrel \u6302\u6389<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u5e38\u89c1\u539f\u56e0\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u672a\u8bbe\u7f6e systemd \u81ea\u52a8\u542f\u52a8<\/li>\n\n\n\n<li>\u7a0b\u5e8f\u610f\u5916\u5d29\u6e83<\/li>\n\n\n\n<li>\u6743\u9650\u4e0d\u8db3<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u89e3\u51b3\uff1a\u521b\u5efa systemd \u670d\u52a1<\/h3>\n\n\n\n<p><code>\/etc\/systemd\/system\/kestrel-app.service<\/code><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;Unit]\nDescription=ASP.NET App\nAfter=network.target\n\n&#x5B;Service]\nWorkingDirectory=\/var\/www\/app\nExecStart=\/usr\/bin\/dotnet app.dll\nRestart=always\nRestartSec=10\nSyslogIdentifier=dotnet-app\nUser=www-data\nEnvironment=ASPNETCORE_ENVIRONMENT=Production\n\n&#x5B;Install]\nWantedBy=multi-user.target\n\n<\/pre><\/div>\n\n\n<p>\u542f\u52a8\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsudo systemctl enable kestrel-app\nsudo systemctl start kestrel-app\nsudo systemctl status kestrel-app\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e09\u3001Nginx \u6027\u80fd\u4f18\u5316\u5efa\u8bae<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u5f00\u542f Gzip \u538b\u7f29<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ngzip on;\ngzip_types text\/plain text\/css application\/javascript application\/json;\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">2. \u51cf\u5c11\u4ee3\u7406\u8d85\u65f6<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nproxy_read_timeout 300;\nproxy_send_timeout 300;\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">3. \u63d0\u5347\u6587\u4ef6\u4e0a\u4f20\u9650\u5236<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nclient_max_body_size 50M;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u56db\u3001\u6700\u7ec8\u5efa\u8bae\uff08\u751f\u4ea7\u73af\u5883\u6700\u4f73\u5b9e\u8df5\uff09<\/h1>\n\n\n\n<p>\u2714 Kestrel <strong>\u53ea\u8d1f\u8d23\u8dd1\u5e94\u7528<\/strong><br>\u2714 Nginx \u8d1f\u8d23\uff1a\u53cd\u5411\u4ee3\u7406 \/ SSL \/ \u8d1f\u8f7d\u5747\u8861 \/ \u9759\u6001\u6587\u4ef6<br>\u2714 \u4f7f\u7528 systemd \u81ea\u52a8\u5b88\u62a4\u5e94\u7528<br>\u2714 \u5f00\u542f Forwarded Headers \u4fee\u6b63 Scheme<br>\u2714 WebSocket \u5fc5\u987b\u5347\u7ea7 Connection \u5934<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u6574\u7406\u4e00\u4efd \u201c.NET \u670d\u52a1\u5668 Kestrel \u914d\u7f6e Nginx \u4f5c\u4e3a\u53cd\u5411&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/07\/net%e6%9c%8d%e5%8a%a1%e5%99%a8kestrel%e9%85%8d%e7%bd%aenginx%e4%bd%9c%e4%b8%ba%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86%e7%9a%84%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":[50],"tags":[],"class_list":["post-617","post","type-post","status-publish","format-standard","hentry","category-asp-net"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/617","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=617"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/617\/revisions"}],"predecessor-version":[{"id":618,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/617\/revisions\/618"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}