{"id":1332,"date":"2024-06-13T09:54:36","date_gmt":"2024-06-13T01:54:36","guid":{"rendered":"https:\/\/blog.quantoyo.com\/?p=1332"},"modified":"2024-06-13T09:54:36","modified_gmt":"2024-06-13T01:54:36","slug":"nginx-proxy_cache-%e8%88%87-post","status":"publish","type":"post","link":"https:\/\/blog.quantoyo.com\/?p=1332","title":{"rendered":"nginx proxy_cache \u8207 POST"},"content":{"rendered":"\n<p>\u63d0\u4f9b\u4e00\u500b\u80fd\u4f7f\u7528\u5728POST\u7684 proxy_cache \u8a2d\u5b9a\u65b9\u5f0f\u8207\u6ce8\u610f\u4e8b\u9805<\/p>\n\n\n\n<!--more-->\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>#user  nobody;\nworker_processes  1;\nevents {\n    worker_connections  1024;\n}\n\nhttp {\n    proxy_cache_path \/tmp\/cache levels=1:2 keys_zone=quanto_cache:128m max_size=10g inactive=10s;\n    include       mime.types;\n    default_type  application\/octet-stream;\n    server {\n        listen       80;\n        server_name  localhost;\n        add_header 'Access-Control-Allow-Origin' '*';       #\u5141\u8a31\u6240\u6709\u4f86\u6e90\u8a2a\u554f\n        add_header 'Access-Control-Allow-Methods' 'POST'; #\u5141\u8a31\u6240\u6709\u65b9\u5f0f\u8a2a\u554f\uff0c\u53ef\u6539\u6210\u53ea\u6709POST\n\n        proxy_cache_lock on;                #\u78ba\u4fdd\u540c\u6642\u53ea\u6703\u767c\u4e00\u500bRequest\u5230\u5f8c\u7aef\n        proxy_cache_lock_age 300s;\n        proxy_cache_lock_timeout 300s;\n        \n        location \/ {\n            add_header Access-Control-Allow-Origin *;\n            try_files $uri @backend;\n        }\n\n        location @backend {\n            proxy_pass http:\/\/123.123.123.123:80;\n            proxy_set_header X-Real-IP $remote_addr;            #\u628aIP\u9001\u7d66\u53cd\u5411\u4ee3\u7406\u7684 server\n            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    #\u628aProtocol\u9001\u7d66\u53cd\u5411\u4ee3\u7406\u7684 server\n            proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;     #\u628aProtocol\u9001\u7d66\u53cd\u5411\u4ee3\u7406\u7684 server\n\n            proxy_cache quanto_cache;\n            proxy_buffering on;\n            proxy_cache_methods POST;                       #\u9019\u908acache POST\n            proxy_cache_key \"$request_uri|$request_body\";   #\u9700\u8981\u91cd\u65b0\u8a2d\u5b9akey\n            proxy_buffers 8 64k;\n            proxy_buffer_size 128k;\n            proxy_cache_valid any 10s;                      #\u53ea\u4fdd\u7559 1s\u6709\u6548\n            proxy_cache_min_uses 1;                         #\u81f3\u5c11\u4e00\u6b21\u8a2a\u554f\u624d\u958b\u555fcache\u529f\u80fd\n            proxy_cache_use_stale updating;                 #\u9084\u5728\u66f4\u65b0\u6642\u6703\u52c9\u5f37\u7528\u820a\u7684\n            proxy_cache_background_update on;               #\u5982\u679c\u7528\u4e86\u820a\u7684\uff0c\u4e5f\u6703\u66f4\u65b0\uff0c\u4e0b\u6b21\u7528\n            proxy_ignore_headers Cache-Control Set-Cookie;  #\u4f7f\u7528POST cache\u6642\u9700\u904e\u6ffe\u6389\u9019\u4e9b\n            add_header X-Cached-Edge $upstream_cache_status;\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5927\u90e8\u5206\u90fd\u5beb\u5728\u7bc4\u4f8b\u8a3b\u89e3\u5167\u4e86\uff0c\u8a72\u7bc4\u4f8b\u662f\u62ff\u4f86\u5c07\u524d\u7aef POST ajax request \u8f49\u50b3\u81f3\u5f8c\u7aef\u4f3a\u670d\u5668\uff0c\u4e26\u4e14\u5c07\u6703\u5728\u672c\u6a5f\u4e0acache 10\u79d2<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u56e0\u70ba\u9810\u8a2d\u7684proxy_cache_key \u4e26\u7121\u6cd5\u5340\u5206 POST \u7684\u5167\u5bb9\u5dee\u5225\uff0c\u6240\u4ee5\u5fc5\u9808\u91cd\u65b0\u8a2d\u5b9a\u4e00\u500bkey<\/li>\n\n\n\n<li>proxy_cache_valid \u8ddf inactive \u6709\u76f8\u4f3c\u529f\u80fd\uff0c\u524d\u8005\u70bacache \u529f\u80fd\u5167\u7684\u8a2d\u5b9a\uff0c\u5f8c\u8005\u6307\u7684\u662fcache \u4f7f\u7528\u7684\u6a94\u6848\u53ef\u4ee5\u5b58\u5728\u591a\u4e45\uff0c\u901a\u5e38\u6a94\u6848\u53ef\u4ee5\u4e45\u4e00\u9ede\u6216\u8005\u5169\u8005\u4e00\u6a23\u3002<\/li>\n\n\n\n<li>$upstream_cache_status \u53ef\u4ee5\u986f\u793a\u76ee\u524d\u9019\u6b21\u662f\u5426\u4f7f\u7528 cache \u5167\u5bb9 (MISS HIT&#8230; \u7b49\u7b49)<\/li>\n\n\n\n<li>proxy_ignore_headers \u5c0d\u65bc POST \u5f88\u91cd\u8981\uff0c\u9700\u8981\u52a0\u4e0a\u624d\u80fd\u6b63\u5e38\u4f7f\u7528cache\uff0c\u56e0\u70ba\u6b63\u5e38\u4f86\u8aaaPOST\u5167\u5bb9\u662f\u7368\u4e00\u7121\u4e8c\u7684\uff0c\u9810\u8a2d\u4e0d\u4f7f\u7528cache\u3002<\/li>\n\n\n\n<li>proxy_pass \u53ef\u4ee5\u6307\u5b9a\u55ae\u4e00\u7db2\u5740\u4f5c\u70ba\u5f8c\u7aef\uff0c\u4e5f\u53ef\u4ee5\u53e6\u5916\u8a2d\u7f6eupstream \u505a\u8ca0\u8f09\u5e73\u8861\u6a5f\u5236\u3002<\/li>\n\n\n\n<li>\u8a18\u5f97\u5148mkdir \u6a94\u6848\u76ee\u9304 mkdir \/tmp\/cache<\/li>\n\n\n\n<li>#user  nobody; \u628a\u8a3b\u89e3\u62ff\u6389\u5f8c\uff0c\u53ef\u4ee5\u6307\u5b9a\u7528\u8ab0\u7684\u540d\u7fa9\u8dd1nginx<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<p>\u53c3\u8003\u9023\u7d50\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/cloud.tencent.com\/developer\/article\/1051461\">https:\/\/cloud.tencent.com\/developer\/article\/1051461<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/www.app-scope.com\/tutorial\/configure-nginx-as-load-balancer.html\">https:\/\/www.app-scope.com\/tutorial\/configure-nginx-as-load-balancer.html<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/blog.csdn.net\/abu935009066\/article\/details\/130115516?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-1-130115516-blog-118713588.235^v43^pc_blog_bottom_relevance_base2&amp;spm=1001.2101.3001.4242.2&amp;utm_relevant_index=2\">https:\/\/blog.csdn.net\/abu935009066\/article\/details\/130115516?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-1-130115516-blog-118713588.235^v43^pc_blog_bottom_relevance_base2&amp;spm=1001.2101.3001.4242.2&amp;utm_relevant_index=2<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/sharefunyeh.gitbooks.io\/webdev\/content\/articles\/understand-nginx-proxy-load-balancing-buffer-and-cache.html\">https:\/\/sharefunyeh.gitbooks.io\/webdev\/content\/articles\/understand-nginx-proxy-load-balancing-buffer-and-cache.html<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/www.cnblogs.com\/Soy-technology\/p\/16357860.html\">https:\/\/www.cnblogs.com\/Soy-technology\/p\/16357860.html<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/ithelp.ithome.com.tw\/m\/articles\/10300110\">https:\/\/ithelp.ithome.com.tw\/m\/articles\/10300110<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/www.cnblogs.com\/lvzhenjiang\/p\/14022161.html\">https:\/\/www.cnblogs.com\/lvzhenjiang\/p\/14022161.html<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/50228180\/post-response-caching-does-not-work-in-nginx\">https:\/\/stackoverflow.com\/questions\/50228180\/post-response-caching-does-not-work-in-nginx<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/my.f5.com\/manage\/s\/article\/K000088625\">https:\/\/my.f5.com\/manage\/s\/article\/K000088625<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/nginx.org\/en\/docs\/http\/ngx_http_proxy_module.html#proxy_cache_key\">https:\/\/nginx.org\/en\/docs\/http\/ngx_http_proxy_module.html#proxy_cache_key<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/docs.nginx.com\/nginx\/admin-guide\/content-cache\/content-caching\/\">https:\/\/docs.nginx.com\/nginx\/admin-guide\/content-cache\/content-caching\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63d0\u4f9b\u4e00\u500b\u80fd\u4f7f\u7528\u5728POST\u7684 proxy_cache \u8a2d\u5b9a\u65b9\u5f0f\u8207\u6ce8\u610f\u4e8b\u9805<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[211,205,213,216,202,50,203,209,206,204,210,207,208,217,214,215,212],"class_list":["post-1332","post","type-post","status-publish","format-standard","hentry","category-technology","tag-upstream_cache_status","tag-access-control-allow-origin","tag-cdn","tag-edge","tag-nginx","tag-post","tag-proxy_cache","tag-proxy_cache_key","tag-proxy_cache_lock","tag-proxy_cache_path","tag-proxy_ignore_headers","tag-proxy_pass","tag-proxy_set_header","tag-x-cached","tag-214","tag-215","tag-212"],"_links":{"self":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/1332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1332"}],"version-history":[{"count":1,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/1332\/revisions"}],"predecessor-version":[{"id":1333,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/1332\/revisions\/1333"}],"wp:attachment":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}