{"id":4606,"date":"2022-03-27T14:10:52","date_gmt":"2022-03-27T06:10:52","guid":{"rendered":"https:\/\/egonlin.com\/?p=4606"},"modified":"2022-03-27T14:10:52","modified_gmt":"2022-03-27T06:10:52","slug":"%e7%ac%ac%e4%ba%8c%e8%8a%82%ef%bc%9a%e5%b0%8f%e7%99%bd%e5%ad%a6nginx%e4%b9%8bnginx%e7%9a%84%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=4606","title":{"rendered":"\u7b2c\u4e8c\u8282\uff1a\u5c0f\u767d\u5b66nginx\u4e4bnginx\u7684\u6a21\u5757"},"content":{"rendered":"<h4>\u5c0f\u767d\u5b66nginx\u4e4bnginx\u7684\u6a21\u5757<\/h4>\n<blockquote>\n<p>nginx\u4e2d\u6709\u4e00\u4e9b\u6bd4\u8f83\u5e38\u89c1\u7684\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u5217\u51fa\u6765\u4e00\u4e9b\u5e38\u7528\u7684<\/p>\n<\/blockquote>\n<h5>1.\u76ee\u5f55\u7d22\u5f15<\/h5>\n<blockquote>\n<p>autoindex:\u5217\u51fa\u76ee\u5f55\u7d22\u5f15<\/p>\n<pre><code class=\"language-python\">\n[root@web01 module]# cat \/etc\/nginx\/conf.d\/autoindex.conf\nserver {\nlisten 80;\nserver_name module..com;\ncharset utf-8,gbk;<\/code><\/pre>\n<\/blockquote>\n<pre><code>location \/ {\n    root \/module;\n    autoindex on;\n    autoindex_exact_size off;\n    autoindex_localtime on;\n}<\/code><\/pre>\n<p>}<\/p>\n<pre><code>\n2.\u51c6\u5907\u5bf9\u5e94\u7684\u76ee\u5f55\uff0c\u5f80\u76ee\u5f55\u4e2d\u4e0a\u4f20\u4e00\u70b9\u6587\u4ef6\n```bash\nmkdir \/module\/{centos,ubuntu,redhat}\/ -p<\/code><\/pre>\n<p>3.\u68c0\u67e5\u8bed\u6cd5\u5e76\u91cd\u65b0\u52a0\u8f7dNginx<\/p>\n<pre><code class=\"language-bash\">nginx -t \nsystemctl restart nginx<\/code><\/pre>\n<h5>nginx\u72b6\u6001\u9875\u9762<\/h5>\n<pre><code class=\"language-bash\">location \/nginx_status {\n        stub_status;\n}<\/code><\/pre>\n<p>\u6700\u7ec8\u5c55\u793a\u7684\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-bash\">Active connections: 2 \nserver accepts handled requests\n        3           3   33 \nReading: 0 Writing: 1 Waiting: 1 \n\nActive connections  # \u5f53\u524d\u6d3b\u52a8\u5ba2\u6237\u7aef\u8fde\u63a5\u6570\uff0c\u5305\u62ecWaiting\u7b49\u5f85\u8fde\u63a5\u6570\u3002\naccepts             # \u5df2\u63a5\u53d7\u603b\u7684TCP\u8fde\u63a5\u6570\u3002\nhandled             # \u5df2\u5904\u7406\u603b\u7684TCP\u8fde\u63a5\u6570\u3002\nrequests            # \u5ba2\u6237\u7aef\u603b\u7684http\u8bf7\u6c42\u6570\u3002\n\nReading             # \u5f53\u524dnginx\u8bfb\u53d6\u8bf7\u6c42\u5934\u7684\u8fde\u63a5\u6570\u3002\nWriting             # \u5f53\u524dnginx\u5c06\u54cd\u5e94\u5199\u56de\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u6570\u3002\nWaiting             # \u5f53\u524d\u7b49\u5f85\u8bf7\u6c42\u7684\u7a7a\u95f2\u5ba2\u6237\u7aef\u8fde\u63a5\u6570\u3002<\/code><\/pre>\n<p>\u6ce8\u610f, \u4e00\u6b21TCP\u7684\u8fde\u63a5\uff0c\u53ef\u4ee5\u53d1\u8d77\u591a\u6b21http\u7684\u8bf7\u6c42, \u5982\u4e0b\u53c2\u6570\u53ef\u914d\u7f6e\u8fdb\u884c\u9a8c\u8bc1<\/p>\n<blockquote>\n<p>keepalive_timeout  0;   # \u7c7b\u4f3c\u4e8e\u5173\u95ed\u957f\u8fde\u63a5<br \/>\nkeepalive_timeout  65;  # 65s\u6ca1\u6709\u6d3b\u52a8\u5219\u65ad\u5f00\u8fde\u63a5<\/p>\n<\/blockquote>\n<h5>\u57fa\u4e8e\u6765\u6e90\u7684IP\u5730\u5740\u505a\u9650\u5236<\/h5>\n<p>1.\u62d2\u7edd<code>10.0.0.1<\/code>\u6765\u6e90IP\u8bbf\u95ee\uff0c\u5176\u4ed6\u4eba\u5141\u8bb8\u3002<\/p>\n<pre><code class=\"language-bash\">location \/nginx_status {\n      stub_status;\n      deny 10.0.0.1\/32;\n      allow all;\n}<\/code><\/pre>\n<p>2.\u5141\u8bb8<code>10.0.0.1<\/code>\u6765\u6e90IP\u8bbf\u95ee\uff0c\u5176\u4ed6\u4eba\u5168\u90e8\u62d2\u7edd\u3002<\/p>\n<pre><code class=\"language-bash\">location \/nginx_status {\n      stub_status;\n      allow 10.0.0.1\/32;\n      deny all;\n}<\/code><\/pre>\n<p>3.\u5b9e\u9645\u914d\u7f6e\u76d1\u63a7Nginx\u72b6\u6001\u65f6\uff0c\u4ec5\u5141\u8bb8\u8be5\u670d\u52a1\u5668\u7684\u56de\u73af\u5730\u5740\u8bbf\u95ee127.0.0.1<\/p>\n<pre><code class=\"language-bash\">location \/nginx_status {\n     stub_status;\n     allow 127.0.0.1;\n     deny all;\n}<\/code><\/pre>\n<h5>\u57fa\u4e8e\u7528\u6237\u5bc6\u7801\u7684\u8eab\u4efd\u9a8c\u8bc1<\/h5>\n<p>1.\u751f\u6210\u4e00\u4e2a\u5bc6\u7801\u6587\u4ef6\uff0c\u5bc6\u7801\u6587\u4ef6\u7684\u683c\u5f0f  name:password(\u52a0\u5bc6)  \uff08\u5efa\u8bae\u4f7f\u7528htpasswd\uff09  <\/p>\n<pre><code class=\"language-bash\">[root@web01 conf.d]# yum install httpd-tools -y\n[root@web01 conf.d]# htpasswd -c -b \/etc\/nginx\/auth_conf oldboy oldboy\n[root@web01 conf.d]# cat \/etc\/nginx\/auth_conf\noldboy:$apr1$Kp87VSae$658Nt5bm4iiblQkUvP7u61<\/code><\/pre>\n<p>2.\u914d\u7f6eNginx\uff0c\u9650\u5236\u5bf9\u5e94\u7684\u8d44\u6e90<\/p>\n<pre><code class=\"language-bash\">location \/download {\n    root \/module;\n    autoindex on;\n    autoindex_exact_size off;\n    autoindex_localtime on;\n\n    auth_basic &quot;Please Password!!!&quot;;\n    auth_basic_user_file \/etc\/nginx\/auth_conf;\n}<\/code><\/pre>\n<h5>nginx\u7684\u8fde\u63a5\u9650\u5236<\/h5>\n<blockquote>\n<p>\u8bbe\u7f6e\u5171\u4eab\u5185\u5b58\u533a\u57df\u548c\u7ed9\u5b9a\u952e\u503c\u7684\u6700\u5927\u5141\u8bb8\u8fde\u63a5\u6570\u3002\u8d85\u8fc7\u6b64\u9650\u5236\u65f6\uff0c\u670d\u52a1\u5668\u5c06\u8fd4\u56de\u9519\u8bef\u4ee5\u56de\u590d\u8bf7\u6c42<\/p>\n<\/blockquote>\n<p><strong>http\u6807\u7b7e\u6bb5\u5b9a\u4e49\u8fde\u63a5\u9650\u5236<\/strong><\/p>\n<pre><code class=\"language-bash\">http{\n    limit_conn_zone $binary_remote_addr zone=conn_zone:10m;\n}\nserver {\n    # \u540c\u4e00\u65f6\u523b\u53ea\u5141\u8bb8\u4e00\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\n    limit_conn conn_zone 1; \n\n    location \/ {\n        root \/code;\n        index index.html;\n    }<\/code><\/pre>\n<p>\u4f7f\u7528ab\u5de5\u5177\u8fdb\u884c\u538b\u529b\u6d4b\u8bd5<\/p>\n<pre><code class=\"language-bash\">[root@localhost ~]# yum install -y httpd-tools\n[root@localhost ~]# ab -n 500 -c 2  http:\/\/127.0.0.1\/index.html\n\n2019\/01\/14 11:11:22 [error] 29962#29962: *19 limiting connections by zone &quot;conn_zone&quot;, client: 47.110.176.164, server: www.localhost.com, request: &quot;GET \/ HTTP\/1.1&quot;, host: &quot;www.localhost.com&quot;\n2019\/01\/14 11:11:23 [error] 29962#29962: *19 limiting connections by zone &quot;conn_zone&quot;, client: 47.110.176.164, server: www.localhost.com, request: &quot;GET \/ HTTP\/1.1&quot;, host: &quot;www.localhost.com&quot;\n2019\/01\/14 11:11:25 [error] 29962#29962: *19 limiting connections by zone &quot;conn_zone&quot;, client: 47.110.176.164, server: www.localhost.com, request: &quot;GET \/ HTTP\/1.1&quot;, host: &quot;www.localhost.com&quot;\n2019\/01\/14 11:11:25 [error] 29962#29962: *19 limiting connections by zone &quot;conn_zone&quot;, client: 47.110.176.164, server: www.localhost.com, request: &quot;GET \/ HTTP\/1.1&quot;, host: &quot;www.localhost.com&quot;<\/code><\/pre>\n<blockquote>\n<p>\u8bbe\u7f6e\u5171\u4eab\u5185\u5b58\u533a\u57df\u548c\u8bf7\u6c42\u7684\u6700\u5927\u7a81\u53d1\u5927\u5c0f\u3002\u8fc7\u591a\u7684\u8bf7\u6c42\u88ab\u5ef6\u8fdf\uff0c\u76f4\u5230\u5b83\u4eec\u7684\u6570\u91cf\u8d85\u8fc7\u6700\u5927\u7a81\u53d1\u5927\u5c0f\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u8bf7\u6c42\u4ee5\u9519\u8bef\u7ec8\u6b62\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6700\u5927\u7a81\u53d1\u5927\u5c0f\u7b49\u4e8e\u96f6\u3002<\/p>\n<\/blockquote>\n<p>1.\u5b9a\u4e49\u9650\u5236\u7684key\uff08\u57fa\u4e8e\u4ec0\u4e48\u6765\u505a\u9650\u5236\uff0cIP\u5730\u5740\uff09<\/p>\n<pre><code class=\"language-bash\">[root@web01 conf.d]# cat test1.oldboy.com.conf \nlimit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r\/s;\nserver {\n    listen 80;\n    server_name test1.oldboy.com;\n\n    limit_req zone=req_zone burst=5 nodelay;\n    limit_req_status 412;\n    error_page 412 \/err.html;    #\u8fd9\u4e2a\u6587\u4ef6\u5fc5\u987b\u5b58\u5728\/code\/test1\/err.html\n\n    location \/ {\n        root \/code\/test1;\n        index index.html;\n    }\n}<\/code><\/pre>\n<p>2.\u586b\u5199hosts\u57df\u540d\u89e3\u6790<\/p>\n<pre><code class=\"language-bash\">echo &quot;10.0.0.7 test1.oldboy.com&quot; &gt;&gt; \/etc\/hosts<\/code><\/pre>\n<p>3.\u8fdb\u884c\u7b80\u5355\u7684\u538b\u529b\u6d4b\u8bd5<\/p>\n<pre><code class=\"language-bash\"> ab -n 50 -c 20 http:\/\/test1.oldboy.com\/index.html<\/code><\/pre>\n<h5>nginx location<\/h5>\n<p>1.Location\u8bed\u6cd5\u793a\u4f8b<\/p>\n<pre><code class=\"language-bash\">location [=|^~|~|~*|!~|!~*|\/] \/uri\/ { ...\n}<\/code><\/pre>\n<p>\u5339\u914d\u7b26 \u5339\u914d\u89c4\u5219                    \u4f18\u5148\u7ea7<br \/>\n=       \u7cbe\u786e\u5339\u914d                        1<br \/>\n^~      \u4ee5\u67d0\u4e2a\u5b57\u7b26\u4e32\u5f00\u5934                2<br \/>\n~       \u533a\u5206\u5927\u5c0f\u5199\u7684\u6b63\u5219\u5339\u914d          3<br \/>\n~<em>      \u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u6b63\u5219\u5339\u914d         4<br \/>\n!~      \u533a\u5206\u5927\u5c0f\u5199\u4e0d\u5339\u914d\u7684\u6b63\u5219         5<br \/>\n!~<\/em>     \u4e0d\u533a\u5206\u5927\u5c0f\u5199\u4e0d\u5339\u914d\u7684\u6b63\u5219        6<br \/>\n\/       \u901a\u7528\u5339\u914d\uff0c\u4efb\u4f55\u8bf7\u6c42\u90fd\u4f1a\u5339\u914d\u5230  7<\/p>\n<p><strong>\u901a\u7528\u5339\u914d\uff0c\u4efb\u4f55\u8bf7\u6c42\u90fd\u4f1a\u5339\u914d\u5230<\/strong><\/p>\n<pre><code class=\"language-bash\">location \/ {\n    ...\n}<\/code><\/pre>\n<p><strong>\u4e25\u683c\u533a\u5206\u5927\u5c0f\u5199\uff0c\u5339\u914d\u4ee5.php\u7ed3\u5c3e\u7684\u90fd\u8d70\u8fd9\u4e2alocation<\/strong><\/p>\n<pre><code class=\"language-bash\">location ~ \\.php$ {\n    ...\n}<\/code><\/pre>\n<p><strong>\u4e25\u683c\u533a\u5206\u5927\u5c0f\u5199\uff0c\u5339\u914d\u4ee5.jsp\u7ed3\u5c3e\u7684\u90fd\u8d70\u8fd9\u4e2alocation<\/strong><\/p>\n<pre><code class=\"language-bash\">location ~ \\.jsp$ {\n    ...\n}<\/code><\/pre>\n<p><strong>\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u5339\u914d\uff0c\u53ea\u8981\u7528\u6237\u8bbf\u95ee.jpg,gif,png,js,css \u90fd\u8d70\u8fd9\u6761location<\/strong><\/p>\n<pre><code class=\"language-bash\">location ~* .*\\.(jpg|gif|png|js|css|mp4)$ {\n    ...\n}<\/code><\/pre>\n<p><strong>\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u5339\u914d<\/strong><\/p>\n<pre><code class=\"language-bash\">location ~* &quot;\\.(sql|bak|tgz|tar.gz|.git)$&quot; {\n    ...\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5c0f\u767d\u5b66nginx\u4e4bnginx\u7684\u6a21\u5757 nginx\u4e2d\u6709\u4e00\u4e9b\u6bd4\u8f83\u5e38\u89c1\u7684\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u5217\u51fa\u6765\u4e00\u4e9b\u5e38\u7528\u7684 1.\u76ee\u5f55\u7d22\u5f15  [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[352,351],"tags":[],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/4606"}],"collection":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4606"}],"version-history":[{"count":0,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/4606\/revisions"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4606"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}