{"id":1425,"date":"2024-11-09T19:45:17","date_gmt":"2024-11-09T11:45:17","guid":{"rendered":"https:\/\/note.ui11.cc\/?p=1425"},"modified":"2024-11-09T19:45:47","modified_gmt":"2024-11-09T11:45:47","slug":"nginx%e8%a7%a3%e5%86%b3%e8%b7%a8%e5%9f%9f%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/note.ui11.cc\/index.php\/2024\/11\/09\/nginx%e8%a7%a3%e5%86%b3%e8%b7%a8%e5%9f%9f%e9%97%ae%e9%a2%98\/","title":{"rendered":"Nginx\u89e3\u51b3\u8de8\u57df\u95ee\u9898"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Nginx \u914d\u7f6e\u793a\u4f8b<\/h4>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u6211\u4eec\u7684 Nginx \u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>location \/your\/path {\n    # \u5141\u8bb8\u6240\u6709\u57df\u540d\u8bbf\u95ee\n    add_header 'Access-Control-Allow-Origin' '*';\n    \n    # \u6216\u8005\u5141\u8bb8\u7279\u5b9a\u57df\u540d\u8bbf\u95ee\uff08\u793a\u4f8b\uff1aexample.com \u548c another-domain.com\uff09\n    # add_header 'Access-Control-Allow-Origin' 'https:\/\/example.com, https:\/\/another-domain.com';\n \n    # \u6307\u5b9a\u5141\u8bb8\u7684 HTTP \u65b9\u6cd5\n    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';\n \n    # \u6307\u5b9a\u5141\u8bb8\u7684\u8bf7\u6c42\u5934\n    add_header 'Access-Control-Allow-Headers' 'Origin, Authorization, Content-Type, Accept, X-Requested-With';\n \n    # \u5982\u679c\u8bf7\u6c42\u65b9\u6cd5\u4e3a OPTIONS\uff0c\u5219\u76f4\u63a5\u8fd4\u56de 204 \u72b6\u6001\u7801\n    if ($request_method = 'OPTIONS') {\n        add_header 'Access-Control-Allow-Origin' '*';\n        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';\n        add_header 'Access-Control-Allow-Headers' 'Origin, Authorization, Content-Type, Accept, X-Requested-With';\n        return 204;\n    }\n \n    # \u8f6c\u53d1\u8bf7\u6c42\u5230\u540e\u7aef\u670d\u52a1\n    proxy_pass http:\/\/192.168.99.12:10086;\n}<\/code><\/pre>\n\n\n\n<p>\u914d\u7f6e\u89e3\u6790<br>location \/your\/path:<\/p>\n\n\n\n<p>\u8be5\u6307\u4ee4\u5b9a\u4e49\u4e86\u4e00\u4e2a URI \u8def\u5f84\uff0c\u5f53\u8bf7\u6c42\u7684\u8def\u5f84\u5339\u914d \/your\/path \u65f6\uff0cNginx \u5c06\u5e94\u7528\u4ee5\u4e0b\u914d\u7f6e\u3002<br>add_header &#8216;Access-Control-Allow-Origin&#8217; &#8216;*&#8217;:<\/p>\n\n\n\n<p>\u5141\u8bb8\u6240\u6709\u57df\u8bbf\u95ee\u8be5\u8d44\u6e90\u3002\u4f7f\u7528 * \u8868\u793a\u5141\u8bb8\u4efb\u4f55\u6765\u6e90\u7684\u8bf7\u6c42\u3002\u8fd9\u79cd\u8bbe\u7f6e\u9002\u7528\u4e8e\u516c\u5171 API\uff0c\u4f46\u5728\u5b89\u5168\u6027\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\u4e0b\uff0c\u5efa\u8bae\u4f7f\u7528\u7279\u5b9a\u7684\u57df\u540d\u3002<br>\u5141\u8bb8\u7279\u5b9a\u57df\u540d\u8bbf\u95ee:<\/p>\n\n\n\n<p>\u5982\u679c\u60a8\u5e0c\u671b\u9650\u5236\u67d0\u4e9b\u7279\u5b9a\u7684\u57df\u540d\uff0c\u53ef\u4ee5\u7528\u5982\u4e0b\u65b9\u5f0f\u8bbe\u7f6e\uff1a<br>nginx<br>\u590d\u5236<\/p>\n\n\n\n<p>add_header &#8216;Access-Control-Allow-Origin&#8217; &#8216;https:\/\/example.com, https:\/\/another-domain.com&#8217;;<\/p>\n\n\n\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ea\u5141\u8bb8 https:\/\/example.com \u548c https:\/\/another-domain.com \u8bbf\u95ee\u8be5\u8d44\u6e90\u3002<br>add_header &#8216;Access-Control-Allow-Methods&#8217; &#8216;GET, POST, OPTIONS&#8217;:<\/p>\n\n\n\n<p>\u6307\u5b9a\u5141\u8bb8\u7684 HTTP \u65b9\u6cd5\uff0c\u8fd9\u91cc\u5305\u62ec GET\u3001POST \u548c OPTIONS\u3002<br>add_header &#8216;Access-Control-Allow-Headers&#8217; &#8216;Origin, Authorization, Content-Type, Accept, X-Requested-With&#8217;:<\/p>\n\n\n\n<p>\u6307\u5b9a\u5141\u8bb8\u7684\u8bf7\u6c42\u5934\uff0c\u786e\u4fdd\u5ba2\u6237\u7aef\u53ef\u4ee5\u53d1\u9001\u8fd9\u4e9b\u5934\u90e8\u4fe1\u606f\u3002<br>\u5904\u7406\u9884\u68c0\u8bf7\u6c42:<\/p>\n\n\n\n<p>CORS \u4e2d\u7684\u9884\u68c0\u8bf7\u6c42\u662f\u6307\u5728\u5b9e\u9645\u8bf7\u6c42\u524d\uff0c\u6d4f\u89c8\u5668\u5148\u53d1\u9001\u4e00\u4e2a OPTIONS \u8bf7\u6c42\uff0c\u4ee5\u786e\u5b9a\u670d\u52a1\u5668\u662f\u5426\u5141\u8bb8\u8be5\u8bf7\u6c42\u3002<br>if ($request_method = &#8216;OPTIONS&#8217;):<br>\u68c0\u67e5\u8bf7\u6c42\u65b9\u6cd5\u662f\u5426\u4e3a OPTIONS\u3002<br>add_header \u2026:<br>\u5982\u679c\u662f OPTIONS \u8bf7\u6c42\uff0c\u6dfb\u52a0\u76f8\u5e94\u7684 CORS \u5934\u90e8\u3002<br>return 204:<br>\u8fd4\u56de HTTP 204 \u72b6\u6001\u7801\uff0c\u8868\u793a\u8bf7\u6c42\u6210\u529f\u4e14\u6ca1\u6709\u5185\u5bb9\u8fd4\u56de\u3002<br>proxy_pass:<\/p>\n\n\n\n<p>proxy_pass http:\/\/192.168.99.12:10086;<br>\u5c06\u8bf7\u6c42\u8f6c\u53d1\u5230\u6307\u5b9a\u7684\u540e\u7aef\u670d\u52a1\u3002<br>\u901a\u8fc7\u4ee5\u4e0a\u914d\u7f6e\uff0cNginx \u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u8de8\u57df\u8bf7\u6c42\uff0c\u5141\u8bb8\u6765\u81ea\u4e0d\u540c\u6e90\u7684\u524d\u7aef\u5e94\u7528\u8bbf\u95ee\u5176\u8d44\u6e90\u3002\u914d\u7f6e\u4e2d\u7684 CORS \u5934\u90e8\u786e\u4fdd\u4e86\u6d4f\u89c8\u5668\u80fd\u591f\u6b63\u786e\u5730\u8bc6\u522b\u548c\u5904\u7406\u8de8\u57df\u8bf7\u6c42<br>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>                        <br><br><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Nginx \u914d\u7f6e\u793a\u4f8b \u4ee5\u4e0b\u662f\u6211\u4eec\u7684 Nginx \u914d\u7f6e\u793a\u4f8b\uff1a \u914d\u7f6e\u89e3\u6790location \/your\/path:  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-1425","post","type-post","status-publish","format-standard","hentry","category-springboot"],"_links":{"self":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts\/1425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/comments?post=1425"}],"version-history":[{"count":2,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts\/1425\/revisions"}],"predecessor-version":[{"id":1428,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts\/1425\/revisions\/1428"}],"wp:attachment":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/media?parent=1425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/categories?post=1425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/tags?post=1425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}