{"id":176319,"date":"2020-04-23T08:38:56","date_gmt":"2020-04-23T00:38:56","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=176319"},"modified":"2020-04-13T09:39:43","modified_gmt":"2020-04-13T01:39:43","slug":"nginx-base-dy","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/nginx-base-dy.html","title":{"rendered":"Nginx \u6559\u7a0b\uff1a\u57fa\u672c\u6982\u5ff5\u4ecb\u7ecd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u55e8\uff01\u5206\u4eab\u5c31\u662f\u5173\u5fc3\uff01\u6240\u4ee5\uff0c\u6211\u4eec\u613f\u610f\u518d\u8ddf\u4f60\u5206\u4eab\u4e00\u70b9\u70b9\u77e5\u8bc6\u3002\u6211\u4eec\u51c6\u5907\u4e86\u8fd9\u4e2a\u5212\u5206\u4e3a\u4e09\u8282\u7684\u300aNginx\u6559\u7a0b\u300b\u3002\u5982\u679c\u4f60\u5bf9 Nginx \u5df2\u7ecf\u6709\u6240\u4e86\u89e3\uff0c\u6216\u8005\u4f60\u5e0c\u671b\u4e86\u89e3\u66f4\u591a\uff0c\u8fd9\u4e2a\u6559\u7a0b\u5c06\u4f1a\u5bf9\u4f60\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\n
\n
\"\"<\/a>\u6211\u4eec\u4f1a\u544a\u8bc9\u4f60 Nginx \u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u5176\u80cc\u540e\u7684\u6982\u5ff5\u6709\u54ea\u4e9b\uff0c\u4ee5\u53ca\u5982\u4f55\u4f18\u5316\u5b83\u4ee5\u63d0\u5347\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u8fd8\u4f1a\u544a\u8bc9\u4f60\u5982\u4f55\u5b89\u88c5\uff0c\u5982\u4f55\u542f\u52a8\u3001\u8fd0\u884c\u3002<\/p>\n

\u8fd9\u4e2a\u6559\u7a0b\u5305\u62ec\u4e09\u8282\uff1a<\/p>\n

    \n
  • \u57fa\u7840\u6982\u5ff5<\/strong>\u2014\u2014\u4f60\u53ef\u4ee5\u4e86\u89e3\u547d\u4ee4\uff08directive\uff09\u4e0e\u73af\u5883\uff08context\uff09\u7684\u533a\u522b\u3001\u7ee7\u627f\u6a21\u5f0f\uff0c\u4ee5\u53ca Nginx \u9009\u62e9\u670d\u52a1\u5668\u533a\u5757\u7684\u987a\u5e8f\uff0c\u8fd8\u6709\u5b89\u88c5\u4f4d\u7f6e\u3002<\/li>\n
  • \u6027\u80fd\u7ba1\u7406<\/strong>\u2014\u2014\u63d0\u5347\u901f\u5ea6\u7684\u8bc0\u7a8d\u3002\u6211\u4eec\u5c06\u4f1a\u8bb2\u89e3 gzip\u3001\u7f13\u5b58\u3001\u7f13\u51b2\u533a\u4ee5\u53ca\u8d85\u65f6\u8bbe\u7f6e\u3002<\/li>\n
  • SSL \u8bbe\u7f6e<\/strong>\u2014\u2014\u8bb2\u89e3\u7528 HTTPS \u6765\u63d0\u4f9b\u5185\u5bb9\u7684\u8bbe\u7f6e\u6b65\u9aa4\u3002<\/li>\n<\/ul>\n

    \u6211\u4eec\u7684\u76ee\u6807\u662f\u521b\u5efa\u4e00\u7cfb\u5217\u6559\u7a0b\uff0c\u8ba9\u4f60\u53ef\u4ee5\u8f7b\u677e\u627e\u5230\u7279\u5b9a\u4e3b\u9898\u7684\u6b63\u786e\u914d\u7f6e\uff0c\u5982 gzip\u3001SSL \u7b49\uff0c\u6216\u8005\u76f4\u63a5\u6d4f\u89c8\u4e00\u4e0b\u90a3\u4e9b\u914d\u7f6e\u3002\u4e3a\u4e86\u83b7\u5f97\u6700\u4f73\u7684\u5b66\u4e60\u4f53\u9a8c\uff0c\u6211\u4eec\u5efa\u8bae\u4f60\u5728\u81ea\u5df1\u7684\u673a\u5668\u4e0a\u5b89\u88c5 Nginx\uff0c\u5e76\u4eb2\u624b\u5b9e\u6218\u4e00\u4e0b\u3002<\/p>\n<\/div>\n<\/div>\n<\/div>\n

    \n
    \n
    \n
    \u4ec0\u4e48\u662f Nginx\uff1f<\/strong><\/div>\n

    Nginx \u6700\u521d\u662f\u4f5c\u4e3a\u4e00\u4e2a Web \u670d\u52a1\u5668\u521b\u5efa\u7684\uff0c\u7528\u4e8e\u89e3\u51b3\u00a0C10k\u00a0\u7684\u95ee\u9898\u3002\u4f5c\u4e3a\u4e00\u4e2a Web \u670d\u52a1\u5668\uff0c\u5b83\u53ef\u4ee5\u4ee5\u60ca\u4eba\u7684\u901f\u5ea6\u4e3a\u60a8\u7684\u6570\u636e\u670d\u52a1\u3002\u4f46 Nginx \u4e0d\u4ec5\u4ec5\u662f\u4e00\u4e2a Web \u670d\u52a1\u5668\uff0c\u4f60\u8fd8\u53ef\u4ee5\u5c06\u5176\u7528\u4f5c\u53cd\u5411\u4ee3\u7406\uff0c\u4e0e\u8f83\u6162\u7684\u4e0a\u6e38\u670d\u52a1\u5668\uff08\u5982\uff1aUnicorn\u00a0\u6216\u00a0Puma\uff09\u8f7b\u677e\u96c6\u6210\u3002\u4f60\u53ef\u4ee5\u9002\u5f53\u5730\u5206\u914d\u6d41\u91cf\uff08\u8d1f\u8f7d\u5747\u8861\u5668\uff09\u3001\u6d41\u5a92\u4f53\u3001\u52a8\u6001\u8c03\u6574\u56fe\u50cf\u5927\u5c0f\u3001\u7f13\u5b58\u5185\u5bb9\u7b49\u7b49\u3002\u57fa\u672c\u7684 nginx \u4f53\u7cfb\u7ed3\u6784\u7531 master \u8fdb\u7a0b\u548c\u5176 worker \u8fdb\u7a0b\u7ec4\u6210\u3002master \u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\uff0c\u5e76\u7ef4\u62a4 worker \u8fdb\u7a0b\uff0c\u800c worker \u5219\u4f1a\u5bf9\u8bf7\u6c42\u8fdb\u884c\u5b9e\u9645\u5904\u7406\u3002<\/p><\/div>\n<\/div>\n<\/div>\n

    \n
    \n
    \n
    \u57fa\u672c\u547d\u4ee4<\/strong><\/div>\n

    \u8981\u542f\u52a8 nginx\uff0c\u53ea\u9700\u8f93\u5165\uff1a<\/p>\n

    [sudo]<\/span>\u00a0nginx<\/span><\/pre>\n

    \u5f53\u4f60\u7684 nginx \u5b9e\u4f8b\u8fd0\u884c\u65f6\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u53d1\u9001\u76f8\u5e94\u7684\u4fe1\u53f7\u6765\u7ba1\u7406\u5b83\uff1a<\/p>\n

    [sudo]<\/span>\u00a0nginx<\/span>\u00a0-s<\/span>\u00a0signal<\/span><\/pre>\n

    \u53ef\u7528\u7684\u4fe1\u53f7\uff1a<\/p>\n

      \n
    • stop\u00a0- \u5feb\u901f\u5173\u95ed<\/li>\n
    • quit\u00a0- \u4f18\u96c5\u5173\u95ed (\u7b49\u5f85 worker \u7ebf\u7a0b\u5b8c\u6210\u5904\u7406)<\/li>\n
    • reload\u00a0- \u91cd\u8f7d\u914d\u7f6e\u6587\u4ef6<\/li>\n
    • reopen\u00a0- \u91cd\u65b0\u6253\u5f00\u65e5\u5fd7\u6587\u4ef6<\/li>\n<\/ul>\n<\/div>\n
      \n
      \n
      <\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n
      \n
      \n
      \n
      \u547d\u4ee4\u548c\u4e0a\u4e0b\u6587<\/strong><\/div>\n

      nginx \u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u9ed8\u8ba4\u7684\u4f4d\u7f6e\u5305\u62ec\uff1a<\/p>\n

        \n
      • \/etc\/nginx\/nginx.conf<\/code>,<\/li>\n
      • \/usr\/local\/etc\/nginx\/nginx.conf<\/code>\uff0c\u6216<\/li>\n
      • \/usr\/local\/nginx\/conf\/nginx.conf<\/code><\/li>\n<\/ul>\n

        \u914d\u7f6e\u6587\u4ef6\u7684\u7531\u4e0b\u9762\u7684\u90e8\u5206\u6784\u6210:<\/p>\n

          \n
        • \u6307\u4ee4\u00a0- \u53ef\u9009\u9879\uff0c\u5305\u542b\u540d\u79f0\u548c\u53c2\u6570\uff0c\u4ee5\u5206\u53f7\u7ed3\u5c3e<\/li>\n<\/ul>\n
          gzip<\/span>\u00a0on<\/span>;<\/pre>\n
            \n
          • \u4e0a\u4e0b\u6587\u00a0- \u5206\u5757\uff0c\u4f60\u53ef\u4ee5\u58f0\u660e\u6307\u4ee4 - \u7c7b\u4f3c\u4e8e\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u4f5c\u7528\u57df<\/li>\n<\/ul>\n
            worker_processes<\/span>\u00a02<\/span>;\u00a0#\u00a0\u5168\u5c40\u4e0a\u4e0b\u6587\u6307\u4ee4<\/span>\r\n\r\nhttp<\/span>\u00a0{\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#\u00a0http\u00a0\u4e0a\u4e0b\u6587<\/span>\r\n\u00a0\u00a0\u00a0\u00a0gzip<\/span>\u00a0on<\/span>;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#\u00a0http\u00a0\u4e0a\u4e0b\u6587\u4e2d\u7684\u6307\u4ee4<\/span>\r\n\r\n\u00a0\u00a0server<\/span>\u00a0{\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#\u00a0server\u00a0\u4e0a\u4e0b\u6587<\/span>\r\n\u00a0\u00a0\u00a0\u00a0listen<\/span>\u00a080<\/span>;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#\u00a0server\u00a0\u4e0a\u4e0b\u6587\u4e2d\u7684\u6307\u4ee4<\/span>\r\n\u00a0\u00a0}\r\n}<\/pre>\n<\/div>\n<\/div>\n<\/div>\n
            \n
            \n
            \n
            \u6307\u4ee4\u7c7b\u578b<\/strong><\/div>\n

            \u5728\u591a\u4e2a\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\u76f8\u540c\u7684\u6307\u4ee4\u65f6\uff0c\u5fc5\u987b\u8981\u5c0f\u5fc3\uff0c\u56e0\u4e3a\u7ee7\u627f\u6a21\u578b\u4e0d\u540c\u65f6\u6709\u7740\u4e0d\u540c\u7684\u6307\u4ee4\u3002\u6709\u4e09\u79cd\u7c7b\u578b\u7684\u6307\u4ee4\uff0c\u6bcf\u79cd\u90fd\u6709\u81ea\u5df1\u7684\u7ee7\u627f\u6a21\u578b\u3002<\/p>\n

            \u666e\u901a\u6307\u4ee4 <\/strong><\/span><\/div>\n

            \u5728\u6bcf\u4e2a\u4e0a\u4e0b\u6587\u4ec5\u6709\u552f\u4e00\u503c\u3002\u800c\u4e14\uff0c\u5b83\u53ea\u80fd\u5728\u5f53\u524d\u4e0a\u4e0b\u6587\u4e2d\u5b9a\u4e49\u4e00\u6b21\u3002\u5b50\u7ea7\u4e0a\u4e0b\u6587\u53ef\u4ee5\u8986\u76d6\u7236\u7ea7\u4e2d\u7684\u503c\uff0c\u5e76\u4e14\u8fd9\u4e2a\u8986\u76d6\u503c\u53ea\u5728\u5f53\u524d\u7684\u5b50\u7ea7\u4e0a\u4e0b\u6587\u4e2d\u6709\u6548\u3002<\/p>\n

            gzip<\/span>\u00a0on<\/span>;\r\ngzip<\/span>\u00a0off<\/span>;\u00a0#\u00a0\u975e\u6cd5\uff0c\u4e0d\u80fd\u5728\u540c\u4e00\u4e2a\u4e0a\u4e0b\u6587\u4e2d\u6307\u5b9a\u540c\u4e00\u666e\u901a\u6307\u4ee42\u6b21<\/span>\r\n\r\nserver<\/span>\u00a0{\r\n\u00a0\u00a0location<\/span>\u00a0\/downloads\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0gzip<\/span>\u00a0off<\/span>;\r\n\u00a0\u00a0}\r\n\r\n\u00a0\u00a0location<\/span>\u00a0\/assets\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0#\u00a0gzip\u00a0is\u00a0on\u00a0here<\/span>\r\n\u00a0\u00a0}\r\n}<\/pre>\n
            \u6570\u7ec4\u6307\u4ee4<\/strong><\/span><\/div>\n

            \u5728\u540c\u4e00\u4e0a\u4e0b\u6587\u4e2d\u6dfb\u52a0\u591a\u6761\u6307\u4ee4\uff0c\u5c06\u6dfb\u52a0\u591a\u4e2a\u503c\uff0c\u800c\u4e0d\u662f\u5b8c\u5168\u8986\u76d6\u3002\u5728\u5b50\u7ea7\u4e0a\u4e0b\u6587\u4e2d\u5b9a\u4e49\u6307\u4ee4\u5c06\u8986\u76d6\u7ed9\u7236\u7ea7\u4e0a\u4e0b\u6587\u4e2d\u7684\u503c\u3002<\/p>\n

            error_log\u00a0\/var\/log<\/span>\/nginx\/error<\/span>.log<\/span>;\r\nerror_log\u00a0\/var\/log<\/span>\/nginx\/error_notive.log<\/span>\u00a0notice;\r\nerror_log\u00a0\/var\/log<\/span>\/nginx\/error_debug.log<\/span>\u00a0debug<\/span>;\r\n\r\nserver\u00a0{\r\n\u00a0\u00a0location\u00a0\/downloads\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0#\u00a0\u4e0b\u9762\u7684\u914d\u7f6e\u4f1a\u8986\u76d6\u7236\u7ea7\u4e0a\u4e0b\u6587\u4e2d\u7684\u6307\u4ee4\r\n\u00a0\u00a0\u00a0\u00a0error_log\u00a0\/var\/log<\/span>\/nginx\/error_downloads.log<\/span>;\r\n\u00a0\u00a0}\r\n}<\/pre>\n<\/div>\n<\/div>\n<\/div>\n
            \n
            \n
            \n
            \u884c\u52a8\u6307\u4ee4<\/strong><\/span><\/div>\n

            \u884c\u52a8\u662f\u6539\u53d8\u4e8b\u60c5\u7684\u6307\u4ee4\u3002\u6839\u636e\u6a21\u5757\u7684\u9700\u8981\uff0c\u5b83\u7ee7\u627f\u7684\u884c\u4e3a\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u4f8b\u5982 rewrite \u6307\u4ee4\uff0c\u53ea\u8981\u662f\u5339\u914d\u7684\u90fd\u4f1a\u6267\u884c\uff1a<\/p>\n

            server<\/span>\u00a0{\r\n\u00a0\u00a0rewrite<\/span>\u00a0^<\/span>\u00a0\/foobar;\r\n\r\n\u00a0\u00a0location<\/span>\u00a0\/foobar\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0rewrite<\/span>\u00a0^<\/span>\u00a0\/foo;\r\n\u00a0\u00a0\u00a0\u00a0rewrite<\/span>\u00a0^<\/span>\u00a0\/bar;\r\n\u00a0\u00a0}\r\n}<\/pre>\n

            \u5982\u679c\u7528\u6237\u60f3\u5c1d\u8bd5\u83b7\u53d6 \/sample\uff1a<\/p>\n

              \n
            • server\u7684rewrite\u5c06\u4f1a\u6267\u884c\uff0c\u4ece \/sample rewrite \u5230 \/foobar<\/li>\n
            • location \/foobar \u4f1a\u88ab\u5339\u914d<\/li>\n
            • location\u7684\u7b2c\u4e00\u4e2arewrite\u6267\u884c\uff0c\u4ece\/foobar rewrite\u5230\/foo<\/li>\n
            • location\u7684\u7b2c\u4e8c\u4e2arewrite\u6267\u884c\uff0c\u4ece\/foo rewrite\u5230\/bar<\/li>\n<\/ul>\n

              return \u6307\u4ee4\u63d0\u4f9b\u7684\u662f\u4e0d\u540c\u7684\u884c\u4e3a\uff1a<\/p>\n

              server\u00a0{\r\n\u00a0\u00a0location\u00a0\/\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0return<\/span>\u00a0200<\/span>;\r\n\u00a0\u00a0\u00a0\u00a0return<\/span>\u00a0404<\/span>;\r\n\u00a0\u00a0}\r\n}<\/pre>\n

              \u5728\u4e0a\u8ff0\u7684\u60c5\u51b5\u4e0b\uff0c\u7acb\u5373\u8fd4\u56de200\u3002<\/p>\n<\/div>\n<\/div>\n<\/div>\n

              \n
              \n
              \n
              \u5904\u7406\u8bf7\u6c42<\/strong><\/div>\n

              \u5728 Nginx \u5185\u90e8\uff0c\u4f60\u53ef\u4ee5\u6307\u5b9a\u591a\u4e2a\u865a\u62df\u670d\u52a1\u5668\uff0c\u6bcf\u4e2a\u865a\u62df\u670d\u52a1\u5668\u7528 server{} \u4e0a\u4e0b\u6587\u63cf\u8ff0\u3002<\/p>\n

              server<\/span>\u00a0{\r\n\u00a0\u00a0listen<\/span>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0*:80<\/span>\u00a0default_server;\r\n\u00a0\u00a0server_name<\/span>\u00a0netguru.co;\r\n\r\n\u00a0\u00a0return<\/span>\u00a0200<\/span>\u00a0\"Hello\u00a0from\u00a0netguru.co\"<\/span>;\r\n}\r\n\r\nserver<\/span>\u00a0{\r\n\u00a0\u00a0listen<\/span>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0*:80<\/span>;\r\n\u00a0\u00a0server_name<\/span>\u00a0foo.co;\r\n\r\n\u00a0\u00a0return<\/span>\u00a0200<\/span>\u00a0\"Hello\u00a0from\u00a0foo.co\"<\/span>;\r\n}\r\n\r\nserver<\/span>\u00a0{\r\n\u00a0\u00a0listen<\/span>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0*:81<\/span>;\r\n\u00a0\u00a0server_name<\/span>\u00a0bar.co;\r\n\r\n\u00a0\u00a0return<\/span>\u00a0200<\/span>\u00a0\"Hello\u00a0from\u00a0bar.co\"<\/span>;\r\n}<\/pre>\n

              \u8fd9\u5c06\u544a\u8bc9 Nginx \u5982\u4f55\u5904\u7406\u5230\u6765\u7684\u8bf7\u6c42\u3002Nginx \u5c06\u4f1a\u9996\u5148\u901a\u8fc7\u68c0\u67e5 listen \u6307\u4ee4\u6765\u6d4b\u8bd5\u54ea\u4e00\u4e2a\u865a\u62df\u4e3b\u673a\u5728\u76d1\u542c\u7ed9\u5b9a\u7684 IP \u7aef\u53e3\u7ec4\u5408\u3002<\/p>\n

              \u7136\u540e\uff0cserver_name \u6307\u4ee4\u7684\u503c\u5c06\u68c0\u6d4b Host \u5934(\u5b58\u50a8\u7740\u4e3b\u673a\u57df\u540d)\u3002<\/p>\n

              Nginx \u5c06\u4f1a\u6309\u7167\u4e0b\u5217\u987a\u5e8f\u9009\u62e9\u865a\u62df\u4e3b\u673a\uff1a<\/p>\n

                \n
              1. \u5339\u914dsever_name\u6307\u4ee4\u7684IP-\u7aef\u53e3\u4e3b\u673a<\/li>\n
              2. \u62e5\u6709default_server\u6807\u8bb0\u7684IP-\u7aef\u53e3\u4e3b\u673a<\/li>\n
              3. \u9996\u5148\u5b9a\u4e49\u7684IP-\u7aef\u53e3\u4e3b\u673a<\/li>\n
              4. \u5982\u679c\u6ca1\u6709\u5339\u914d\uff0c\u62d2\u7edd\u8fde\u63a5\u3002<\/li>\n<\/ol>\n

                \u4f8b\u5982\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a<\/p>\n

                Request\u00a0to\u00a0foo.co:80<\/span>\u00a0\u00a0\u00a0\u00a0\u00a0=>\u00a0\"Hello\u00a0from\u00a0foo.co\"<\/span>\r\nRequest\u00a0to\u00a0www.foo.co:80<\/span>\u00a0=>\u00a0\"Hello\u00a0from\u00a0netguru.co\"<\/span>\r\nRequest\u00a0to\u00a0bar.co:80<\/span>\u00a0\u00a0\u00a0\u00a0\u00a0=>\u00a0\"Hello\u00a0from\u00a0netguru.co\"<\/span>\r\nRequest\u00a0to\u00a0bar.co:81<\/span>\u00a0\u00a0\u00a0\u00a0\u00a0=>\u00a0\"Hello\u00a0from\u00a0bar.co\"<\/span>\r\nRequest\u00a0to\u00a0foo.co:81<\/span>\u00a0\u00a0\u00a0\u00a0\u00a0=>\u00a0\"Hello from bar.co\"<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n
                \n
                \n
                \n
                server_name<\/code>\u00a0\u6307\u4ee4<\/strong><\/span><\/div>\n

                server_name\u6307\u4ee4\u63a5\u53d7\u591a\u4e2a\u503c\u3002\u5b83\u8fd8\u5904\u7406\u901a\u914d\u7b26\u5339\u914d\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n

                server_name<\/span>\u00a0netguru.co\u00a0www.netguru.co;\u00a0#\u00a0exact\u00a0match<\/span>\r\nserver_name<\/span>\u00a0*.netguru.co<\/span>;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#\u00a0wildcard\u00a0matching<\/span>\r\nserver_name<\/span>\u00a0netguru.*<\/span>;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#\u00a0wildcard\u00a0matching<\/span>\r\nserver_name<\/span>\u00a0\u00a0~^[0<\/span>-9<\/span>]*\\.netguru\\.co$;\u00a0\u00a0\u00a0#\u00a0regexp\u00a0matching<\/span><\/pre>\n

                \u5f53\u6709\u6b67\u4e49\u65f6\uff0cnginx \u5c06\u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\uff1a<\/p>\n

                  \n
                1. \u786e\u5207\u7684\u540d\u5b57<\/li>\n
                2. \u6700\u957f\u7684\u901a\u914d\u7b26\u540d\u79f0\u4ee5\u661f\u53f7\u5f00\u59cb\uff0c\u4f8b\u5982\u201c* .example.org\u201d\u3002<\/li>\n
                3. \u6700\u957f\u7684\u901a\u914d\u7b26\u540d\u79f0\u4ee5\u661f\u53f7\u7ed3\u5c3e\uff0c\u4f8b\u5982\u201cmail.**\u201d<\/li>\n
                4. \u9996\u5148\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\uff08\u6309\u7167\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u987a\u5e8f\uff09<\/li>\n<\/ol>\n

                  Nginx \u4f1a\u5b58\u50a8 3 \u4e2a\u54c8\u5e0c\u8868\uff1a\u786e\u5207\u7684\u540d\u5b57\uff0c\u4ee5\u661f\u53f7\u5f00\u59cb\u7684\u901a\u914d\u7b26\uff0c\u548c\u4ee5\u661f\u53f7\u7ed3\u5c3e\u7684\u901a\u914d\u7b26\u3002\u5982\u679c\u7ed3\u679c\u4e0d\u5728\u4efb\u4f55\u8868\u4e2d\uff0c\u5219\u5c06\u6309\u987a\u5e8f\u8fdb\u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u6d4b\u8bd5\u3002<\/p>\n

                  \u503c\u5f97\u8c28\u8bb0\u7684\u662f<\/p>\n

                  server_name<\/span>\u00a0.netguru<\/span>.co<\/span>;<\/pre>\n

                  \u662f\u4e00\u4e2a\u6765\u81ea\u4e0b\u9762\u7684\u7f29\u5199<\/p>\n

                  server_name<\/span>\u00a0\u00a0netguru<\/span>.co<\/span>\u00a0\u00a0www<\/span>.netguru<\/span>.co<\/span>\u00a0\u00a0*.netguru<\/span>.co<\/span>;<\/pre>\n

                  \u6709\u4e00\u70b9\u4e0d\u540c\uff0c.netguru.co<\/code> \u5b58\u50a8\u5728\u7b2c\u4e8c\u5f20\u8868\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u6bd4\u663e\u5f0f\u58f0\u660e\u7684\u6162\u4e00\u70b9\u3002<\/p>\n<\/div>\n<\/div>\n<\/div>\n

                  \n
                  \n
                  \n
                  listen<\/code>\u00a0\u6307\u4ee4<\/strong><\/span><\/div>\n

                  \u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u80fd\u591f\u627e\u5230 listen \u6307\u4ee4\uff0c\u63a5\u53d7IP:\u7aef\u53e3\u503c<\/p>\n

                  listen<\/span>\u00a0127.0.0.1:80<\/span>;\r\nlisten<\/span>\u00a0127.0.0.1<\/span>;\u00a0\u00a0\u00a0\u00a0#\u00a0by\u00a0default\u00a0port\u00a0:80\u00a0is\u00a0used<\/span>\r\n\r\nlisten<\/span>\u00a0*:81<\/span>;\r\nlisten<\/span>\u00a081<\/span>;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#\u00a0by\u00a0default\u00a0all\u00a0ips\u00a0are\u00a0used<\/span>\r\n\r\nlisten<\/span>\u00a0[::]:80<\/span>;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#\u00a0IPv6\u00a0addresses<\/span>\r\nlisten<\/span>\u00a0[::1<\/span>];\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#\u00a0IPv6\u00a0addresses<\/span><\/pre>\n

                  \u7136\u800c\uff0c\u8fd8\u53ef\u4ee5\u6307\u5b9a UNIX-domain \u5957\u63a5\u5b57\u3002<\/p>\n

                  listen<\/span>\u00a0unix:\/var\/run\/nginx.sock;<\/pre>\n

                  \u4f60\u751a\u81f3\u53ef\u4ee5\u4f7f\u7528\u4e3b\u673a\u540d<\/p>\n

                  listen<\/span>\u00a0localhost<\/span>:80<\/span>;\r\nlisten<\/span>\u00a0netguru<\/span>.co<\/span>:80<\/span>;<\/pre>\n

                  \u4f46\u8bf7\u614e\u7528\uff0c\u7531\u4e8e\u4e3b\u673a\u53ef\u80fd\u65e0\u6cd5\u542f\u52a8 nginx\uff0c\u5bfc\u81f4\u65e0\u6cd5\u7ed1\u5b9a\u5728\u7279\u5b9a\u7684 TCP Socket\u3002<\/p>\n

                  \u6700\u540e\uff0c\u5982\u679c\u6307\u4ee4\u4e0d\u5b58\u5728\uff0c\u5219\u4f7f\u7528\u00a0*:80<\/code>\u3002<\/p>\n<\/div>\n<\/div>\n<\/div>\n

                  \n
                  \n
                  \n
                  \u6700\u5c0f\u5316\u914d\u7f6e<\/strong><\/div>\n

                  \u6709\u4e86\u8fd9\u4e9b\u77e5\u8bc6 - \u6211\u4eec\u5e94\u8be5\u80fd\u591f\u521b\u5efa\u5e76\u7406\u89e3\u8fd0\u884c nginx \u6240\u9700\u7684\u6700\u4f4e\u914d\u7f6e\u3002<\/p>\n

                  #\u00a0\/etc\/nginx\/nginx.conf<\/span>\r\n\r\nevents<\/span>\u00a0{}\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#\u00a0events\u00a0context\u00a0needs\u00a0to\u00a0be\u00a0defined\u00a0to\u00a0consider\u00a0config\u00a0valid<\/span>\r\n\r\nhttp<\/span>\u00a0{\r\n\u00a0server<\/span>\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0listen<\/span>\u00a080<\/span>;\r\n\u00a0\u00a0\u00a0\u00a0server_name<\/span>\u00a0\u00a0netguru.co\u00a0\u00a0www.netguru.co\u00a0\u00a0*.netguru.co<\/span>;\r\n\r\n\u00a0\u00a0\u00a0\u00a0return<\/span>\u00a0200<\/span>\u00a0\"Hello\"<\/span>;\r\n\u00a0\u00a0}\r\n}<\/pre>\n
                  root,\u00a0location, \u548c try_files \u6307\u4ee4<\/strong><\/div>\n
                  root \u6307\u4ee4<\/strong><\/span><\/div>\n

                  root \u6307\u4ee4\u8bbe\u7f6e\u8bf7\u6c42\u7684\u6839\u76ee\u5f55\uff0c\u5141\u8bb8 nginx \u5c06\u4f20\u5165\u8bf7\u6c42\u6620\u5c04\u5230\u6587\u4ef6\u7cfb\u7edf\u3002<\/p>\n

                  server<\/span>\u00a0{\r\n\u00a0\u00a0listen<\/span>\u00a080<\/span>;\r\n\u00a0\u00a0server_name<\/span>\u00a0netguru.co;\r\n\u00a0\u00a0root<\/span>\u00a0\/var\/www\/netguru.co;\r\n}<\/pre>\n

                  \u6839\u636e\u7ed9\u5b9a\u7684\u8bf7\u6c42\uff0c\u6307\u5b9a nginx \u670d\u52a1\u5668\u5141\u8bb8\u7684\u5185\u5bb9<\/p>\n

                  netguru.co:80<\/span>\/index.html\u00a0\u00a0\u00a0\u00a0\u00a0#\u00a0returns\u00a0\/var\/www\/netguru.co\/index.html<\/span>\r\nnetguru.co:80<\/span>\/foo\/index.html\u00a0#\u00a0returns\u00a0\/var\/www\/netguru.co\/foo\/index.html\r\n<\/span><\/pre>\n
                  \n
                  \n
                  \n
                  location<\/code>\u00a0\u6307\u4ee4<\/strong><\/span><\/div>\n

                  location\u6307\u4ee4\u6839\u636e\u8bf7\u6c42\u7684 URI \u6765\u8bbe\u7f6e\u914d\u7f6e\u3002location [modifier] path<\/code><\/p>\n

                  location\u00a0\/foo\/\u00a0{\r\n\u00a0\u00a0#\u00a0...\r\n}<\/pre>\n

                  \u5982\u679c\u6ca1\u6709\u6307\u5b9a\u4fee\u9970\u7b26\uff0c\u5219\u8def\u5f84\u88ab\u89c6\u4e3a\u524d\u7f00\uff0c\u5176\u540e\u53ef\u4ee5\u8ddf\u968f\u4efb\u4f55\u4e1c\u897f\u3002<\/p>\n

                  \u4ee5\u4e0a\u4f8b\u5b50\u5c06\u5339\u914d<\/p>\n

                  \/foo\r\n\/fooo\r\n\/foo123\r\n\/foo\/bar\/index.html\r\n...<\/pre>\n

                  \u6b64\u5916\uff0c\u5728\u7ed9\u5b9a\u7684\u4e0a\u4e0b\u6587\u4e2d\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a location \u6307\u4ee4\u3002<\/p>\n

                  server\u00a0{\r\n\u00a0\u00a0listen\u00a080;\r\n\u00a0\u00a0server_name\u00a0netguru.co;\r\n\u00a0\u00a0root\u00a0\/var\/www\/netguru.co;\r\n\r\n\u00a0\u00a0location\u00a0\/\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0return\u00a0200\u00a0\"root\";\r\n\u00a0\u00a0}\r\n\r\n\u00a0\u00a0location\u00a0\/foo\/\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0return\u00a0200\u00a0\"foo\";\r\n\u00a0\u00a0}\r\n}<\/pre>\n
                  netguru.co:80\u00a0\u00a0\u00a0\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#\u00a0=>\u00a0\"root\"\r\nnetguru.co:80\u00a0\u00a0\u00a0\/foo\u00a0\u00a0\u00a0\u00a0#\u00a0=>\u00a0\"foo\"\r\nnetguru.co:80\u00a0\u00a0\u00a0\/foo123\u00a0#\u00a0=>\u00a0\"foo\"\r\nnetguru.co:80\u00a0\u00a0\u00a0\/bar\u00a0\u00a0\u00a0\u00a0#\u00a0=>\u00a0\"root\"<\/pre>\n

                  Nginx \u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u4fee\u9970\u7b26\uff0c\u53ef\u7528\u4e8e\u8fde\u63a5 location\u3002\u8fd9\u4e9b\u4fee\u9970\u7b26\u5c06\u5f71\u54cd location \u6a21\u5757\u4f7f\u7528\u7684\u5730\u65b9\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u4fee\u9970\u7b26\u90fd\u5206\u914d\u4e86\u4f18\u5148\u7ea7\u3002<\/p>\n

                  =\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0-\u00a0Exact\u00a0match\r\n^~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0-\u00a0Preferential\u00a0match\r\n~\u00a0&&\u00a0~*\u00a0\u00a0\u00a0\u00a0\u00a0-\u00a0Regex\u00a0match\r\nno\u00a0modifier\u00a0-\u00a0Prefix\u00a0match<\/pre>\n

                  Nginx \u4f1a\u5148\u68c0\u67e5\u7cbe\u786e\u5339\u914d\u3002\u5982\u679c\u627e\u4e0d\u5230\uff0c\u6211\u4eec\u4f1a\u627e\u4f18\u5148\u7ea7\u6700\u9ad8\u7684\u3002\u5982\u679c\u8fd9\u4e2a\u5339\u914d\u4f9d\u7136\u5931\u8d25\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u5c06\u6309\u7167\u51fa\u73b0\u7684\u987a\u5e8f\u8fdb\u884c\u6d4b\u8bd5\u3002\u81f3\u5c11\uff0c\u6700\u540e\u4e00\u4e2a\u524d\u7f00\u5339\u914d\u5c06\u88ab\u4f7f\u7528\u3002<\/p>\n

                  location\u00a0\/match\u00a0{\r\n\u00a0\u00a0return\u00a0200\u00a0'Prefix\u00a0match:\u00a0matches\u00a0everything\u00a0that\u00a0starting\u00a0with\u00a0\/match';\r\n}\r\n\r\nlocation\u00a0~*\u00a0\/match[0-9]\u00a0{\r\n\u00a0\u00a0return\u00a0200\u00a0'Case\u00a0insensitive\u00a0regex\u00a0match';\r\n}\r\n\r\nlocation\u00a0~\u00a0\/MATCH[0-9]\u00a0{\r\n\u00a0\u00a0return\u00a0200\u00a0'Case\u00a0sensitive\u00a0regex\u00a0match';\r\n}\r\n\r\nlocation\u00a0^~\u00a0\/match0\u00a0{\r\n\u00a0\u00a0return\u00a0200\u00a0'Preferential\u00a0match';\r\n}\r\n\r\nlocation\u00a0=\u00a0\/match\u00a0{\r\n\u00a0\u00a0return\u00a0200\u00a0'Exact\u00a0match';\r\n}<\/pre>\n
                  \/match\/\u00a0\u00a0\u00a0\u00a0# => 'Exact match'\r\n\/match0\u00a0\u00a0\u00a0\u00a0# => 'Preferential match'\r\n\/match1\u00a0\u00a0\u00a0\u00a0# => 'Case insensitive regex match'\r\n\/MATCH1\u00a0\u00a0\u00a0\u00a0# => 'Case sensitive regex match'\r\n\/match-abc # => 'Prefix match: matches everything that starting with \/match'<\/pre>\n<\/div>\n<\/div>\n<\/div>\n
                  \n
                  \n
                  \n
                  try_files<\/code>\u00a0\u6307\u4ee4<\/strong><\/span><\/div>\n

                  \u5c1d\u8bd5\u4e0d\u540c\u7684\u8def\u5f84\uff0c\u627e\u5230\u4e00\u4e2a\u8def\u5f84\u5c31\u8fd4\u56de\u3002<\/p>\n

                  try_files\u00a0$uri\u00a0index.html\u00a0=404;<\/pre>\n

                  \u6240\u4ee5\u5bf9\u4e8e\u00a0\/foo.html<\/code>\u00a0\u8bf7\u6c42\uff0c\u5b83\u5c06\u5c1d\u8bd5\u6309\u4ee5\u4e0b\u987a\u5e8f\u8fd4\u56de\u6587\u4ef6\uff1a<\/p>\n

                    \n
                  1. $uri ( \/foo.html )<\/li>\n
                  2. index.html<\/li>\n
                  3. \u5982\u679c\u4ec0\u4e48\u90fd\u6ca1\u627e\u5230\u5219\u8fd4\u56de 404<\/li>\n<\/ol>\n

                    \u6709\u8da3\u7684\u662f\uff0c\u5982\u679c\u6211\u4eec\u5728\u670d\u52a1\u5668\u4e0a\u4e0b\u6587\u4e2d\u5b9a\u4e49 try_files\uff0c\u7136\u540e\u5b9a\u4e49\u5339\u914d\u7684\u6240\u6709\u8bf7\u6c42\u7684 location \u2014\u2014 try_files \u5c06\u4e0d\u4f1a\u6267\u884c\u3002<\/p>\n

                    \u8fd9\u662f\u56e0\u4e3a\u5728\u670d\u52a1\u5668\u4e0a\u4e0b\u6587\u4e2d\u5b9a\u4e49\u7684 try_files \u662f\u5b83\u7684 pseudo-location\uff0c\u8fd9\u662f\u6700\u4e0d\u53ef\u80fd\u7684\u4f4d\u7f6e\u3002\u56e0\u6b64\uff0c\u5b9a\u4e49 location\/ \u5c06\u6bd4 pseudo-location \u66f4\u5177\u4f53\u3002<\/p>\n

                    server\u00a0{\r\n\u00a0\u00a0try_files\u00a0$uri\u00a0\/index.html\u00a0=404;\r\n\r\n\u00a0\u00a0location\u00a0\/\u00a0{\r\n\u00a0\u00a0}\r\n}<\/pre>\n

                    \u56e0\u6b64\uff0c\u4f60\u5e94\u8be5\u907f\u514d\u5728 server \u4e0a\u4e0b\u6587\u4e2d\u51fa\u73b0 try_files:<\/p>\n

                    server {\r\n\u00a0\u00a0location \/ {\r\n\u00a0\u00a0\u00a0\u00a0try_files $uri \/index.html =404;\r\n\u00a0\u00a0}\r\n}<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"

                    \u6211\u4eec\u4f1a\u544a\u8bc9\u4f60 Nginx \u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u5176\u80cc\u540e\u7684\u6982\u5ff5\u6709\u54ea\u4e9b\uff0c\u4ee5\u53ca\u5982\u4f55\u4f18\u5316\u5b83\u4ee5\u63d0\u5347\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u8fd8\u4f1a\u544a\u8bc9\u4f60\u5982\u4f55 […]<\/p>\n","protected":false},"author":643,"featured_media":176328,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-176319","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/176319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/643"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=176319"}],"version-history":[{"count":11,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/176319\/revisions"}],"predecessor-version":[{"id":176830,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/176319\/revisions\/176830"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/176328"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=176319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=176319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=176319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}