{"id":97,"date":"2021-08-29T20:33:22","date_gmt":"2021-08-29T12:33:22","guid":{"rendered":"https:\/\/www.osvlabs.com\/blog\/?p=97"},"modified":"2021-11-01T18:11:00","modified_gmt":"2021-11-01T10:11:00","slug":"nginx-useful-config","status":"publish","type":"post","link":"https:\/\/blog.osvlabs.com\/?p=97","title":{"rendered":"Nginx\u5e38\u7528\u547d\u4ee4\u914d\u7f6e"},"content":{"rendered":"<h2>\u5e38\u7528\u547d\u4ee4<\/h2>\n<pre><code># \u5b89\u88c5, ubuntu\u7cfb\u7edf\u4e3a\u4f8b\uff0ccentos\u662f\u5176\u4ed6\u547d\u4ee4\nsudo apt-get install nginx\n\n# \u91cd\u542f\u524d\u5148\u68c0\u67e5\u914d\u7f6e\u6587\u4ef6\u8bed\u6cd5\nnginx -t\n\n# \u5982\u679c\u6ca1\u6709\u62a5\u9519\uff0c\u6267\u884c\u91cd\u542f\nnginx -s reload<\/code><\/pre>\n<h2>\u5e38\u7528\u914d\u7f6e<\/h2>\n<h4>\u521b\u5efa\u65b0\u7ad9\u70b9<\/h4>\n<p>\u5b89\u88c5\u5b8cnginx\uff0c\u4f1a\u6709<code>sites-available<\/code>\u548c<code>sites-enabled<\/code>\u76ee\u5f55\uff0c\u53ea\u6709\u5728<code>sites-enabled<\/code>\u76ee\u5f55\u4e0b\u521b\u5efa\u7684\u7ad9\u70b9\u914d\u7f6e\u6587\u4ef6\u624d\u4f1a\u751f\u6548\uff0c\u4f46\u662f\u6211\u4eec\u4e00\u822c\u5728<code>sites-available<\/code>\u76ee\u5f55\u4e0b\u7ad9\u70b9\u914d\u7f6e\u6587\u4ef6\uff0c\u7136\u540e\u8f6f\u94fe\u63a5\u5230<code>sites-enabled<\/code>\uff0c\u8fd9\u6837\u6709\u4e2a\u597d\u5904\u662f\u5047\u8bbe\u4e0b\u9762\u7684 www.booking.com \u7ad9\u70b9\u4e0d\u9700\u8981\u4e86\uff0c\u53ea\u9700\u8981\u5220\u6389\u8fd9\u4e2a\u94fe\u63a5\u6587\u4ef6\u5373\u53ef\uff0c\u6e90\u6587\u4ef6\u8fd8\u662f\u5728sites-available\u76ee\u5f55\u4e0b\u9762\uff0c\u65b9\u4fbf\u8fd8\u539f\u3002<\/p>\n<pre><code>sudo touch \/etc\/nginx\/sites-available\/www.booking.com\nsudo ln -s \/etc\/nginx\/sites-available\/www.booking.com \/etc\/nginx\/sites-enabled\/<\/code><\/pre>\n<h4>\u9759\u6001\u7ad9\u70b9<\/h4>\n<pre><code>server {\n    listen 80;\n    # \u9879\u76ee\u6839\u76ee\u5f55\n    root \/var\/www\/www.test.com\/public\n    # \u9879\u76ee\u5165\u53e3\u6587\u4ef6\uff0c\u6309\u4f18\u5148\u7ea7\u4f9d\u6b21\u5339\u914d\uff0c\u5148\u627eindex.html\uff0c\u5982\u679c\u627e\u4e0d\u5230\u5c31\u627e     index.htm...\n    index index.html index.htm index.nginx-debian.html;\n    # \u57df\u540d\uff0c\u9700\u8981\u5148\u63d0\u524d\u914d\u7f6e\u89e3\u6790\n    server_name test.com www.test.com;\n    # \u5982\u679c\u5339\u914d\u5730\u5740\u5931\u8d25\u8fd4\u56de404\u72b6\u6001\u7801\n    location \/ {\n        try_files $uri $uri\/ =404;\n    }\n}<\/code><\/pre>\n<h4>\u914d\u7f6ehttps<\/h4>\n\n<p>\u914d\u7f6ehttps\u9700\u8981\u5148\u7533\u8bf7SSL\u8bc1\u4e66\uff0c\u73b0\u5728\u963f\u91cc\u4e91\u548c\u817e\u8baf\u4e91\u90fd\u53ef\u4ee5\u7533\u8bf7\u514d\u8d391\u5e74\u7684\u8bc1\u4e66\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u8fd9\u4e9b\u516c\u6709\u4e91\u4e0a\u7533\u8bf7\u8bc1\u4e66\uff0c\u5b89\u88c5\u5728\u81ea\u5df1\u7684\u670d\u52a1\u5668\u4e0a\u9762\uff0c<a href=\"https:\/\/cloud.tencent.com\/document\/product\/400\/35244\">Nginx \u670d\u52a1\u5668 SSL \u8bc1\u4e66\u5b89\u88c5\u90e8\u7f72<\/a>\uff0c\u6587\u6863\u6bd4\u8f83\u8be6\u7ec6\uff0c\u8fd9\u91cc\u4e0d\u5728\u590d\u8ff0\u3002<\/p>\n\n\n<p>\u9700\u8981\u6ce8\u610f\u51e0\u70b9\uff1a<\/p>\n\n\n<ol><li>\u8bc1\u4e66\u548c\u57df\u540d\u76f8\u5173\uff0c\u540c\u4e00\u4e2a\u57df\u540d\u53ef\u4ee5\u7533\u8bf7\u591a\u4e2a\u8bc1\u4e66<\/li><li>\u4f7f\u7528\u8bc1\u4e66\u9700\u8981\u9a8c\u8bc1\u5bf9\u5e94\u7684\u57df\u540d\u7684\u6240\u6709\u6743\uff0c\u6bd4\u5982\u4f1a\u8ba9\u4f60\u5728\u57df\u540d\u5bf9\u5e94\u7684\u6839\u76ee\u5f55\u6dfb\u52a0\u4e00\u4e2a\u6587\u4ef6\uff0c\u53ef\u4ee5\u6b63\u786e\u8bbf\u95ee\u5230\uff0c\u6700\u7b80\u5355\u7684\u65b9\u5f0fDNS\u9a8c\u8bc1<\/li><li>\u5728\u817e\u8baf\u4e91\u4e0a\u7533\u8bf7\u7684\u8bc1\u4e66\u6ca1\u6709\u53ea\u80fd\u5e94\u7528\u5728\u817e\u8baf\u4e91\u8d2d\u4e70\u670d\u52a1\u5668\u7684\u9650\u5236\uff0c\u57df\u540d\u4e5f\u4e0d\u4e00\u5b9a\u662f\u5728\u817e\u8baf\u4e91\u8d2d\u4e70\u7684\uff0c\u5f53\u7136\u90fd\u662f\u7528\u7684\u540c\u4e00\u5bb6\u516c\u6709\u4e91\u7684\u670d\u52a1\u9a8c\u8bc1\u8d77\u6765\u6bd4\u8f83\u65b9\u4fbf<\/li><\/ol>\n\n<h4>basic\u8ba4\u8bc1<\/h4>\n\n<p>\u6709\u4e9b\u7f51\u7ad9\u9875\u9762\u9700\u8981\u8f93\u5165\u6b63\u786e\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u624d\u80fd\u6253\u5f00<\/p>\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/p1-juejin.byteimg.com\/tos-cn-i-k3u1fbpfcp\/a14be72019cf437bab3466456da0827a~tplv-k3u1fbpfcp-watermark.image\" alt=\"image.png\"\/><\/figure>\n\n<p>\u5b9e\u73b0\u65b9\u6cd5\u4e5f\u6bd4\u8f83\u7b80\u5355<\/p>\n<pre><code>sudo apt-get install apache2-utils\n\ncd \/etc\/nginx\n# \u4f7f\u7528htpasswd\u547d\u4ee4\u521b\u5efa\u7528\u6237demo,\u5bc6\u7801123456\u6587\u4ef6\u540dhtpasswd\u7684\u9a8c\u8bc1\u6587\u4ef6\nsudo htpasswd -bc htpasswd demo 123456\n\n# \u7f16\u8f91 nginx \u7ad9\u70b9\u914d\u7f6e\u6587\u4ef6\n\n# \u52a0\u5165\u4e0b\u9762\u4e24\u884c\u5230 server \u6216 location \u6bb5\u4e2d\nauth_basic &#039;Restricted&#039;;  # \u8ba4\u8bc1\u540d\u79f0\uff0c\u968f\u610f\u586b\u5199 \nauth_basic_user_file \/usr\/local\/nginx\/htpasswd; # \u8ba4\u8bc1\u7684\u5bc6\u7801\u6587\u4ef6\uff0c\u9700\u8981\u751f\u6210\u3002\n\n# \u91cd\u542f nginx\nsudo nginx -t &amp;&amp; sudo nginx -s reload<\/code><\/pre>\n<h4>\u663e\u793a\u76ee\u5f55\u6587\u4ef6\u5217\u8868<\/h4>\n<p>\u573a\u666f\uff1a\u6709\u4e2a\u5b58\u653e\u6bcf\u65e5\u5907\u4efd\u6570\u636e\u5e93\u6216\u65e5\u5fd7\u7684\u76ee\u5f55\uff0c\u5e0c\u671b\u663e\u793a\u5217\u8868\uff0c\u65b9\u4fbf\u4e0b\u8f7d\u6587\u4ef6<\/p>\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/p3-juejin.byteimg.com\/tos-cn-i-k3u1fbpfcp\/d07ab6b8a0fe4a3d92c87bf0766afca3~tplv-k3u1fbpfcp-watermark.image\" alt=\"image.png\"\/><\/figure>\n\n<pre><code>location \/ {\n   root \/data\/www\/file                   \/\/\u6307\u5b9a\u5b9e\u9645\u76ee\u5f55\u7edd\u5bf9\u8def\u5f84\uff1b\n   autoindex on;                         \/\/\u5f00\u542f\u76ee\u5f55\u6d4f\u89c8\u529f\u80fd\uff1b\n   autoindex_exact_size off;             \/\/\u5173\u95ed\u8be6\u7ec6\u6587\u4ef6\u5927\u5c0f\u7edf\u8ba1\uff0c\u8ba9\u6587\u4ef6\u5927\u5c0f\u663e\u793aMB\uff0cGB\u5355\u4f4d\uff0c\u9ed8\u8ba4\u4e3ab\uff1b\n   autoindex_localtime on;              \/\/\u5f00\u542f\u4ee5\u670d\u52a1\u5668\u672c\u5730\u65f6\u533a\u663e\u793a\u6587\u4ef6\u4fee\u6539\u65e5\u671f\uff01\n   charset utf-8,gbk;                   \/\/\u907f\u514d\u4e2d\u6587\u4e71\u7801\n}<\/code><\/pre>\n<p>\u53e6\u5916\uff0c\u5982\u679c\u5e0c\u671b\u8bf7\u6c42\u6587\u4ef6\u662f\u4e0b\u8f7d\u800c\u4e0d\u662f\u663e\u793a\u5185\u5bb9\uff0c\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u4e0b\u9762\u53c2\u6570\u5b9e\u73b0\uff1a<code>add_header Content-Disposition attachment;<\/code><\/p>\n<h4>\u53cd\u5411\u4ee3\u7406<\/h4>\n<p>\u6211\u4eec\u53ea\u9700\u8981\u8bb0\u5f97\u6b63\u5411\u4ee3\u7406\u4ee3\u7406\u7684\u5bf9\u8c61\u662f\u5ba2\u6237\u7aef\uff0c\u6700\u5e38\u89c1\u7684\u5c31\u662fFQ\u8f6f\u4ef6<\/p>\n<p>\u53cd\u5411\u4ee3\u7406\u4ee3\u7406\u7684\u5bf9\u8c61\u662f\u670d\u52a1\u7aef<\/p>\n<p>\u5ba2\u6237\u7aef\u672c\u6765\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7HTTP\u534f\u8bae\u8bbf\u95ee\u67d0\u7f51\u7ad9\u5e94\u7528\u670d\u52a1\u5668\uff0c\u5982\u679c\u7f51\u7ad9\u7ba1\u7406\u5458\u5728\u4e2d\u95f4\u52a0\u4e0a\u4e00\u4e2aNginx\uff0c\u5ba2\u6237\u7aef\u8bf7\u6c42Nginx\uff0cNginx\u8bf7\u6c42\u5e94\u7528\u670d\u52a1\u5668\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\uff0c\u6b64\u65f6Nginx\u5c31\u662f\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\u3002<\/p>\n<p>\u4f8b\u5b50\uff1aNginx\u76d1\u542c\u6765\u81ea\u5916\u90e8\u8bbf\u95ee80\u7684\u8bf7\u6c42\uff0c\u8f6c\u53d1\u7ed9\u81ea\u5df1\u670d\u52a1\u5668\u5360\u752818083\u7aef\u53e3\u7684\u670d\u52a1<\/p>\n<pre><code>server {\n        listen 80;\n        index index.html index.htm index.nginx-debian.html;\n        server_name mqtt.demo.com;\n        location \/ {\n          proxy_pass  http:\/\/127.0.0.1:18083;\n        }\n}<\/code><\/pre>\n<h4>\u6574\u5408 PHP<\/h4>\n<pre><code>server {\n    listen 80;\n    server_name foo.com;\n\n    root \/path;\n    index index.html index.htm index.php;\n\n    location \/ {\n        try_files $uri $uri\/ \/index.php$is_args$args;\n    }\n\n    location ~ .php$ {\n        try_files $uri =404;\n        include fastcgi.conf;\n        # \u9700\u8981\u786e\u4fdd \/run\/php\/php7.2-fpm.sock \u6587\u4ef6\u5b58\u5728\n        fastcgi_pass unix:\/run\/php\/php7.2-fpm.sock;\n        # fastcgi_pass 127.0.0.1:9000;\n    }\n}<\/code><\/pre>\n<h4>\u5173\u4e8e try_files \u6307\u4ee4<\/h4>\n<pre><code>   location \/ {\n        # First attempt to serve request as file, then\n        # as directory, then fall back to displaying a 404.\n        # $uri \u662f\u53d8\u91cf \u5982 www.xxx.com\/aaa.php \u5219 $uri\u662faaa.php\n        # \u5047\u8bbe\u6211\u4eec\u8bbf\u95ee\/a.php \u5148\u5224\u65ad a.php\u662f\u4e0d\u662f\u6587\u4ef6,\u662f\u8fd4\u56de\n        # \u5982\u679c\u4e0d\u662f\u518d\u5224\u65ad\u662f\u4e0d\u662f\u76ee\u5f55\uff08$uri\/\uff09,\u662f\u8fd4\u56de\n        # \u5982\u679c\u90fd\u4e0d\u662f\u5219\u8fd4\u56de404\n        try_files $uri $uri\/ =404;\n    }\n\n    #  \u6240\u6709\u7684\u8bf7\u6c42\u5f15\u5bfc\u5230index.php\u4e2d\n    try_files $uri $uri\/   \/index.php?$query_string  =404;<\/code><\/pre>\n<h4>@ \u7b26\u53f7\u7684\u4f7f\u7528<\/h4>\n<p>@\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a location \u5757\uff0c\u4e14\u8be5\u5757\u4e0d\u80fd\u88ab\u5916\u90e8 Client \u6240\u8bbf\u95ee\uff0c\u53ea\u80fd\u88ab Nginx \u5185\u90e8\u914d\u7f6e\u6307\u4ee4\u6240\u8bbf\u95ee\uff0c\u6bd4\u5982 try_files<\/p>\n<p><code>c49fb81c8f9141ae3fe7f9db2da60bd7.htm<\/code> \u5b9e\u9645\u4e0a\u4e2a\u4e0d\u5b58\u5728\u7684\u6587\u4ef6\uff0c\n\u4e0b\u9762\u7684\u542b\u4e49\u7684\u5982\u679c\u8bbf\u95ee\u9879\u76ee\u6839\u76ee\u5f55\u6216blog\u76ee\u5f55\u5e95\u4e0b\u7684\u4ee5php\u7ed3\u5c3e\u7684\u6587\u4ef6\u90fd\u8d70php fpm\u89e3\u6790\u6d41\u7a0b<\/p>\n<pre><code>location ~ \\.php$ {\n   try_files \/c49fb81c8f9141ae3fe7f9db2da60bd7.htm @php;\n}\n\nlocation \/blog\/\\.php$ {\n   try_files \/c49fb81c8f9141ae3fe7f9db2da60bd7.htm @php;\n}\n\nlocation @php {\n  try_files $uri =404;\n  include \/etc\/nginx\/fastcgi_params;\n  fastcgi_pass unix:\/var\/lib\/php7.2-fpm\/web10.sock;\n  fastcgi_index index.php;\n  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n  fastcgi_intercept_errors on;\n}<\/code><\/pre>\n<h4>\u9a8c\u8bc1\u5934\u4fe1\u606f\u4e2d\u7684 referer \u53c2\u6570<\/h4>\n<p>\u8bf7\u6c42\u5934\u4fe1\u606f\u4e2d\u7684 referer \u53c2\u6570\uff0c\u8bb0\u5f55\u4e86\u4e0a\u4e00\u4e2a\u9875\u9762\u7684\u5730\u5740\uff0cNginx\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u6821\u9a8c\uff0c\u8fbe\u5230\u9632\u76d7\u94fe\u7684\u76ee\u7684\u3002<\/p>\n<p>\u901a\u5e38\u5728\u914d\u7f6e\u6587\u4ef6\u7684location\u5757\u4e2d\u589e\u52a0\u914d\u7f6e\u3002<\/p>\n<pre><code>server {\n    listen       80;   # \u7aef\u53e3\n    server_name  www.osvlabs.com;  # \u670d\u52a1\u540d\uff0c\u53ef\u4ee5\u662fIP\u5730\u5740\u6216\u8005\u57df\u540d\n\n    location \/ {   # \u6839\u8def\u5f84\n\u3000\u3000\u3000\u3000\u3000root   html;  # \u5bf9\u5e94nginx\u5b89\u88c5\u76ee\u6807\u4e0b\u7684html\u6587\u4ef6\u5939\n        index  hello.html; # \u6307\u5b9a\u9996\u9875\u4e3a hello.html\n    }\n\n    location ~* \\.(GIF|PNG|jpg|bmp|jpeg) {  # *\u4ee3\u8868\u4e0d\u533a\u5206\u5927\u5c0f\u5199\n        # \u6821\u9a8c\u8bf7\u6c42\u662f\u5426\u6765\u81ea\u4e8eosvlabs.com\u8fd9\u4e2a\u7ad9\u70b9\uff0c\u4e0d\u662f\u5219\u8fd4\u56de404\u9875\u9762\n    \u3000\u3000valid_referers *.osvlabs.com;\n    \u3000\u3000if ($invalid_referer) {\n           return 404;\n    \u3000\u3000}\n\u3000\u3000\u3000\u3000 root \/home\/img;\n\u3000\u3000}\n\n    error_page 500 502 503 504  \/50x.html;  # \u6307\u5b9a\u8fd9\u4e9b\u72b6\u6001\u7801\u8df3\u8f6c\u7684\u9519\u8bef\u9875\n    location = \/50x.html {\n        root   html;\n    }\n}<\/code><\/pre>\n<h4>Nginx\u6784\u5efa\u670d\u52a1\u5668\u96c6\u7fa4<\/h4>\n<p>Nginx\u6700\u5e38\u7528\u7684\u4e00\u4e2a\u529f\u80fd\uff0c\u5c31\u662f\u4e3a\u670d\u52a1\u5668\u6784\u5efa\u96c6\u7fa4\uff0c\u4ee5\u8fbe\u5230\u5b9e\u73b0\u9ad8\u5e76\u53d1\u3001\u9ad8\u53ef\u7528\u7684\u76ee\u7684\u3002<\/p>\n<p>\u9996\u5148\u5728 upstream \u5757\u4e2d\uff0c\u914d\u7f6e\u670d\u52a1\u5668\u96c6\u7fa4\u4e2d\u7684IP\u5730\u5740\uff0c\u7136\u540e\u5728location\u5757\u4e2d\u914d\u7f6e\u8f6c\u53d1\u8bf7\u6c42\u5230\u6b64 upstream\u3002<\/p>\n<pre><code># \u914d\u7f6e\u96c6\u7fa4\u4e2d\u7684\u670d\u52a1\u5668\nupstream osvlabs {\n    server 192.168.1.101:8080;\n    server 192.168.1.102:8080;\n    server 192.168.1.103:8080;\n}\n\nserver {\n    listen  80;\n    server_name     www.osvlabs.com;\n\n    location \/ {\n        proxy_pass      http:\/\/osvlabs;\n    }\n}<\/code><\/pre>\n<p>\u9ed8\u8ba4\u6309\u6240\u6709\u673a\u5668\u6743\u91cd\u4e3a1\u7684\u8f6e\u8be2\u65b9\u5f0f\u5bf9\u96c6\u7fa4\u670d\u52a1\u8fdb\u884c\u8bbf\u95ee\uff0c\u6bcf\u4e2a\u670d\u52a1\u8bbf\u95ee1\u6b21\uff0c\u7136\u540e\u8bbf\u95ee\u4e0b\u4e00\u4e2a\u670d\u52a1\uff0c\u9002\u5408\u96c6\u7fa4\u4e2d\u6bcf\u53f0\u670d\u52a1\u5668\u6027\u80fd\u5dee\u4e0d\u591a\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u6743\u91cd\u914d\u7f6e\u4e5f\u662f\u7ecf\u5e38\u7528\u7684\uff0c\u9002\u7528\u4e8e\u673a\u5668\u6027\u80fd\u6709\u5dee\u5f02\u7684\u60c5\u51b5\u3002<\/p>\n<pre><code>upstream osvlabs {\n\u3000\u3000server 192.168.1.101:8080  weight=1;\n\u3000\u3000server 192.168.1.102:8080;\n\u3000\u3000server 192.168.1.103:8080  weight=3;\n}<\/code><\/pre>\n<p>weight \u5c31\u662f\u6743\u91cd\u914d\u7f6e\uff0c\u4e0d\u914d\u9ed8\u8ba4\u662f1\uff0c\u6309\u7167\u4ee5\u4e0a\u914d\u7f6e\uff0c\u57285\u6b21\u8bf7\u6c42\u4e2d\uff0c101\u548c102\u4f1a\u88ab\u8bbf\u95ee1\u6b21\uff0c103\u4f1a\u88ab\u8bbf\u95ee3\u6b21\u3002<\/p>\n<p>\u4f7f\u7528down\uff0c\u53ef\u4ee5\u6807\u8bc6\u67d0\u4e2a\u670d\u52a1\u5df2\u505c\u7528\uff0cNginx\u4fbf\u4e0d\u4f1a\u53bb\u8bbf\u95ee\u4ed6\u4e86\u3002<\/p>\n<pre><code>upstream osvlabs {\n    server 192.168.1.101:8080;\n    server 192.168.1.102:8080 down;\n    server 192.168.1.103:8080;\n}<\/code><\/pre>\n<p>\u4f7f\u7528backup\uff0c\u53ef\u4ee5\u6807\u8bc6101\u662f\u5907\u7528\u673a\uff0c\u5f53102\u3001103\u5b95\u673a\u540e\uff0c101\u4f1a\u8fdb\u884c\u670d\u52a1\u3002<\/p>\n<pre><code>upstream osvlabs {\n    server 192.168.1.101:8080 backup;\n    server 192.168.1.102:8080;\n    server 192.168.1.103:8080;\n}<\/code><\/pre>\n<p>\u4f7f\u7528 max_fails \u548c fail_timeout \u5c06\u670d\u52a1\u52a8\u6001\u505c\u7528\nmax_fails \u9ed8\u8ba4\u662f1\uff0cfail_timeout\u9ed8\u8ba4\u662f10s<\/p>\n<pre><code>upstream osvlabs {\n    server 192.168.1.101:8080 max_fails=2 fail_timeout=10s;\n    server 192.168.1.102:8080;\n    server 192.168.1.103:8080;\n}<\/code><\/pre>\n<p>\u5982\u6b64\u914d\u7f6e\uff0c101\u670d\u52a1\u5668\u572810\u79d2\u5185\u5982\u679c\u5931\u8d25\u6b21\u6570\u8fbe\u52302\u6b21\uff0c\u4f1a\u505c\u752810\u79d2\u300210\u79d2\u540e\uff0c\u4f1a\u5c1d\u8bd5\u8fde\u63a5101\u670d\u52a1\u5668\uff0c\u5982\u679c\u8fde\u63a5\u6210\u529f\u5219\u6062\u590d\u8f6e\u8be2\u65b9\u5f0f\uff0c\u5982\u679c\u4e0d\u6210\u529f\uff0c\u518d\u7b49\u5f8510\u79d2\u5c1d\u8bd5\u3002<\/p>\n<h4>\u4f7f\u7528keepalive\u8bbe\u7f6e\u957f\u94fe\u63a5\u6570\u91cf\uff0c\u63d0\u9ad8\u541e\u5410\u91cf<\/h4>\n<pre><code>upstream osvlabs {\n    server 192.168.1.101:8080;\n    server 192.168.1.102:8080;\n    server 192.168.1.103:8080;\n\n    keepalive 50;\n}\n\n server {\n    listen  80;\n    server_name     www.osvlabs.com;\n\n    location \/ {\n        proxy_pass      http:\/\/osvlabs;\n        proxy_http_version    1.1;\n        proxy_set_header    Connection &quot;&quot;;\n    }\n}<\/code><\/pre>\n<p>\u9700\u8981\u5728upstream\u5757\u4e2d\u589e\u52a0 keepalive \u914d\u7f6e\uff0c\u5728server\u7684location\u5757\u4e2d\u589e\u52a0 proxy_http_version \u548c proxy_set_header \u914d\u7f6e\u3002<\/p>\n<p>\u8fd9\u6837\u8bbe\u7f6e\u53ef\u4ee5\u51cf\u5c11\u8fde\u63a5\u65ad\u5f00\u3001\u65b0\u5efa\u7684\u635f\u8017\uff0c\u589e\u52a0\u541e\u5410\u91cf\u3002<\/p>\n<h4>\u5176\u4ed6\u8d1f\u8f7d\u5747\u8861\u7b56\u7565<\/h4>\n<p>\u9664\u4e86\u524d\u9762\u8bf4\u5230\u7684\u8f6e\u8be2\u65b9\u5f0f\uff0cNginx\u5728\u8d1f\u8f7d\u5747\u8861\u65f6\uff0c\u8fd8\u6709\u5176\u4ed6\u7b56\u7565\u3002<\/p>\n<ul>\n<li>ip_hash\uff1a\u4ee5\u5ba2\u6237\u7aefIP\u5730\u5740\u4e3a\u4f9d\u636e\uff0c\u5339\u914d\u670d\u52a1\u5668\u3002<\/li>\n<li>hash $request_uri\uff1a\u4ee5\u8bf7\u6c42\u7684URL\u4e3a\u4f9d\u636e\uff0c\u5339\u914d\u670d\u52a1\u5668\u3002<\/li>\n<li>least_conn\uff1a\u4ee5\u670d\u52a1\u5668\u8fde\u63a5\u6570\u4e3a\u4f9d\u636e\uff0c\u54ea\u4e2a\u670d\u52a1\u5668\u8fde\u63a5\u6570\u5c11\uff0c\u5339\u914d\u54ea\u53f0\u670d\u52a1\u5668<\/li>\n<\/ul>\n<pre><code>upstream osvlabs {\n    # ip_hash;\n    # hash $request_uri;\n    least_conn;\n\n    server 192.168.1.101:8080;\n    server 192.168.1.102:8080;\n    server 192.168.1.103:8080;\n}<\/code><\/pre>","protected":false},"excerpt":{"rendered":"<p>\u5e38\u7528\u547d\u4ee4 # \u5b89\u88c5, ubuntu\u7cfb\u7edf\u4e3a\u4f8b\uff0ccentos\u662f\u5176\u4ed6\u547d\u4ee4 sudo apt-get install nginx # \u91cd\u542f\u524d\u5148\u68c0\u67e5\u914d\u7f6e\u6587\u4ef6\u8bed\u6cd5 nginx -t # \u5982\u679c\u6ca1\u6709\u62a5\u9519\uff0c\u6267\u884c\u91cd\u542f n\u2026<\/p>\n","protected":false},"author":1,"featured_media":113,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":5}},"_links":{"self":[{"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/posts\/97"}],"collection":[{"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=97"}],"version-history":[{"count":9,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/posts\/97\/revisions"}],"predecessor-version":[{"id":120,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/posts\/97\/revisions\/120"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/media\/113"}],"wp:attachment":[{"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}