{"id":43142,"date":"2024-10-04T18:36:43","date_gmt":"2024-10-04T10:36:43","guid":{"rendered":"https:\/\/server.hk\/cnblog\/43142\/"},"modified":"2024-10-04T18:36:43","modified_gmt":"2024-10-04T10:36:43","slug":"centos-7-%e7%92%b0%e5%a2%83%e4%b8%8b%e4%bd%bf%e7%94%a8-nginx-%e6%89%98%e7%ae%a1-net-core-%e6%87%89%e7%94%a8%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/43142\/","title":{"rendered":"CentOS 7 \u74b0\u5883\u4e0b\u4f7f\u7528 Nginx \u6258\u7ba1 .Net Core \u61c9\u7528\u7a0b\u5e8f"},"content":{"rendered":"<h1 id=\"centos-7-%e7%92%b0%e5%a2%83%e4%b8%8b%e4%bd%bf%e7%94%a8-nginx-%e6%89%98%e7%ae%a1-net-core-%e6%87%89%e7%94%a8%e7%a8%8b%e5%ba%8f-yJwOdLTZvk\">CentOS 7 \u74b0\u5883\u4e0b\u4f7f\u7528 Nginx \u6258\u7ba1 .Net Core \u61c9\u7528\u7a0b\u5e8f<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c.Net Core \u61c9\u7528\u7a0b\u5e8f\u56e0\u5176\u8de8\u5e73\u53f0\u7684\u7279\u6027\u800c\u53d7\u5230\u5ee3\u6cdb\u95dc\u6ce8\u3002\u9019\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7d71\u4e0a\u904b\u884c\u61c9\u7528\u7a0b\u5e8f\uff0c\u800c CentOS 7 \u4f5c\u70ba\u4e00\u500b\u7a69\u5b9a\u7684 Linux \u767c\u884c\u7248\uff0c\u6210\u70ba\u4e86\u8a31\u591a\u4f01\u696d\u7684\u9996\u9078\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 CentOS 7 \u74b0\u5883\u4e0b\u4f7f\u7528 Nginx \u6258\u7ba1 .Net Core \u61c9\u7528\u7a0b\u5e8f\u3002<\/p>\n<h2 id=\"%e5%89%8d%e6%9c%9f%e6%ba%96%e5%82%99-yJwOdLTZvk\">\u524d\u671f\u6e96\u5099<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u60a8\u7684 CentOS 7 \u7cfb\u7d71\u5df2\u7d93\u66f4\u65b0\u5230\u6700\u65b0\u7248\u672c\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u66f4\u65b0\uff1a<\/p>\n<pre><code>sudo yum update -y<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u9700\u8981\u5b89\u88dd .Net Core SDK \u548c\u904b\u884c\u6642\u3002\u53ef\u4ee5\u901a\u904e Microsoft \u7684\u5b98\u65b9\u6587\u6a94\u7372\u53d6\u6700\u65b0\u7684\u5b89\u88dd\u6307\u5c0e\u3002\u4ee5\u4e0b\u662f\u5b89\u88dd .Net Core \u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<pre><code>sudo rpm -Uvh https:\/\/packages.microsoft.com\/config\/rhel\/7\/prod.repo\nsudo yum install dotnet-sdk-7.0<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e8%a3%9d-nginx-yJwOdLTZvk\">\u5b89\u88dd Nginx<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5b89\u88dd Nginx \u4f5c\u70ba\u53cd\u5411\u4ee3\u7406\u4f3a\u670d\u5668\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88dd Nginx\uff1a<\/p>\n<pre><code>sudo yum install epel-release\nsudo yum install nginx<\/code><\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u555f\u52d5 Nginx \u4e26\u8a2d\u7f6e\u5176\u5728\u7cfb\u7d71\u555f\u52d5\u6642\u81ea\u52d5\u555f\u52d5\uff1a<\/p>\n<pre><code>sudo systemctl start nginx\nsudo systemctl enable nginx<\/code><\/pre>\n<h2 id=\"%e9%85%8d%e7%bd%ae-net-core-%e6%87%89%e7%94%a8%e7%a8%8b%e5%ba%8f-yJwOdLTZvk\">\u914d\u7f6e .Net Core \u61c9\u7528\u7a0b\u5e8f<\/h2>\n<p>\u5728\u9019\u88e1\uff0c\u6211\u5011\u5047\u8a2d\u60a8\u5df2\u7d93\u6709\u4e00\u500b .Net Core \u61c9\u7528\u7a0b\u5e8f\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f86\u767c\u5e03\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff1a<\/p>\n<pre><code>dotnet publish -c Release -o .\/publish<\/code><\/pre>\n<p>\u9019\u5c07\u5728\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u76ee\u9304\u4e0b\u5275\u5efa\u4e00\u500b\u540d\u70ba &#8220;publish&#8221; \u7684\u6587\u4ef6\u593e\uff0c\u88e1\u9762\u5305\u542b\u4e86\u6240\u6709\u9700\u8981\u7684\u6587\u4ef6\u3002\u63a5\u4e0b\u4f86\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f86\u904b\u884c\u61c9\u7528\u7a0b\u5e8f\uff1a<\/p>\n<pre><code>dotnet .\/publish\/YourApp.dll<\/code><\/pre>\n<h2 id=\"%e9%85%8d%e7%bd%ae-nginx-%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86-yJwOdLTZvk\">\u914d\u7f6e Nginx \u53cd\u5411\u4ee3\u7406<\/h2>\n<p>\u73fe\u5728\uff0c\u6211\u5011\u9700\u8981\u914d\u7f6e Nginx \u4ee5\u4fbf\u5c07\u8acb\u6c42\u8f49\u767c\u5230 .Net Core \u61c9\u7528\u7a0b\u5e8f\u3002\u7de8\u8f2f Nginx \u7684\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<pre><code>sudo vi \/etc\/nginx\/conf.d\/yourapp.conf<\/code><\/pre>\n<p>\u5728\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5167\u5bb9\uff1a<\/p>\n<pre><code>server {\n    listen 80;\n    server_name your_domain.com;  # \u66ff\u63db\u70ba\u60a8\u7684\u57df\u540d\n\n    location \/ {\n        proxy_pass http:\/\/localhost:5000;  # .Net Core \u61c9\u7528\u7a0b\u5e8f\u7684\u7aef\u53e3\n        proxy_http_version 1.1;\n        proxy_set_header Upgrade $http_upgrade;\n        proxy_set_header Connection 'upgrade';\n        proxy_set_header Host $host;\n        proxy_cache_bypass $http_upgrade;\n    }\n}<\/code><\/pre>\n<p>\u4fdd\u5b58\u4e26\u9000\u51fa\u7de8\u8f2f\u5668\u5f8c\uff0c\u6aa2\u67e5 Nginx \u914d\u7f6e\u662f\u5426\u6b63\u78ba\uff1a<\/p>\n<pre><code>sudo nginx -t<\/code><\/pre>\n<p>\u5982\u679c\u6c92\u6709\u932f\u8aa4\uff0c\u91cd\u65b0\u52a0\u8f09 Nginx \u914d\u7f6e\uff1a<\/p>\n<pre><code>sudo systemctl reload nginx<\/code><\/pre>\n<h2 id=\"%e8%a8%ad%e7%bd%ae%e9%98%b2%e7%81%ab%e7%89%86-yJwOdLTZvk\">\u8a2d\u7f6e\u9632\u706b\u7246<\/h2>\n<p>\u6700\u5f8c\uff0c\u78ba\u4fdd\u60a8\u7684\u9632\u706b\u7246\u5141\u8a31 HTTP \u6d41\u91cf\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f86\u958b\u653e 80 \u7aef\u53e3\uff1a<\/p>\n<pre><code>sudo firewall-cmd --zone=public --add-port=80\/tcp --permanent\nsudo firewall-cmd --reload<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-yJwOdLTZvk\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u60a8\u5df2\u7d93\u6210\u529f\u5728 CentOS 7 \u74b0\u5883\u4e0b\u4f7f\u7528 Nginx \u6258\u7ba1\u4e86 .Net Core \u61c9\u7528\u7a0b\u5e8f\u3002\u9019\u7a2e\u914d\u7f6e\u4e0d\u50c5\u80fd\u5920\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u9084\u80fd\u589e\u5f37\u5b89\u5168\u6027\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4e91\u670d\u52a1\u5668<\/a>\uff0c\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 CentOS 7 \u74b0\u5883\u4e2d\uff0c\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528 Nginx \u6258\u7ba1 .Net Core \u61c9\u7528\u7a0b\u5e8f\uff0c\u63d0\u5347\u6027\u80fd\u8207\u5b89\u5168\u6027\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-43142","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/43142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=43142"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/43142\/revisions"}],"predecessor-version":[{"id":43143,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/43142\/revisions\/43143"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=43142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=43142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=43142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}