{"id":321,"date":"2021-07-28T14:28:43","date_gmt":"2021-07-28T06:28:43","guid":{"rendered":"http:\/\/egonlin.com\/?p=321"},"modified":"2024-03-21T15:53:34","modified_gmt":"2024-03-21T07:53:34","slug":"%e7%ac%ac%e4%ba%8c%e8%8a%82%ef%bc%9anginx%e6%a8%a1%e5%9d%97%e6%89%a9%e5%b1%95","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=321","title":{"rendered":"\u7b2c\u4e8c\u8282\uff1anginx\u6a21\u5757\u6269\u5c55"},"content":{"rendered":"<h2>\u4e00\u3001nginx\u56de\u987e<\/h2>\n<h3>1.\u5b89\u88c5<\/h3>\n<pre><code class=\"language-bash\">1.epol\u6e90\u5b89\u88c5\n2.\u5b98\u65b9\u6e90\u5b89\u88c5\n3.\u6e90\u7801\u5305\u5b89\u88c5\n    1\uff09\u4e0b\u8f7d\n    2\uff09\u89e3\u538b\n    3\uff09\u751f\u6210\n    4\uff09\u7f16\u8bd1\n    5\uff09\u5b89\u88c5<\/code><\/pre>\n<h3>2.nginx\u914d\u7f6e\u6587\u4ef6<\/h3>\n<pre><code class=\"language-bash\">[root@web01 ~]# cat \/etc\/nginx\/nginx.conf\n##################\u6838\u5fc3\u6a21\u5757###############\nuser  www;\nworker_processes  1;\nerror_log  \/var\/log\/nginx\/error.log warn;\npid        \/var\/run\/nginx.pid;\n#################\u4e8b\u4ef6\u9a71\u52a8\u6a21\u5757###########\nevents {\n    worker_connections  1024;\n}\n################http\u5185\u6838\u6a21\u5757############\nhttp {\n    include       \/etc\/nginx\/mime.types;\n    default_type  application\/octet-stream;\n    log_format  main  &#039;$remote_addr - $remote_user [$time_local] &quot;$request&quot; &#039;\n                      &#039;$status $body_bytes_sent &quot;$http_referer&quot; &#039;\n                      &#039;&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot;&#039;;\n\n    access_log  \/var\/log\/nginx\/access.log  main;\n    sendfile        on;\n    #tcp_nopush     on;\n    keepalive_timeout  65;\n    #tcp_nodelay     on;\n    #gzip  on;\n\n    include \/etc\/nginx\/conf.d\/*.conf;\n\n    server {\n        listen       80;\n        server_name  localhost;\n        charset utf8;\n        #access_log  \/var\/log\/nginx\/host.access.log  main;\n\n        location \/ {\n            root   \/usr\/share\/nginx\/html;\n            index  index.html index.htm;\n        }\n\n        location \/download {\n            root   \/usr\/share\/nginx\/html;\n            index  index.html index.htm;\n        }\n    }\n}<\/code><\/pre>\n<h2>\u4e8c\u3001Nginx\u865a\u62df\u4e3b\u673a<\/h2>\n<h3>1.\u865a\u62df\u4e3b\u673a\u65b9\u5f0f<\/h3>\n<pre><code class=\"language-bash\">1.\u57fa\u4e8e\u591aIP\u7684\u65b9\u5f0f\n2.\u57fa\u4e8e\u591a\u7aef\u53e3\u7684\u65b9\u5f0f\n3.\u57fa\u4e8e\u591a\u57df\u540d\u7684\u65b9\u5f0f<\/code><\/pre>\n<h3>2.\u57fa\u4e8e\u591aIP\u7684\u65b9\u5f0f<\/h3>\n<h4>0\uff09\u7f51\u5361\u6dfb\u52a0\u5b50IP<\/h4>\n<pre><code class=\"language-bash\">[root@web01 ~]# ifconfig eth0:1 10.0.0.3\/24\n[root@web03 ~]# ip addr add 10.0.0.91\/24 dev eth0<\/code><\/pre>\n<h4>1\uff09\u7b2c\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6<\/h4>\n<pre><code class=\"language-bash\">[root@web03 \/etc\/nginx\/conf.d]# vim addr1.conf \nserver {\n        listen 10.0.0.9:80;\n\n        location \/ {\n                root \/code\/addr1;\n                index index.html;\n        }\n}<\/code><\/pre>\n<h4>2\uff09\u7b2c\u4e8c\u4e2a\u914d\u7f6e\u6587\u4ef6<\/h4>\n<pre><code class=\"language-bash\">[root@web03 \/etc\/nginx\/conf.d]# vim addr2.conf \nserver {\n        listen 10.0.0.91:80;\n\n        location \/ {\n                root \/code\/addr2;\n                index index.html;\n        }\n}<\/code><\/pre>\n<h4>3\uff09\u6839\u636e\u914d\u7f6e\u6587\u4ef6\u914d\u7f6e\u73af\u5883<\/h4>\n<pre><code class=\"language-bash\">[root@web03 \/etc\/nginx\/conf.d]# mkdir \/code\n[root@web03 \/etc\/nginx\/conf.d]# mkdir \/code\/addr1\n[root@web03 \/etc\/nginx\/conf.d]# mkdir \/code\/addr2\n[root@web03 \/etc\/nginx\/conf.d]# echo &quot;9&quot; &gt;\/code\/addr1\/index.html\n[root@web03 \/etc\/nginx\/conf.d]# echo &quot;91&quot; &gt;\/code\/addr2\/index.html<\/code><\/pre>\n<h4>4\uff09\u68c0\u67e5\u914d\u7f6e<\/h4>\n<pre><code class=\"language-bash\">[root@web01 ~]# nginx -t\nnginx: the configuration file \/etc\/nginx\/nginx.conf syntax is ok\nnginx: configuration file \/etc\/nginx\/nginx.conf test is successful<\/code><\/pre>\n<h4>5\uff09\u91cd\u542f\u8bbf\u95ee<\/h4>\n<pre><code class=\"language-bash\">[root@web01 ~]# systemctl restart nginx\n\nhttp:\/\/10.0.0.7\/\nhttp:\/\/10.0.0.3\/<\/code><\/pre>\n<h3>3.\u57fa\u4e8e\u591a\u7aef\u53e3\u7684\u65b9\u5f0f<\/h3>\n<h4>1\uff09\u7b2c\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6<\/h4>\n<pre><code class=\"language-bash\">[root@web01 ~]# cat \/etc\/nginx\/conf.d\/game.conf\nserver {\n    listen 80;\n    server_name localhost;\n\n    location \/ {\n    root \/code\/tuixiangzi;\n    index index.html;\n    }\n}<\/code><\/pre>\n<h4>2\uff09\u7b2c\u4e8c\u4e2a\u914d\u7f6e\u6587\u4ef6<\/h4>\n<pre><code class=\"language-bash\">[root@web01 ~]# cat \/etc\/nginx\/conf.d\/gametwo.conf\nserver {\n    listen 81;\n    server_name localhost;\n\n    location \/ {\n    root \/code\/tank;\n    index index.html;\n    }\n}<\/code><\/pre>\n<h4>3\uff09\u68c0\u67e5\u914d\u7f6e<\/h4>\n<pre><code class=\"language-bash\">[root@web01 ~]# nginx -t\nnginx: the configuration file \/etc\/nginx\/nginx.conf syntax is ok\nnginx: configuration file \/etc\/nginx\/nginx.conf test is successful<\/code><\/pre>\n<h4>4\uff09\u91cd\u542f\u8bbf\u95ee<\/h4>\n<pre><code class=\"language-bash\">[root@web01 ~]# systemctl restart nginx\n\nhttp:\/\/10.0.0.7\/\nhttp:\/\/10.0.0.7:81\/<\/code><\/pre>\n<h3>4.\u57fa\u4e8e\u591a\u57df\u540d\u7684\u65b9\u5f0f<\/h3>\n<h4>1\uff09\u7b2c\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6<\/h4>\n<pre><code class=\"language-bash\">[root@web01 ~]# cat \/etc\/nginx\/conf.d\/game.conf\nserver {\n    listen 80;\n    server_name www.tuixiangzi.com;\n\n    location \/ {\n        root \/code\/tuixiangzi;\n        index index.html;\n    }\n}<\/code><\/pre>\n<h4>2\uff09\u7b2c\u4e8c\u4e2a\u914d\u7f6e\u6587\u4ef6<\/h4>\n<pre><code class=\"language-bash\">[root@web01 ~]# cat \/etc\/nginx\/conf.d\/gametwo.conf\nserver {\n    listen 80;\n    server_name www.tank.com;\n\n    location \/ {\n        root \/code\/tank;\n        index index.html;\n    }\n}<\/code><\/pre>\n<h4>3\uff09\u68c0\u67e5\u914d\u7f6e<\/h4>\n<pre><code class=\"language-bash\">[root@web01 ~]# nginx -t\nnginx: the configuration file \/etc\/nginx\/nginx.conf syntax is ok\nnginx: configuration file \/etc\/nginx\/nginx.conf test is successful<\/code><\/pre>\n<h4>4\uff09\u91cd\u542f<\/h4>\n<pre><code class=\"language-bash\">[root@web01 ~]# systemctl restart nginx<\/code><\/pre>\n<h4>5\uff09\u914d\u7f6e\u672c\u5730hosts\u8bbf\u95ee<\/h4>\n<pre><code class=\"language-bash\">#\u4fee\u6539windows\u7684hosts\u6587\u4ef6\nC:\\Windows\\System32\\drivers\\etc\\hosts\n10.0.0.7 www.tuixiangzi.com www.tank.com\n\n#\u8bbf\u95ee\nhttp:\/\/www.tuixiangzi.com\/\nhttp:\/\/www.tank.com\/<\/code><\/pre>\n<h3>\u603b\u7ed3\uff1a<\/h3>\n<p>\u200b   \u5728nginx\u7684\u865a\u62df\u4e3b\u673a\u7c7b\u578b\u4e2d\uff0c\u57fa\u4e8e\u57df\u540d\u7684\u865a\u62df\u4e3b\u673a\u5e94\u7528\u6700\u4e3a\u5e7f\u6cdb\u3002<\/p>\n<pre><code class=\"language-bash\">1\uff0c\u57fa\u4e8e\u7aef\u53e3\u548cIP\u7684\u865a\u62df\u4e3b\u673a\u7c7b\u578b\uff0c\u7528\u6237\u4f53\u9a8c\u4e0d\u597d\u3002\n2\uff0c\u57fa\u4e8eIP\u7c7b\u578b\u7684\u865a\u62df\u4e3b\u673a\uff0c\u5982\u5728\u516c\u7f51\u73af\u5883\u4e0b\u4f7f\u7528\uff0c\u4f1a\u4ea7\u751f\u989d\u5916\u8d39\u7528\u3002\n3\uff0c\u57fa\u4e8e\u57df\u540d\u7684\u865a\u62df\u4e3b\u673a\uff0c\u4e00\u6b21\u4ed8\u8d39\uff0c\u7528\u6237\u4f53\u9a8c\u8f83\u597d\u3002\n\n\u4e0d\u7ba1\u4f7f\u7528\u54ea\u79cd\u865a\u62df\u4e3b\u673a\uff0c\u6700\u7ec8\u4f7f\u7528\u7684\u90fd\u662f\u672c\u5730\u4e3b\u673a\u73b0\u6709\u8d44\u6e90\u3002<\/code><\/pre>\n<h3>5.\u65e5\u5fd7\u914d\u7f6e<\/h3>\n<h4>1\uff09\u7b2c\u4e00\u4e2a\u914d\u7f6e<\/h4>\n<pre><code class=\"language-bash\">[root@web01 ~]# cat \/etc\/nginx\/conf.d\/game.conf \nserver {\n    listen 80;\n    server_name www.tuixiangzi.com;\n\n    access_log \/var\/log\/nginx\/www.tuixiangzi.com.log main;  #\u8bbf\u95ee\u65e5\u5fd7\u4ee5main\u683c\u5f0f\u5b58\u653e\u5230\u76ee\u6807\u5730\u5740\n\n    location \/ {\n    root \/code\/tuixiangzi;\n    index index.html;\n    }\n}<\/code><\/pre>\n<h4>2\uff09\u7b2c\u4e8c\u4e2a\u914d\u7f6e<\/h4>\n<pre><code class=\"language-bash\">[root@web01 ~]# cat \/etc\/nginx\/conf.d\/gametwo.conf \nserver {\n    listen 80;\n    server_name www.tank.com;\n\n    access_log \/var\/log\/nginx\/www.tank.com.log main;\n\n    location \/ {\n    root \/code\/tank;\n    index index.html;\n    }\n}<\/code><\/pre>\n<h4>3\uff09\u91cd\u542f\u8bbf\u95ee\u6d4b\u8bd5<\/h4>\n<pre><code class=\"language-bash\">[root@web01 ~]# nginx -t\nnginx: the configuration file \/etc\/nginx\/nginx.conf syntax is ok\nnginx: configuration file \/etc\/nginx\/nginx.conf test is successful\n[root@web01 ~]# systemctl restart nginx\n\n[root@web01 ~]# ll \/var\/log\/nginx\/\ntotal 136\n-rw-r-----. 1 nginx adm  103660 Nov 27 09:31 access.log\n-rw-r-----. 1 nginx adm   21972 Nov 27 09:36 error.log\n-rw-r--r--. 1 root  root    666 Nov 27 09:36 www.tank.com.log\n-rw-r--r--. 1 root  root    190 Nov 27 09:36 www.tuixiangzi.com.log<\/code><\/pre>\n<p>\u603b\u7ed3\uff1anginx\u8fd0\u884c\u4f18\u5148\u9075\u5faaserver\u5185\u914d\u7f6e\uff0c\u518d\u9075\u5faahttp..\u3002\u6240\u4ee5\u65e5\u5fd7\u53ef\u4ee5\u5206\u7c7b\u50a8\u5b58<\/p>\n<h2>\u4e09\u3001nginx\u65e5\u5fd7<\/h2>\n<pre><code class=\"language-bash\">Nginx\u6709\u975e\u5e38\u7075\u6d3b\u7684\u65e5\u5fd7\u8bb0\u5f55\u6a21\u5f0f\uff0c\u6bcf\u4e2a\u7ea7\u522b\u7684\u914d\u7f6e\u53ef\u4ee5\u6709\u5404\u81ea\u72ec\u7acb\u7684\u8bbf\u95ee\u65e5\u5fd7\u3002\u65e5\u5fd7\u683c\u5f0f\u901a\u8fc7log_format\u547d\u4ee4\u5b9a\u4e49\u683c\u5f0f<\/code><\/pre>\n<h3>1.log_format\u8bed\u6cd5<\/h3>\n<pre><code class=\"language-bash\">Syntax: log_format name [escape=default|json|none] string ...;\nDefault:    log_format combined &quot;...&quot;;\nContext:    http<\/code><\/pre>\n<h3>2.\u9ed8\u8ba4\u65e5\u5fd7\u683c\u5f0f<\/h3>\n<pre><code class=\"language-bash\">    log_format  main  &#039;$remote_addr - $remote_user [$time_local] &quot;$request&quot; &#039;\n                      &#039;$status $body_bytes_sent &quot;$http_referer&quot; &#039;\n                      &#039;&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot;&#039;;\n\n10.0.0.1 - - [27\/Nov\/2020:09:36:08 +0800] &quot;GET \/images\/tank.ico HTTP\/1.1&quot; 200 25214 &quot;http:\/\/www.tank.com\/&quot; &quot;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/84.0.4147.89 Safari\/537.36&quot; &quot;-&quot;\n\n10.0.0.1 - - [2020-11-27T10:13:58+08:00] &quot;GET \/images\/modewin\/help0.png HTTP\/1.1&quot; 200 27947 &quot;http:\/\/www.tank.com\/css\/tank.css&quot; &quot;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/84.0.4147.89 Safari\/537.36&quot; &quot;-&quot;<\/code><\/pre>\n<h3>3.\u65e5\u5fd7\u5e38\u7528\u53d8\u91cf<\/h3>\n<pre><code class=\"language-bash\">$remote_addr        # \u8bb0\u5f55\u5ba2\u6237\u7aefIP\u5730\u5740\n$remote_user        # \u8bb0\u5f55\u5ba2\u6237\u7aef\u7528\u6237\u540d\n$time_local         # \u8bb0\u5f55\u901a\u7528\u7684\u672c\u5730\u65f6\u95f4\n$time_iso8601       # \u8bb0\u5f55ISO8601\u6807\u51c6\u683c\u5f0f\u4e0b\u7684\u672c\u5730\u65f6\u95f4\n$request            # \u8bb0\u5f55\u8bf7\u6c42\u7684\u65b9\u6cd5\u4ee5\u53ca\u8bf7\u6c42\u7684http\u534f\u8bae\n$status             # \u8bb0\u5f55\u8bf7\u6c42\u72b6\u6001\u7801(\u7528\u4e8e\u5b9a\u4f4d\u9519\u8bef\u4fe1\u606f)\n$body_bytes_sent    # \u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\u7684\u8d44\u6e90\u5b57\u8282\u6570\uff0c\u4e0d\u5305\u62ec\u54cd\u5e94\u5934\u7684\u5927\u5c0f\n$bytes_sent         # \u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\u7684\u603b\u5b57\u8282\u6570\n$msec               # \u65e5\u5fd7\u5199\u5165\u65f6\u95f4\u3002\u5355\u4f4d\u4e3a\u79d2\uff0c\u7cbe\u5ea6\u662f\u6beb\u79d2\u3002\n$http_referer       # \u8bb0\u5f55\u4ece\u54ea\u4e2a\u9875\u9762\u94fe\u63a5\u8bbf\u95ee\u8fc7\u6765\u7684\n$http_user_agent    # \u8bb0\u5f55\u5ba2\u6237\u7aef\u6d4f\u89c8\u5668\u76f8\u5173\u4fe1\u606f\n$http_x_forwarded_for #\u8bb0\u5f55\u7ecf\u8fc7\u7684\u6240\u6709\u670d\u52a1\u5668\u7684IP\u5730\u5740\n$X-Real-IP         #\u8bb0\u5f55\u8d77\u59cb\u7684\u5ba2\u6237\u7aefIP\u5730\u5740\u548c\u4e0a\u4e00\u5c42\u5ba2\u6237\u7aef\u7684IP\u5730\u5740\n$request_length     # \u8bf7\u6c42\u7684\u957f\u5ea6\uff08\u5305\u62ec\u8bf7\u6c42\u884c\uff0c \u8bf7\u6c42\u5934\u548c\u8bf7\u6c42\u6b63\u6587\uff09\u3002\n$request_time       # \u8bf7\u6c42\u82b1\u8d39\u7684\u65f6\u95f4\uff0c\u5355\u4f4d\u4e3a\u79d2\uff0c\u7cbe\u5ea6\u6beb\u79d2\n# \u6ce8:\u5982\u679cNginx\u4f4d\u4e8e\u8d1f\u8f7d\u5747\u8861\u5668\uff0cnginx\u53cd\u5411\u4ee3\u7406\u4e4b\u540e\uff0c web\u670d\u52a1\u5668\u65e0\u6cd5\u76f4\u63a5\u83b7\u53d6\u5230\u5ba2 \u6237\u7aef\u771f\u5b9e\u7684IP\u5730\u5740\u3002\n# $remote_addr\u83b7\u53d6\u7684\u662f\u53cd\u5411\u4ee3\u7406\u7684IP\u5730\u5740\u3002 \u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\u5728\u8f6c\u53d1\u8bf7\u6c42\u7684http\u5934\u4fe1\u606f\u4e2d\uff0c\n# \u589e\u52a0X-Forwarded-For\u4fe1\u606f\uff0c\u7528\u6765\u8bb0\u5f55\u5ba2\u6237\u7aefIP\u5730\u5740\u548c\u5ba2\u6237\u7aef\u8bf7\u6c42\u7684\u670d\u52a1\u5668\u5730\u5740\u3002<\/code><\/pre>\n<h3>4.nginx\u65e5\u5fd7\u5207\u5272<\/h3>\n<pre><code class=\"language-bash\">[root@web01 ~]# vim \/etc\/logrotate.d\/nginx \n#\u6307\u5b9a\u8981\u5207\u5272\u7684\u65e5\u5fd7\n\/var\/log\/nginx\/*.log {\n\n        daily   #\u6bcf\u5929\u5207\u5272\u65e5\u5fd7\n        missingok   #\u5ffd\u7565\u65e5\u5fd7\u4e22\u5931\n        rotate 52   #\u65e5\u5fd7\u4fdd\u7559\u65f6\u95f4 52\u5929\n        compress   #\u65e5\u5fd7\u538b\u7f29\n        delaycompress  #\u5ef6\u65f6\u538b\u7f29\n        not if empty    #\u4e0d\u5207\u5272\u7a7a\u65e5\u5fd7\n        create 640 nginx adm    #\u5207\u5272\u597d\u7684\u65e5\u5fd7\u6743\u9650\n        sharedscripts   #\u5f00\u59cb\u6267\u884c\u811a\u672c\n        postrotate  #\u6807\u6ce8\u811a\u672c\u5185\u5bb9\n                if [ -f \/var\/run\/nginx.pid ]; then#\u5224\u65adnginx\u542f\u52a8\n                        kill -USR1 `cat \/var\/run\/nginx.pid`   #\u91cd\u65b0\u751f\u6210\u4e00\u4e2aaccess.log\n\n                fi\n        endscript   #\u811a\u672c\u6267\u884c\u5b8c\u6bd5\n}<\/code><\/pre>\n<h2>\u56db\u3001nginx\u5e38\u7528\u6a21\u5757<\/h2>\n<h3>1.\u76ee\u5f55\u7d22\u5f15\u6a21\u5757<\/h3>\n<pre><code class=\"language-bash\"># ngx_http_autoindex_module\n\nngx_http_autoindex_module\u6a21\u5757\u5904\u7406\u4ee5\u659c\u6760\u5b57\u7b26\uff08&#039;\/&#039;\uff09\u7ed3\u5c3e\u7684\u8bf7\u6c42\uff0c\u5e76\u751f\u6210\u76ee\u5f55\u5217\u8868\u3002\n\n\u5f53ngx_http_index_module\u6a21\u5757\u627e\u4e0d\u5230\u7d22\u5f15\u6587\u4ef6\u65f6\uff0c\u901a\u5e38\u4f1a\u5c06\u8bf7\u6c42\u4f20\u9012\u7ed9ngx_http_autoindex_module\u6a21\u5757\u3002<\/code><\/pre>\n<h4>1\uff09\u8bed\u6cd5<\/h4>\n<pre><code class=\"language-bash\">Syntax: autoindex on | off;\nDefault:    autoindex off;\nContext:    http, server, location<\/code><\/pre>\n<h4>2\uff09\u914d\u7f6e<\/h4>\n<pre><code class=\"language-bash\">[root@web01 ~]# vim \/etc\/nginx\/conf.d\/www.autoindex.com.conf \nserver {\n    listen 80;\n    server_name www.autoindex.com;\n    charset utf8;\n\n    location \/ {\n        root \/code\/autoindex;\n        autoindex on;\n    }\n}<\/code><\/pre>\n<h4>3\uff09\u8bbf\u95ee\u7f51\u7ad9\u6b63\u5e38\uff0c\u52a0download\u8df3\u8f6c\u76ee\u5f55\u9875\u9762<\/h4>\n<pre><code class=\"language-bash\">[root@web01 ~]# vim \/etc\/nginx\/conf.d\/www.autoindex.com.conf \nserver {\n    listen 80;\n    server_name www.autoindex.com;\n    charset utf8;\n\n    location \/ {\n        root \/code\/autoindex;\n        index index.html;\n    }\n\n    location \/download {\n        root \/code\/autoindex;\n        autoindex on;\n    }\n}\n\n#\u521b\u5efa\u7ad9\u70b9\u76ee\u5f55\n[root@web01 ~]# mkdir \/code\/autoindex\/download -p\n[root@web01 ~]# echo &quot;\u6d4b\u8bd5autoindex\u6a21\u5757&quot; &gt; \/code\/autoindex\/index.html\n\n#\u8bbf\u95ee\nhttp:\/\/www.autoindex.com\/       \u4e3a\u4e3b\u7ad9\nhttp:\/\/www.autoindex.com\/download\/      \u4e3a\u4e0b\u8f7d\u6587\u4ef6\u7684\u76ee\u5f55<\/code><\/pre>\n<h4>4\uff09\u5e38\u7528\u4f18\u5316\u53c2\u6570<\/h4>\n<pre><code class=\"language-bash\">#\u663e\u793a\u6587\u4ef6\u5b57\u8282\u5927\u5c0f\uff0c\u9ed8\u8ba4\u662f\u663e\u793a\u5b57\u8282\u5927\u5c0f\uff0c\u914d\u7f6e\u4e3aoff\u4e4b\u540e\uff0c\u663e\u793a\u5177\u4f53\u5927\u5c0f M\/G\/K\nSyntax: autoindex_exact_size on | off;\nDefault:    autoindex_exact_size on;\nContext:    http, server, location\n\n#\u663e\u793a\u6587\u4ef6\u7684\u4fee\u6539\u7684\u5177\u4f53\u65f6\u95f4\uff0c\u9ed8\u8ba4\u663e\u793a\u7684\u65f6\u95f4\u4e0e\u771f\u5b9e\u65f6\u95f4\u76f8\u5dee8\u5c0f\u65f6\uff0c\u6240\u4ee5\u914d\u7f6e on\nSyntax: autoindex_localtime on | off;\nDefault:    autoindex_localtime off;\nContext:    http, server, location<\/code><\/pre>\n<h4>5\uff09\u5b8c\u6574\u914d\u7f6e<\/h4>\n<pre><code class=\"language-bash\">[root@web01 ~]# cat \/etc\/nginx\/conf.d\/www.autoindex.com.conf \nserver {\n    listen 80;\n    server_name www.autoindex.com;\n    charset utf8;\n\n    location \/ {\n    root \/code\/autoindex;\n    index index.html;\n    }\n\n    location \/download {\n    root \/code\/autoindex;\n    autoindex on;\n    autoindex_exact_size off;\n    autoindex_localtime on;\n    }\n}<\/code><\/pre>\n<h3>2.Nginx\u8bbf\u95ee\u63a7\u5236\u6a21\u5757<\/h3>\n<pre><code class=\"language-bash\">#ngx_http_access_module<\/code><\/pre>\n<h4>1\uff09\u8bed\u6cd5<\/h4>\n<pre><code class=\"language-bash\">#\u5141\u8bb8\u8bbf\u95ee\u7684\u8bed\u6cd5\nSyntax: allow address | all;\nDefault:    \u2014\nContext:    http, server, location, limit_except\n\n#\u62d2\u7edd\u8bbf\u95ee\u7684\u8bed\u6cd5\nSyntax: deny address | all;\nDefault:    \u2014\nContext:    http, server, location, limit_except\n\n#\u5982\u679c\u914d\u7f6e\u5141\u8bb8\uff0c\u5219\u4e5f\u8981\u914d\u7f6e\u62d2\u7edd\uff1b\u914d\u7f6e\u62d2\u7edd\u53ef\u4ee5\u5355\u72ec\u914d\u7f6e<\/code><\/pre>\n<h4>2\uff09\u914d\u7f6e\u8bbf\u95ee\u63a7\u5236\u793a\u4f8b<\/h4>\n<h5>1&gt;\u62d2\u7edd\u6307\u5b9a\u7684IP,\u5176\u4ed6\u5168\u90e8\u5141\u8bb8<\/h5>\n<pre><code class=\"language-bash\">[root@web01 ~]# vim \/etc\/nginx\/conf.d\/www.autoindex.com.conf \nserver {\n    listen 80;\n    server_name www.autoindex.com;\n    charset utf8;\n\n    location \/ {\n        root \/code\/autoindex;\n        index index.html;\n    }\n\n    location \/download {\n        root \/code\/autoindex;\n        autoindex on;\n        autoindex_exact_size off;\n        autoindex_localtime on;\n        deny 10.0.0.1;\n        allow all;\n    }\n}<\/code><\/pre>\n<h5>2&gt;\u53ea\u5141\u8bb8\u6307\u5b9aIP\u80fd\u8bbf\u95ee, \u5176\u5b83\u5168\u90e8\u62d2\u7edd<\/h5>\n<pre><code class=\"language-bash\">[root@web01 ~]# vim \/etc\/nginx\/conf.d\/www.autoindex.com.conf \nserver {\n    listen 80;\n    server_name www.autoindex.com;\n    charset utf8;\n\n    location \/ {\n        root \/code\/autoindex;\n        index index.html;\n    }\n\n    location \/download {\n        root \/code\/autoindex;\n        autoindex on;\n        autoindex_exact_size off;\n        autoindex_localtime on;\n        allow 10.0.0.1;\n        #\u5982\u679c\u4f7f\u7528all\uff0c\u4e00\u5b9a\u653e\u5728\u6700\u540e\u9762\n        deny all;\n    }\n}<\/code><\/pre>\n<h5>3&gt;\u53ea\u5141\u8bb810.0.0.1\u8bbf\u95ee\uff0c\u62d2\u7edd\u8be5\u7f51\u6bb5\u5176\u4ed6IP<\/h5>\n<pre><code class=\"language-bash\">[root@web01 ~]# vim \/etc\/nginx\/conf.d\/www.autoindex.com.conf \nserver {\n    listen 80;\n    server_name www.autoindex.com;\n    charset utf8;\n\n    location \/ {\n        root \/code\/autoindex;\n        index index.html;\n    }\n\n    location \/download {\n        root \/code\/autoindex;\n        autoindex on;\n        autoindex_exact_size off;\n        autoindex_localtime on;\n        allow 10.0.0.1;\n        #\u5982\u679c\u4f7f\u7528all\uff0c\u4e00\u5b9a\u653e\u5728\u6700\u540e\u9762\n        deny 10.0.0.0\/24;\n    }\n}<\/code><\/pre>\n<h3>3.Nginx\u8bbf\u95ee\u8ba4\u8bc1\u6a21\u5757<\/h3>\n<pre><code class=\"language-bash\"># ngx_http_auth_basic_module<\/code><\/pre>\n<h4>1\uff09\u8bed\u6cd5<\/h4>\n<pre><code class=\"language-bash\">#\u5f00\u542f\u7684\u767b\u5f55\u8ba4\u8bc1\uff0c\u6ca1\u6709\u5375\u7528\nSyntax: auth_basic string | off;\nDefault:    auth_basic off;\nContext:    http, server, location, limit_except\n\n#\u6307\u5b9a\u767b\u5f55\u7528\u7684\u7528\u6237\u540d\u5bc6\u7801\u6587\u4ef6\nSyntax: auth_basic_user_file file;\nDefault:    \u2014\nContext:    http, server, location, limit_except<\/code><\/pre>\n<h4>2\uff09\u521b\u5efa\u5bc6\u7801\u6587\u4ef6<\/h4>\n<pre><code class=\"language-bash\">#\u521b\u5efa\u5bc6\u7801\u6587\u4ef6\u9700\u8981\u7528\u5230 htpasswd\n[root@web01 ~]# htpasswd -c \/etc\/nginx\/auth_basic lhd\nNew password: \nRe-type new password: \nAdding password for user lhd\n\n#\u6dfb\u52a0\u4e00\u4e2a\u767b\u5f55\u7528\u6237\n[root@web01 ~]# htpasswd \/etc\/nginx\/auth_basic egon\nNew password: \nRe-type new password: \nAdding password for user egon\n\n#\u5bc6\u7801\u6587\u4ef6\u5185\u5bb9\n[root@web01 ~]# cat \/etc\/nginx\/auth_basic\nlhd:$apr1$A7d4BWYe$HzlIA7pjdMHBDJPuLBkvd\/\negon:$apr1$psp0M3A5$601t7Am1BG3uINvuBVbFV0<\/code><\/pre>\n<h4>3\uff09\u914d\u7f6e\u8bbf\u95ee\u767b\u5f55<\/h4>\n<pre><code class=\"language-bash\">[root@web01 ~]# vim \/etc\/nginx\/conf.d\/www.autoindex.com.conf \nserver {\n    listen 80;\n    server_name www.autoindex.com;\n    charset utf8;\n    access_log \/var\/log\/nginx\/www.autoindex.com.log main;\n\n    location \/ {\n        root \/code\/autoindex;\n        index index.html;\n    }\n\n    location \/download {\n        root \/code\/autoindex;\n        autoindex on;\n        autoindex_exact_size off;\n        autoindex_localtime on;\n        auth_basic &quot;\u6027\u611f\u8377\u5b98\u5728\u7ebf\u53d1\u724c\uff01\uff01\uff01&quot;;\n        auth_basic_user_file \/etc\/nginx\/auth_basic;\n    }\n}<\/code><\/pre>\n<h3>4.Nginx\u72b6\u6001\u76d1\u63a7\u6a21\u5757<\/h3>\n<pre><code class=\"language-bash\"># ngx_http_stub_status_module\n\nngx_http_stub_status_module\u6a21\u5757\u63d0\u4f9b\u5bf9nginx\u57fa\u672c\u72b6\u6001\u4fe1\u606f\u7684\u8bbf\u95ee\u3002\n\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4e0d\u6784\u5efa\u6b64\u6a21\u5757\uff0c\u5e94\u4f7f\u7528--with-http_stub_status_module\u914d\u7f6e\u53c2\u6570\u542f\u7528\u5b83<\/code><\/pre>\n<h4>1\uff09\u8bed\u6cd5<\/h4>\n<pre><code class=\"language-bash\">Syntax: stub_status;\nDefault:    \u2014\nContext:    server, location<\/code><\/pre>\n<h4>2\uff09\u914d\u7f6e<\/h4>\n<pre><code class=\"language-bash\">[root@web01 ~]# vim \/etc\/nginx\/conf.d\/www.autoindex.com.conf \nserver {\n    listen 80;\n    server_name www.autoindex.com;\n    charset utf8;\n    access_log \/var\/log\/nginx\/www.autoindex.com.log main;\n\n    location \/ {\n        root \/code\/autoindex;\n        index index.html;\n    }\n\n    location \/download {\n        root \/code\/autoindex;\n        autoindex on;\n        autoindex_exact_size off;\n        autoindex_localtime on;\n        auth_basic &quot;\u6027\u611f\u8377\u5b98\u5728\u7ebf\u53d1\u724c\uff01\uff01\uff01&quot;;\n        auth_basic_user_file \/etc\/nginx\/auth_basic;\n    }\n\n    location = \/basic_status {\n        stub_status;\n    }\n}<\/code><\/pre>\n<h4>3\uff09\u8bbf\u95ee<\/h4>\n<pre><code class=\"language-bash\">#\u8bbf\u95ee http:\/\/www.autoindex.com\/basic_status\n\n#nginx\u4e03\u79cd\u72b6\u6001\nActive connections: 2 \nserver accepts handled requests\n        2       2       2 \nReading: 0 Writing: 1 Waiting: 1\n\nActive connections      #\u6d3b\u8dc3\u7684\u8fde\u63a5\u6570\naccepts                 #TCP\u8fde\u63a5\u603b\u6570\nhandled                 #\u6210\u529f\u7684TCP\u8fde\u63a5\u6570\nrequests                #\u6210\u529f\u7684\u8bf7\u6c42\u6570\nReading                 #\u8bfb\u53d6\u7684\u8bf7\u6c42\u5934\nWriting                 #\u54cd\u5e94\nWaiting                 #\u7b49\u5f85\u7684\u8bf7\u6c42\u6570\uff0c\u5f00\u542f\u4e86keepalive\n\n# \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\nkeepalive_timeout  0;   # \u7c7b\u4f3c\u4e8e\u5173\u95ed\u957f\u8fde\u63a5\nkeepalive_timeout  65;  # 65s\u6ca1\u6709\u6d3b\u52a8\u5219\u65ad\u5f00\u8fde\u63a5<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001nginx\u56de\u987e 1.\u5b89\u88c5 1.epol\u6e90\u5b89\u88c5 2.\u5b98\u65b9\u6e90\u5b89\u88c5 3.\u6e90\u7801\u5305\u5b89\u88c5 1\uff09\u4e0b\u8f7d 2\uff09\u89e3\u538b 3\uff09\u751f\u6210 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":319,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[21,19,58,22,57,20],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/321"}],"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=321"}],"version-history":[{"count":1,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/321\/revisions"}],"predecessor-version":[{"id":8761,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/321\/revisions\/8761"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/media\/319"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}