{"id":50993,"date":"2024-10-08T15:16:15","date_gmt":"2024-10-08T07:16:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/50993\/"},"modified":"2024-10-08T15:16:15","modified_gmt":"2024-10-08T07:16:15","slug":"nginx-%e6%96%b0%e6%89%8b%e6%95%99%e7%a8%8b%ef%bc%9a%e8%a8%ad%e7%bd%ae%e8%b2%a0%e8%bc%89%e5%9d%87%e8%a1%a1","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/50993\/","title":{"rendered":"Nginx \u65b0\u624b\u6559\u7a0b\uff1a\u8a2d\u7f6e\u8ca0\u8f09\u5747\u8861"},"content":{"rendered":"<h1 id=\"nginx-%e6%96%b0%e6%89%8b%e6%95%99%e7%a8%8b%ef%bc%9a%e8%a8%ad%e7%bd%ae%e8%b2%a0%e8%bc%89%e5%9d%87%e8%a1%a1-uQMabYmCjS\">Nginx \u65b0\u624b\u6559\u7a0b\uff1a\u8a2d\u7f6e\u8ca0\u8f09\u5747\u8861<\/h1>\n<p>Nginx \u662f\u4e00\u6b3e\u9ad8\u6548\u80fd\u7684\u7db2\u9801\u4f3a\u670d\u5668\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u8ca0\u8f09\u5747\u8861\u3001\u53cd\u5411\u4ee3\u7406\u548c\u975c\u614b\u5167\u5bb9\u7684\u63d0\u4f9b\u3002\u96a8\u8457\u7db2\u7ad9\u6d41\u91cf\u7684\u589e\u52a0\uff0c\u55ae\u4e00\u4f3a\u670d\u5668\u53ef\u80fd\u7121\u6cd5\u6eff\u8db3\u9700\u6c42\uff0c\u56e0\u6b64\u8a2d\u7f6e\u8ca0\u8f09\u5747\u8861\u6210\u70ba\u4e00\u500b\u91cd\u8981\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Nginx \u4f86\u8a2d\u7f6e\u8ca0\u8f09\u5747\u8861\uff0c\u5e6b\u52a9\u65b0\u624b\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e8%b2%a0%e8%bc%89%e5%9d%87%e8%a1%a1%ef%bc%9f-uQMabYmCjS\">\u4ec0\u9ebc\u662f\u8ca0\u8f09\u5747\u8861\uff1f<\/h2>\n<p>\u8ca0\u8f09\u5747\u8861\u662f\u4e00\u7a2e\u6280\u8853\uff0c\u901a\u904e\u5c07\u9032\u5165\u7684\u6d41\u91cf\u5206\u914d\u5230\u591a\u500b\u4f3a\u670d\u5668\u4e0a\uff0c\u4ee5\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u53ef\u7528\u6027\u548c\u53ef\u9760\u6027\u3002\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u55ae\u4e00\u4f3a\u670d\u5668\u7684\u904e\u8f09\uff0c\u4e26\u78ba\u4fdd\u5373\u4f7f\u67d0\u500b\u4f3a\u670d\u5668\u51fa\u73fe\u6545\u969c\uff0c\u5176\u4ed6\u4f3a\u670d\u5668\u4ecd\u7136\u53ef\u4ee5\u7e7c\u7e8c\u63d0\u4f9b\u670d\u52d9\u3002<\/p>\n<h2 id=\"nginx-%e7%9a%84%e8%b2%a0%e8%bc%89%e5%9d%87%e8%a1%a1%e6%a8%a1%e5%bc%8f-uQMabYmCjS\">Nginx \u7684\u8ca0\u8f09\u5747\u8861\u6a21\u5f0f<\/h2>\n<p>Nginx \u652f\u6301\u591a\u7a2e\u8ca0\u8f09\u5747\u8861\u7b97\u6cd5\uff0c\u4e3b\u8981\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u8f2a\u8a62\uff08Round Robin\uff09<\/strong>\uff1a\u5c07\u8acb\u6c42\u4f9d\u6b21\u5206\u914d\u7d66\u6bcf\u500b\u4f3a\u670d\u5668\uff0c\u9069\u5408\u4f3a\u670d\u5668\u6027\u80fd\u76f8\u8fd1\u7684\u60c5\u6cc1\u3002<\/li>\n<li><strong>\u6700\u5c11\u9023\u63a5\uff08Least Connections\uff09<\/strong>\uff1a\u5c07\u8acb\u6c42\u5206\u914d\u7d66\u7576\u524d\u9023\u63a5\u6578\u6700\u5c11\u7684\u4f3a\u670d\u5668\uff0c\u9069\u5408\u8655\u7406\u9577\u9023\u63a5\u7684\u61c9\u7528\u3002<\/li>\n<li><strong>IP \u54c8\u5e0c\uff08IP Hash\uff09<\/strong>\uff1a\u6839\u64da\u5ba2\u6236\u7aef\u7684 IP \u5730\u5740\u5c07\u8acb\u6c42\u5206\u914d\u7d66\u7279\u5b9a\u7684\u4f3a\u670d\u5668\uff0c\u9069\u5408\u9700\u8981\u6703\u8a71\u4fdd\u6301\u7684\u61c9\u7528\u3002<\/li>\n<\/ul>\n<h2 id=\"%e8%a8%ad%e7%bd%ae-nginx-%e8%b2%a0%e8%bc%89%e5%9d%87%e8%a1%a1%e7%9a%84%e6%ad%a5%e9%a9%9f-uQMabYmCjS\">\u8a2d\u7f6e Nginx \u8ca0\u8f09\u5747\u8861\u7684\u6b65\u9a5f<\/h2>\n<h3 id=\"1-%e5%ae%89%e8%a3%9d-nginx-uQMabYmCjS\">1. \u5b89\u88dd Nginx<\/h3>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5728\u4f3a\u670d\u5668\u4e0a\u5b89\u88dd Nginx\u3002\u4ee5 Ubuntu \u70ba\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>sudo apt update\nsudo apt install nginx<\/code><\/pre>\n<h3 id=\"2-%e9%85%8d%e7%bd%ae-nginx-uQMabYmCjS\">2. \u914d\u7f6e Nginx<\/h3>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u60a8\u9700\u8981\u7de8\u8f2f Nginx \u7684\u914d\u7f6e\u6587\u4ef6\u3002\u901a\u5e38\uff0c\u914d\u7f6e\u6587\u4ef6\u4f4d\u65bc \/etc\/nginx\/nginx.conf\u3002\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6253\u958b\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<pre><code>sudo nano \/etc\/nginx\/nginx.conf<\/code><\/pre>\n<p>\u5728 http \u5340\u584a\u4e2d\uff0c\u6dfb\u52a0 upstream \u6a21\u584a\u4f86\u5b9a\u7fa9\u4f3a\u670d\u5668\u96c6\u7fa4\uff1a<\/p>\n<pre><code>http {\n    upstream backend {\n        server backend1.example.com;\n        server backend2.example.com;\n        server backend3.example.com;\n    }\n    \n    server {\n        listen 80;\n        location \/ {\n            proxy_pass http:\/\/backend;\n        }\n    }\n}<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u914d\u7f6e\u4e2d\uff0c\u60a8\u9700\u8981\u5c07 backend1.example.com\u3001backend2.example.com \u548c backend3.example.com \u66ff\u63db\u70ba\u5be6\u969b\u7684\u4f3a\u670d\u5668\u5730\u5740\u3002<\/p>\n<h3 id=\"3-%e6%b8%ac%e8%a9%a6%e9%85%8d%e7%bd%ae-uQMabYmCjS\">3. \u6e2c\u8a66\u914d\u7f6e<\/h3>\n<p>\u5728\u4fdd\u5b58\u914d\u7f6e\u6587\u4ef6\u5f8c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6e2c\u8a66 Nginx \u914d\u7f6e\u662f\u5426\u6b63\u78ba\uff1a<\/p>\n<pre><code>sudo nginx -t<\/code><\/pre>\n<h3 id=\"4-%e9%87%8d%e6%96%b0%e5%95%9f%e5%8b%95-nginx-uQMabYmCjS\">4. \u91cd\u65b0\u555f\u52d5 Nginx<\/h3>\n<p>\u5982\u679c\u914d\u7f6e\u6b63\u78ba\uff0c\u60a8\u53ef\u4ee5\u91cd\u65b0\u555f\u52d5 Nginx \u4ee5\u4f7f\u66f4\u6539\u751f\u6548\uff1a<\/p>\n<pre><code>sudo systemctl restart nginx<\/code><\/pre>\n<h2 id=\"%e7%9b%a3%e6%8e%a7%e5%92%8c%e7%b6%ad%e8%ad%b7-uQMabYmCjS\">\u76e3\u63a7\u548c\u7dad\u8b77<\/h2>\n<p>\u8a2d\u7f6e\u5b8c\u6210\u5f8c\uff0c\u60a8\u9700\u8981\u5b9a\u671f\u76e3\u63a7\u4f3a\u670d\u5668\u7684\u6027\u80fd\u548c\u6d41\u91cf\u3002\u53ef\u4ee5\u4f7f\u7528 Nginx \u7684\u65e5\u8a8c\u529f\u80fd\u4f86\u5206\u6790\u8acb\u6c42\u548c\u932f\u8aa4\uff0c\u4e26\u6839\u64da\u9700\u8981\u8abf\u6574\u914d\u7f6e\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-uQMabYmCjS\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u5730\u5728 Nginx \u4e2d\u8a2d\u7f6e\u8ca0\u8f09\u5747\u8861\uff0c\u5f9e\u800c\u63d0\u9ad8\u7db2\u7ad9\u7684\u53ef\u7528\u6027\u548c\u6027\u80fd\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u7db2\u7ad9\u9084\u662f\u5927\u578b\u61c9\u7528\uff0c\u8ca0\u8f09\u5747\u8861\u90fd\u662f\u4e00\u500b\u4e0d\u53ef\u6216\u7f3a\u7684\u6280\u8853\u3002\u82e5\u60a8\u9700\u8981\u66f4\u591a\u6709\u95dc <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u7684\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\uff0c\u4e86\u89e3\u66f4\u591a\u95dc\u65bc\u4f3a\u670d\u5668\u548c\u96f2\u670d\u52d9\u7684\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528 Nginx \u8a2d\u7f6e\u8ca0\u8f09\u5747\u8861\uff0c\u63d0\u5347\u7db2\u7ad9\u6027\u80fd\u8207\u7a69\u5b9a\u6027\uff0c\u9069\u5408\u65b0\u624b\u7684\u8a73\u7d30\u6b65\u9a5f\u8207\u5be6\u7528\u6280\u5de7\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-50993","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50993","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=50993"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50993\/revisions"}],"predecessor-version":[{"id":50994,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50993\/revisions\/50994"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=50993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=50993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=50993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}