{"id":3659,"date":"2021-09-26T17:11:34","date_gmt":"2021-09-26T09:11:34","guid":{"rendered":"https:\/\/wyxxt.org.cn\/?p=3659"},"modified":"2023-12-04T14:50:38","modified_gmt":"2023-12-04T06:50:38","slug":"nginx","status":"publish","type":"post","link":"https:\/\/wyxxt.org.cn\/?p=3659","title":{"rendered":"nginx"},"content":{"rendered":"<h1>\u67b6\u6784\u7684\u6f14\u8fdb<\/h1>\n<p>\u6ca1\u6709\u6700\u597d\u7684\u67b6\u6784\uff0c\u53ea\u6709\u6700\u5408\u9002\u7684\u67b6\u6784<\/p>\n<h2>\u6280\u672f\u548c\u4e1a\u52a1\u7684\u5173\u7cfb<\/h2>\n<p>\u6280\u672f\u662f\u4e3a\u4e1a\u52a1\u670d\u52a1\u7684\uff0c\u4e92\u8054\u7f51\u516c\u53f8\u4e1a\u52a1\u9760\u6280\u672f\u652f\u6491<\/p>\n<h2>\u4e09\u9ad8\u67b6\u6784<\/h2>\n<p><code>\u9ad8\u53ef\u7528<\/code> <code>\u9ad8\u5e76\u53d1<\/code> <code>\u9ad8\u6027\u80fd<\/code><\/p>\n<h3>\u9ad8\u53ef\u7528<\/h3>\n<p><code>\u9ad8\u53ef\u7528<\/code> \u4fdd\u8bc1\u670d\u52a1\u7a33\u5b9a\uff0c\u91cd\u70b9\u662f\u89e3\u51b3\u5355\u70b9\u6545\u969c\u95ee\u9898\u3002\u56e0\u4e3a\u5355\u70b9\u6545\u969c\uff0c\u6240\u4ee5\u8981\u6709\u5907\u673a\uff0c\u8981\u6709\u81ea\u52a8\u6545\u969c\u8f6c\u79fb\u3002\u4e3b\u5907\u67b6\u6784\u6216\u4e3b\u4ece\u67b6\u6784<\/p>\n<p>lvs + keepalive \u5b9e\u73b0\u4e3b\u5907\u5207\u6362<\/p>\n<p>nginx + keepalive \u5b9e\u73b0\u4e3b\u5907\u5207\u6362<\/p>\n<p>mysql \u4e3b\u4ece MHA\u81ea\u52a8\u6545\u969c\u8f6c\u79fb \uff08\u4e3b\u4ece\u590d\u5236\u5f02\u6b65\uff0c\u6709\u4e22\u6570\u636e\u98ce\u9669\uff09<\/p>\n<p>redis \u4e3b\u4ece sentinel\u81ea\u52a8\u6545\u969c\u8f6c\u79fb \uff08\u4e3b\u4ece\u590d\u5236\u5f02\u6b65\uff0c\u6709\u4e22\u6570\u636e\u98ce\u9669\uff09<\/p>\n<h3>\u9ad8\u5e76\u53d1<\/h3>\n<p><code>\u9ad8\u5e76\u53d1<\/code> \u63d0\u5347qps tps\uff0c\u5355\u70b9\u5e76\u53d1\u4e00\u5b9a\u6709\u4e0a\u9650\u3002\u4e3b\u4ece\u67b6\u6784\u3001\u5206\u7247\u67b6\u6784<\/p>\n<p>\u4e3b\u4ece\u67b6\u6784\u8bfb\u5199\u5206\u79bb\u53ef\u4ee5\u6709\u6548\u89e3\u51b3\u8bfb\u9ad8\u5e76\u53d1\uff0c\u5199\u4ecd\u7136\u6709\u5355\u70b9\u9650\u5236\u3002\u56e0\u6b64\u884d\u751f\u51fa\u5206\u7247\u673a\u5236<\/p>\n<p>\u4e3b\u8282\u70b9\u6309\u7167\u4e00\u5b9a\u89c4\u5219\u8fdb\u884c\u5206\u7247\uff0c\u5982hash\u5206\u7247\uff0c\u6bcf\u4e00\u4e2a\u5206\u7247\u5728\u6709\u81ea\u5df1\u7684\u4ece\u8282\u70b9\u3002<\/p>\n<p>redis cluster hash\u69fd<\/p>\n<p>mysql \u6839\u636e\u7528\u6237id \u6216 \u8ba2\u5355id\u8fdb\u884c\u54c8\u5e0c\u5206\u7247\uff08\u4ee3\u7801\u5b9e\u73b0\u6216\u4f7f\u7528proxy\uff09<\/p>\n<p>\u6b64\u5904\u53ef\u53c2\u8003akf\u5212\u5206\u539f\u5219\uff0cx\u8f74\u5148\u8fdb\u884c\u4e1a\u52a1\u5212\u5206\uff0cy\u8f74\u518d\u8fdb\u884c\u6570\u636e\u5206\u7247\uff0cz\u8f74\u4e3b\u4ece\u67b6\u6784<\/p>\n<h3>\u9ad8\u6027\u80fd<\/h3>\n<p><code>\u9ad8\u6027\u80fd<\/code> \u5f00\u59cb\u8ffd\u6c42\u5bf9cpu\u7684\u6027\u80fd\u538b\u69a8\u4e86\uff0c\u8981\u6df1\u5165\u4e86\u89e3\u8ba1\u7b97\u673a\u5e95\u5c42\u539f\u7406\u3002\u5bf9cpu\u3001\u5185\u5b58\u3001\u786c\u76d8\u3001\u7f51\u7edc\u3001io\u7b49\u8981\u6709\u6e05\u6670\u7684\u8ba4\u8bc6\uff0c\u5bf9linux\u5185\u6838\u8981\u6709\u57fa\u672c\u7684\u4e86\u89e3<\/p>\n<p>\u8fdb\u7a0b\u5185<code>\u9501<\/code> \u9501\u7684\u5347\u7ea7\u673a\u5236\uff0ccas\u8f7b\u91cf\u7ea7\u9501\u3001\u91cd\u91cf\u7ea7\u9501\uff0catomic \u539f\u8bed<\/p>\n<p>kafka\u9ad8\u6027\u80fd\u5e95\u5c42\u903b\u8f91\uff0c\u6279\u91cf\u3001\u5f02\u6b65\u3001mmap\u3001\u987a\u5e8f\u5199\u5165\u3001sendfile\uff08\u96f6\u62f7\u8d1d\uff09\u3001\u5185\u6838\u5237\u76d8\u673a\u5236<\/p>\n<p>msyql\u9ad8\u6027\u80fd\u5e95\u5c42\u903b\u8f91\uff0cwal\uff08write ahead log\uff09\u3001\u5185\u6838\u5237\u76d8\u673a\u5236\u3001mvcc\u3001\u9501<\/p>\n<p>redis\u9ad8\u6027\u80fd\u5e95\u5c42\u903b\u8f91\uff0c\u57fa\u4e8e\u5185\u5b58\uff0c\u8ba1\u7b97\u60f3\u6570\u636e\u79fb\u52a8\uff0c6\u4ee5\u540eiothread\u4e3a\u591a\u7ebf\u7a0b\uff0c\u7f51\u7edcIO epoll\u673a\u5236\uff0ccopy on write \u673a\u5236<\/p>\n<h2>\u521d\u521b\u516c\u53f8\u67b6\u6784<\/h2>\n<p>\u65e0\u8fd0\u7ef4\u56e2\u961f\uff0c\u8d2d\u4e70\u4e91\u670d\u52a1<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.wyxxt.org.cn\/images\/2021\/09\/27\/image-20210927132823715.png\" alt=\"image-20210927132823715\" \/><\/p>\n<p>4\u53f0esc\u7ec4\u6210\u9ad8\u53ef\u7528\u67b6\u6784<\/p>\n<p>\u6b64\u5904\u53ef\u4ee5\u660e\u663e\u770b\u51fa\uff0cnginx\u7684\u8d1f\u8f7d\u5747\u8861\u7b56\u7565\u5f88\u91cd\u8981\u3002\u5e76\u4e14\u518d\u4ee5\u540e\u7684\u67b6\u6784\u6f14\u8fdb\u4e2d\uff0cnginx\u4e00\u76f4\u5728\u67b6\u6784\u4e2d\uff0c\u5f88\u91cd\u8981\u3002\u6240\u4ee5\uff0c\u63a5\u4e0b\u6765\u91cd\u70b9\u8ba8\u8bba\u4e0bnginx \u7684\u8d1f\u8f7d\u5747\u8861\u914d\u7f6e<\/p>\n<h1>nginx\u8d1f\u8f7d\u5747\u8861<\/h1>\n<p>\u5c06\u591a\u53f0\u670d\u52a1\u7ec4\u6210\u96c6\u7fa4\uff0c\u524d\u7aef\u4f7f\u7528nginx\u5b9e\u73b0\u8d1f\u8f7d\u5747\u8861\uff0c\u5c06\u8bf7\u6c42\u5206\u6563\u6253\u5230\u540e\u7aef\u670d\u52a1\u4e2d<\/p>\n<p>\u5b9e\u73b0\u8d1f\u8f7d\u5747\u8861\u7684\u5206\u53d1\uff0c\u5bf9\u4e09\u9ad8\u4f1a\u6709\u4e00\u5b9a\u7684\u63d0\u5347<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.wyxxt.org.cn\/images\/2021\/09\/27\/image-20210927141318907.png\" alt=\"image-20210927141318907\" \/><\/p>\n<p>Nginx\u8981\u5b9e\u73b0\u8d1f\u8f7d\u5747\u8861\u9700\u8981\u7528\u5230proxy_pass\u4ee3\u7406\u6a21\u5757\u914d\u7f6e<\/p>\n<p>Nginx\u8d1f\u8f7d\u5747\u8861\u4e0eNginx\u4ee3\u7406\u4e0d\u540c\u5730\u65b9\u5728\u4e8e<\/p>\n<p>Nginx\u4ee3\u7406\u4ec5\u4ee3\u7406\u4e00\u53f0\u670d\u52a1\u5668\uff0c\u800cNginx\u8d1f\u8f7d\u5747\u8861\u5219\u662f\u5c06\u5ba2\u6237\u7aef\u8bf7\u6c42\u4ee3\u7406\u8f6c\u53d1\u81f3\u4e00\u7ec4upstream\u865a\u62df\u670d\u52a1\u6c60<\/p>\n<p>Nginx\u53ef\u4ee5\u914d\u7f6e\u4ee3\u7406\u591a\u53f0\u670d\u52a1\u5668\uff0c\u5f53\u4e00\u53f0\u670d\u52a1\u5668\u5b95\u673a\u4e4b\u540e\uff0c\u4ecd\u80fd\u4fdd\u6301\u7cfb\u7edf\u53ef\u7528\u3002<\/p>\n<h2>upstream \u914d\u7f6e<\/h2>\n<h3>\u76f8\u5173\u53c2\u6570<\/h3>\n<ul>\n<li>server \u53cd\u54cd\u670d\u52a1\u5730\u5740 + \u7aef\u53e3<\/li>\n<li>weight \u6743\u91cd<\/li>\n<li>max_fails \u5931\u8d25\u591a\u5c11\u6b21 \u8ba4\u4e3a\u4e3b\u673a\u5df2\u7ecf\u6302\u6389\uff0c\u5219\u8e22\u51fa<\/li>\n<li>fail_timeout \u8e22\u51fa\u540e\u91cd\u65b0\u63a2\u6d4b\u65f6\u95f4<\/li>\n<li>slow_start \u5f53\u8282\u70b9\u6062\u590d\uff0c\u4e0d\u7acb\u5373\u52a0\u5165\uff0c\u800c\u662f\u7b49\u5f85 slow_start \u540e\u52a0\u5165\u670d\u52a1\u961f\u5217<\/li>\n<li>backup \u5907\u7528\u670d\u52a1 \u5f53\u914d\u7f6e\u7684\u96c6\u7fa4\u6240\u6709\u670d\u52a1\u6302\u6389\u4e86\uff0c\u542f\u7528\u5f53\u524d\u7684\u914d\u7f6e\u9879<\/li>\n<li>max_conns \u5141\u8bb8\u6700\u5927\u8fde\u63a5\u6570<\/li>\n<\/ul>\n<h3>\u8d1f\u8f7d\u5747\u8861\u7b97\u6cd5<\/h3>\n<ul>\n<li>ll + weight \u8f6e\u8be2+\u6743\u91cd\uff08\u9ed8\u8ba4\uff09<\/li>\n<li>ip_hash \u57fa\u4e8e\u516c\u7f51ip hash\uff0c\u53ef\u4fdd\u8bc1\u540c\u4e00\u4e2a\u7528\u6237\u8bbf\u95ee\u6c38\u8fdc\u6253\u5230\u540c\u4e00\u4e2a\u5b9e\u4f8b\u4e0a\u3002\u53ef\u80fd\u6709\u6d41\u91cf\u503e\u659c\u538b\u529b<\/li>\n<li>url_hash \u57fa\u4e8eurl hash\uff08\u7b2c\u4e09\u65b9\uff09\uff0c\u53ef\u4fdd\u8bc1\u540c\u4e00\u4e2aurl\u6c38\u8fdc\u6253\u5230\u4e00\u4e2a\u5b9e\u4f8b\u4e0a<\/li>\n<li>least_conn \u6700\u5c11\u8fde\u63a5\uff08\u7b2c\u4e09\u65b9\uff09<\/li>\n<li>least_time \u6700\u5c0f\u54cd\u5e94\u65f6\u95f4\uff08\u7b2c\u4e09\u65b9\uff09<\/li>\n<\/ul>\n<pre><code class=\"language-nginx line-numbers\">upstream node {\n  server 10.20.0.10:8080;\n  server 10.20.0.11:8080;\n}\n<\/code><\/pre>\n<pre><code class=\"language-nginx line-numbers\">location \/ {\n  proxy_pass http:\/\/node;\n}\n<\/code><\/pre>\n<h2> <\/h2>\n<ol>\n<li>\u9ed8\u8ba4\u8f6e\u8be2\n<pre><code class=\"language-nginx line-numbers\">upstream node {\n server 10.20.0.10:8080;\n server 10.20.0.11:8080;\n keepalive 65;\n}\n<\/code><\/pre>\n<\/li>\n<li>weight \u52a0\u6743\u8f6e\u8be2\n<pre><code class=\"language-nginx line-numbers\">upstream node {\n server 10.20.0.10:8080 weight=5;\n server 10.20.0.11:8080 weight=10;\n keepalive 65;\n}\n<\/code><\/pre>\n<\/li>\n<li>ip_hash\n<pre><code class=\"language-nginx line-numbers\">upstream node {\n ip_hash;\n server 10.20.0.10:8080;\n server 10.20.0.11:8080;\n keepalive 65;\n}\n<\/code><\/pre>\n<\/li>\n<li>url_hash\n<pre><code class=\"language-nginx line-numbers\">upstream node {\n hash $request_uri; # \u7b2c\u4e09\u65b9\n server 10.20.0.10:8080;\n server 10.20.0.11:8080;\n keepalive 65;\n}\n<\/code><\/pre>\n<\/li>\n<li>least_conn \u6700\u5c11\u8fde\u63a5\u6570\n<pre><code class=\"language-nginx line-numbers\">upstream node {\n least_conn; # \u7b2c\u4e09\u65b9\n server 10.20.0.10:8080;\n server 10.20.0.11:8080;\n keepalive 65;\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<h2>proxy_params<\/h2>\n<pre><code class=\"language-nginx line-numbers\">proxy_set_header Host $http_host;\nproxy_set_header X-Real-IP $remote_addr;\nproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n\nproxy_connect_timeout 30;\nproxy_send_timeout 60;\nproxy_read_timeout 60;\n\nproxy_buffering on;\nproxy_buffer_size 32k;\nproxy_buffers 4 128k;\n<\/code><\/pre>\n<h2>\u9759\u6001\u8d44\u6e90\u4ee3\u7406<\/h2>\n<p>http <code>&gt;<\/code> server <code>&gt;<\/code><\/p>\n<pre><code class=\"language-nginx line-numbers\">location ~* .*\\.(png|jpg|git)$ {\n    root \/data;\n}\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/oss.wyxxt.org.cn\/images\/2021\/09\/28\/image-20210928162725700.png\" alt=\"image-20210928162725700\" \/><\/p>\n<h2>\u52a8\u6001\u8d44\u6e90\u8d1f\u8f7d\u5747\u8861<\/h2>\n<p>http <code>&gt;<\/code><\/p>\n<pre><code class=\"language-nginx line-numbers\">upstream node {\n  server 172.16.213.133:9090;\n  server 172.16.213.134:9090;\n}\n<\/code><\/pre>\n<p>http <code>&gt;<\/code> server <code>&gt;<\/code><\/p>\n<pre><code class=\"language-nginx line-numbers\">location \/hello {\n  proxy_pass http:\/\/node;\n  include conf.d\/proxy_params;\n}\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/oss.wyxxt.org.cn\/images\/2021\/09\/28\/image-20210928163717292.png\" alt=\"image-20210928163717292\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.wyxxt.org.cn\/images\/2021\/09\/28\/image-20210928163731885.png\" alt=\"image-20210928163731885\" \/><\/p>\n<h2>\u52a8\u9759\u8d44\u6e90\u5206\u79bb<\/h2>\n<p>http <code>&gt;<\/code><\/p>\n<pre><code class=\"language-nginx line-numbers\">upstream node {\n        server 172.16.213.133:9090;\n        server 172.16.213.134:9090;\n    }\n\n    upstream static {\n        server 172.16.213.133:80; #\u9759\u6001\u8d44\u6e90\u670d\u52a1\u5668\n    }\n<\/code><\/pre>\n<p>http <code>&gt;<\/code> server <code>&gt;<\/code><\/p>\n<pre><code class=\"language-nginx line-numbers\">location \/hello {\n  proxy_pass http:\/\/node;\n  include conf.d\/proxy_params;\n}\n\nlocation \/images {\n  proxy_pass http:\/\/static;\n  include conf.d\/proxy_params;\n}\n<\/code><\/pre>\n<h1>nginx location\u5339\u914d\u987a\u5e8f<\/h1>\n<p><img decoding=\"async\" src=\"https:\/\/oss.wyxxt.org.cn\/images\/2021\/09\/28\/image-20210928170502687.png\" alt=\"image-20210928170502687\" \/><\/p>\n<pre><code class=\"language-nginx line-numbers\">location = \/ {} # = \u7cbe\u786e\u5339\u914d\nlocation \/ {} # \u666e\u901a\u5339\u914d\nlocation \/images {} # \u666e\u901a\u5339\u914d\nlocation ~\/documents {} # \u666e\u901a\u5339\u914d\uff0c\u533a\u5206\u5927\u5c0f\u5199 \uff5e\nlocation ^~ \/images\/ {} # \u666e\u901a\u5339\u914d\uff0c\u533a\u5206\u5927\u5c0f\u5199 ~ \u4ee5\/iamges\/\u5f00\u5934 ^\nlocation ~* \\.(git|jpg|png)$ {} # \u6b63\u5219\u5339\u914d ~* \u4ee5.git .jpg .png \u7ed3\u5c3e $\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u67b6\u6784\u7684\u6f14\u8fdb \u6ca1\u6709\u6700\u597d\u7684\u67b6\u6784\uff0c\u53ea\u6709\u6700\u5408\u9002\u7684\u67b6\u6784 \u6280\u672f\u548c\u4e1a\u52a1\u7684\u5173\u7cfb \u6280\u672f\u662f\u4e3a\u4e1a\u52a1\u670d\u52a1\u7684\uff0c\u4e92\u8054\u7f51\u516c\u53f8\u4e1a\u52a1\u9760\u6280\u672f\u652f\u6491  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[15],"tags":[390],"class_list":["post-3659","post","type-post","status-publish","format-standard","hentry","category-15","tag-390"],"_links":{"self":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/3659","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3659"}],"version-history":[{"count":2,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/3659\/revisions"}],"predecessor-version":[{"id":3663,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/3659\/revisions\/3663"}],"wp:attachment":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}