{"id":50937,"date":"2024-10-08T14:50:16","date_gmt":"2024-10-08T06:50:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/50937\/"},"modified":"2024-10-08T14:50:16","modified_gmt":"2024-10-08T06:50:16","slug":"nginx-%e6%96%b0%e6%89%8b%e6%95%99%e7%a8%8b%ef%bc%9a%e6%8e%a7%e5%88%b6-http-%e8%ab%8b%e6%b1%82%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/50937\/","title":{"rendered":"Nginx \u65b0\u624b\u6559\u7a0b\uff1a\u63a7\u5236 HTTP \u8acb\u6c42\u65b9\u6cd5"},"content":{"rendered":"<h1 id=\"nginx-%e6%96%b0%e6%89%8b%e6%95%99%e7%a8%8b%ef%bc%9a%e6%8e%a7%e5%88%b6-http-%e8%ab%8b%e6%b1%82%e6%96%b9%e6%b3%95-uYwdqNaAIa\">Nginx \u65b0\u624b\u6559\u7a0b\uff1a\u63a7\u5236 HTTP \u8acb\u6c42\u65b9\u6cd5<\/h1>\n<p>Nginx \u662f\u4e00\u6b3e\u9ad8\u6548\u80fd\u7684\u7db2\u9801\u4f3a\u670d\u5668\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u975c\u614b\u5167\u5bb9\u7684\u63d0\u4f9b\u3001\u53cd\u5411\u4ee3\u7406\u3001\u8ca0\u8f09\u5747\u8861\u7b49\u5834\u666f\u3002\u5c0d\u65bc\u65b0\u624b\u4f86\u8aaa\uff0c\u4e86\u89e3\u5982\u4f55\u63a7\u5236 HTTP \u8acb\u6c42\u65b9\u6cd5\u662f\u4f7f\u7528 Nginx \u7684\u4e00\u500b\u91cd\u8981\u6b65\u9a5f\u3002\u672c\u6587\u5c07\u4ecb\u7d39 Nginx \u4e2d\u7684 HTTP \u8acb\u6c42\u65b9\u6cd5\u53ca\u5176\u914d\u7f6e\u65b9\u5f0f\u3002<\/p>\n<h2 id=\"http-%e8%ab%8b%e6%b1%82%e6%96%b9%e6%b3%95%e6%a6%82%e8%bf%b0-uYwdqNaAIa\">HTTP \u8acb\u6c42\u65b9\u6cd5\u6982\u8ff0<\/h2>\n<p>HTTP \u8acb\u6c42\u65b9\u6cd5\u662f\u7528\u4f86\u6307\u793a\u4f3a\u670d\u5668\u6240\u9700\u57f7\u884c\u7684\u64cd\u4f5c\u3002\u5e38\u898b\u7684\u8acb\u6c42\u65b9\u6cd5\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>GET<\/strong>\uff1a\u8acb\u6c42\u6307\u5b9a\u7684\u8cc7\u6e90\uff0c\u901a\u5e38\u7528\u65bc\u7372\u53d6\u6578\u64da\u3002<\/li>\n<li><strong>POST<\/strong>\uff1a\u5411\u6307\u5b9a\u8cc7\u6e90\u63d0\u4ea4\u6578\u64da\uff0c\u901a\u5e38\u7528\u65bc\u8868\u55ae\u63d0\u4ea4\u3002<\/li>\n<li><strong>PUT<\/strong>\uff1a\u66f4\u65b0\u6307\u5b9a\u8cc7\u6e90\u7684\u5167\u5bb9\u3002<\/li>\n<li><strong>DELETE<\/strong>\uff1a\u522a\u9664\u6307\u5b9a\u8cc7\u6e90\u3002<\/li>\n<li><strong>HEAD<\/strong>\uff1a\u7372\u53d6\u6307\u5b9a\u8cc7\u6e90\u7684\u5143\u6578\u64da\uff0c\u4e0d\u8fd4\u56de\u8cc7\u6e90\u672c\u8eab\u3002<\/li>\n<\/ul>\n<p>\u9019\u4e9b\u65b9\u6cd5\u5728\u4e0d\u540c\u7684\u61c9\u7528\u5834\u666f\u4e2d\u626e\u6f14\u8457\u91cd\u8981\u89d2\u8272\uff0c\u4e86\u89e3\u5b83\u5011\u7684\u4f7f\u7528\u60c5\u5883\u5c0d\u65bc\u914d\u7f6e Nginx \u662f\u975e\u5e38\u5fc5\u8981\u7684\u3002<\/p>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%9c%a8-nginx-%e4%b8%ad%e6%8e%a7%e5%88%b6-http-%e8%ab%8b%e6%b1%82%e6%96%b9%e6%b3%95-uYwdqNaAIa\">\u5982\u4f55\u5728 Nginx \u4e2d\u63a7\u5236 HTTP \u8acb\u6c42\u65b9\u6cd5<\/h2>\n<p>\u5728 Nginx \u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u901a\u904e\u914d\u7f6e\u6587\u4ef6\u4f86\u63a7\u5236\u5141\u8a31\u7684 HTTP \u8acb\u6c42\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n<h3 id=\"%e9%99%90%e5%88%b6%e8%ab%8b%e6%b1%82%e6%96%b9%e6%b3%95-uYwdqNaAIa\">\u9650\u5236\u8acb\u6c42\u65b9\u6cd5<\/h3>\n<p>\u5982\u679c\u60a8\u5e0c\u671b\u9650\u5236\u67d0\u4e9b\u8acb\u6c42\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>limit_except<\/code> \u6307\u4ee4\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\uff0c\u8a72\u793a\u4f8b\u50c5\u5141\u8a31 GET \u548c POST \u65b9\u6cd5\uff0c\u4e26\u62d2\u7d55\u5176\u4ed6\u6240\u6709\u65b9\u6cd5\uff1a<\/p>\n<pre><code>server {\n    listen 80;\n    server_name example.com;\n\n    location \/ {\n        limit_except GET POST {\n            deny all;\n        }\n    }\n}<\/code><\/pre>\n<p>\u5728\u9019\u500b\u914d\u7f6e\u4e2d\uff0c\u7576\u7528\u6236\u5617\u8a66\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\uff08\u5982 PUT \u6216 DELETE\uff09\u8a2a\u554f\u8a72\u4f3a\u670d\u5668\u6642\uff0c\u5c07\u6703\u6536\u5230 403 Forbidden \u932f\u8aa4\u3002<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8-try_files-%e6%8c%87%e4%bb%a4-uYwdqNaAIa\">\u4f7f\u7528 try_files \u6307\u4ee4<\/h3>\n<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u60a8\u53ef\u80fd\u5e0c\u671b\u6839\u64da\u8acb\u6c42\u65b9\u6cd5\u4f86\u8655\u7406\u4e0d\u540c\u7684\u8acb\u6c42\u3002\u9019\u6642\u53ef\u4ee5\u4f7f\u7528 <code>try_files<\/code> \u6307\u4ee4\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u6839\u64da\u8acb\u6c42\u65b9\u6cd5\u8fd4\u56de\u4e0d\u540c\u7684\u5167\u5bb9\uff1a<\/p>\n<pre><code>server {\n    listen 80;\n    server_name example.com;\n\n    location \/ {\n        try_files $uri $uri\/ =404;\n    }\n\n    location \/submit {\n        if ($request_method = POST) {\n            # \u8655\u7406 POST \u8acb\u6c42\n            return 200 \"POST request received\";\n        }\n        if ($request_method = GET) {\n            # \u8655\u7406 GET \u8acb\u6c42\n            return 200 \"GET request received\";\n        }\n    }\n}<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u7576\u7528\u6236\u767c\u9001 GET \u6216 POST \u8acb\u6c42\u5230 \/submit \u8def\u5f91\u6642\uff0c\u4f3a\u670d\u5668\u5c07\u6839\u64da\u8acb\u6c42\u65b9\u6cd5\u8fd4\u56de\u4e0d\u540c\u7684\u97ff\u61c9\u3002<\/p>\n<h2 id=\"%e6%b8%ac%e8%a9%a6%e9%85%8d%e7%bd%ae-uYwdqNaAIa\">\u6e2c\u8a66\u914d\u7f6e<\/h2>\n<p>\u5b8c\u6210\u914d\u7f6e\u5f8c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>nginx -t<\/code> \u547d\u4ee4\u4f86\u6e2c\u8a66\u914d\u7f6e\u6587\u4ef6\u7684\u6b63\u78ba\u6027\u3002\u5982\u679c\u4e00\u5207\u6b63\u5e38\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>systemctl reload nginx<\/code> \u4f86\u91cd\u65b0\u52a0\u8f09 Nginx \u914d\u7f6e\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-uYwdqNaAIa\">\u7e3d\u7d50<\/h2>\n<p>\u63a7\u5236 HTTP \u8acb\u6c42\u65b9\u6cd5\u662f Nginx \u914d\u7f6e\u4e2d\u7684\u4e00\u500b\u91cd\u8981\u65b9\u9762\u3002\u901a\u904e\u9069\u7576\u7684\u914d\u7f6e\uff0c\u60a8\u53ef\u4ee5\u63d0\u9ad8\u4f3a\u670d\u5668\u7684\u5b89\u5168\u6027\u548c\u6027\u80fd\u3002\u7121\u8ad6\u662f\u9650\u5236\u4e0d\u5fc5\u8981\u7684\u8acb\u6c42\u65b9\u6cd5\uff0c\u9084\u662f\u6839\u64da\u8acb\u6c42\u65b9\u6cd5\u57f7\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\uff0cNginx \u90fd\u63d0\u4f9b\u4e86\u9748\u6d3b\u7684\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f\u975c\u614b\u7db2\u7ad9\u9084\u662f\u52d5\u614b\u61c9\u7528\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528 Nginx \u63a7\u5236 HTTP \u8acb\u6c42\u65b9\u6cd5\uff0c\u638c\u63e1\u57fa\u672c\u914d\u7f6e\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u63d0\u5347\u7db2\u7ad9\u5b89\u5168\u6027\u8207\u6027\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-50937","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50937","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=50937"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50937\/revisions"}],"predecessor-version":[{"id":50938,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50937\/revisions\/50938"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=50937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=50937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=50937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}