{"id":172660,"date":"2020-02-21T10:08:01","date_gmt":"2020-02-21T02:08:01","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=172660"},"modified":"2020-02-19T10:13:15","modified_gmt":"2020-02-19T02:13:15","slug":"nginx-cors-reuwest","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/nginx-cors-reuwest.html","title":{"rendered":"Nginx\u89e3\u51b3\u524d\u7aef\u8de8\u57df\u95ee\u9898 CORS\u8de8\u57df\u914d\u7f6e"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u6700\u8fd1\u8fde\u7eed\u4e24\u4e2a\u670b\u53cb\u95ee\u6211\u8de8\u57df\u76f8\u5173\u95ee\u9898\uff0c\u6211\u731c\u60f3\u53ef\u80fd\u4e0d\u5c11\u670b\u53cb\u4e5f\u9047\u5230\u7c7b\u4f3c\u95ee\u9898\uff0c\u6211\u6253\u7b97\u5199\u4e2a\u535a\u5ba2\u804a\u4e00\u4e0b\u6211\u5b9e\u9645\u4f7f\u7528\u7684\u914d\u7f6e\uff0c<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5148\u8bf4\u660e\u4e00\u4e0b\uff0c\u6211\u5e76\u4e0d\u592a\u4e86\u89e3\u8fd9\u914d\u7f6e\uff0c\u6ca1\u7cbe\u529b\u53bb\u4e86\u89e3\u592a\u591a\uff0c\u4f46\u6211\u89c9\u5f97\u5176\u4e2d\u6709\u4e00\u4e9b\u5173\u952e\u7684\u5c0f\u6ce8\u610f\u70b9\uff0c\u53ef\u80fd\u6709\u4e9b\u521d\u5b66\u8005\u4e0d\u592a\u6ce8\u610f\u5230\uff0c\u5bfc\u81f4\u914d\u7f6e\u6709\u95ee\u9898\uff0c\u672c\u6587\u7ae0\u53ef\u80fd\u53ea\u5bf9\u65b0\u624b\u6709\u70b9\u5e2e\u52a9\uff0c\u5982\u679c\u4f60\u6709\u597d\u914d\u7f6e\uff0c\u6b22\u8fce\u8bc4\u8bba\u56de\u590d\uff0c\u8ba9\u5927\u5bb6\u5b66\u4e60\uff01<\/p>\n

Nginx\u7684CORS\u914d\u7f6e\uff0c\u7f51\u4e0a\u592a\u591a\u8fd9\u914d\u7f6e\u4e86\uff0c\u4f46\u5927\u5bb6\u66f4\u591a\u7684\u590d\u5236\u7c98\u8d34\u3001\u8f6c\u53d1\uff0c\u51e0\u4e4e\u90fd\u662f\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u4e09\u4e24\u884c\uff1a<\/p>\n

add_header Access-Control-Allow-Origin *;\r\nadd_header Access-Control-Allow-Headers X-Requested-With;\r\nadd_header Access-Control-Allow-Methods GET,POST,OPTIONS;\r\n<\/pre>\n

\u8fd9\u6837\u6709\u7528\u4e48\uff1f\u6709\u7528\uff0c\u6211\u4ee5\u524d\u8fd9\u6837\u4f7f\u7528\u4e5f\u6b63\u5e38\u8fc7\uff0c\u4f46\u540e\u6765\u8fd8\u662f\u9047\u5230\u95ee\u9898\u4e86\uff0c\u53d1\u73b0\u6709\u4e9b\u9879\u76ee\u8bf7\u6c42\u5c31\u4e0d\u6210\u529f\uff0c\u4e5f\u9047\u5230\u6709\u4e9b\u6d4f\u89c8\u5668\u6210\u529f\uff0c\u6709\u4e9b\u6d4f\u89c8\u5668\u4e0d\u6210\u529f\uff1b<\/p>\n

\u6211\u4e5f\u5728\u7f51\u4e0a\u67e5\u627e\u5404\u79cd\u8d44\u6599\u548c\u8c03\u6574\u5199\u6cd5\uff0c\u6700\u540e\u6211\u8c03\u6574\u597d\u7684\u5199\u6cd5\uff0c\u57fa\u672c\u7684\u4f7f\u7528\u6ca1\u95ee\u9898\uff0c\u6211\u5728\u9879\u76ee\u4e2d\u4e5f\u4e00\u76f4\u4f7f\u7528\u7740\uff01<\/p>\n

\u4e0b\u9762\u53d1\u4e00\u6bb5\u6211\u5b9e\u9645\u9879\u76ee\u4e2d\u7684\u90e8\u5206\u914d\u7f6e\uff1a<\/p>\n

location \/aoda-web {\r\nadd_header 'Access-Control-Allow-Origin' $http_origin;\r\nadd_header 'Access-Control-Allow-Credentials' 'true';\r\nadd_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';\r\nadd_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';\r\nadd_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';\r\nif ($request_method = 'OPTIONS') {\r\nadd_header 'Access-Control-Max-Age' 1728000;\r\nadd_header 'Content-Type' 'text\/plain; charset=utf-8';\r\nadd_header 'Content-Length' 0;\r\nreturn 204;\r\n}\r\nroot html;\r\nindex index.html index.htm;\r\nproxy_pass http:\/\/127.0.0.1:8080;\r\nproxy_set_header Host $host;\r\nproxy_set_header X-Real-IP $remote_addr;\r\nproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\r\nproxy_set_header X-Forwarded-Proto $scheme;\r\nproxy_connect_timeout 5;\r\n}\r\n<\/pre>\n

\u8de8\u57df\u76f8\u5173\u7684\u914d\u7f6e\uff0c\u4e3b\u8981\u662f\u4e0b\u9762\u8fd9\u90e8\u5206\uff1a<\/p>\n

add_header 'Access-Control-Allow-Origin' $http_origin;\r\nadd_header 'Access-Control-Allow-Credentials' 'true';\r\nadd_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';\r\nadd_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';\r\nadd_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';\r\nif ($request_method = 'OPTIONS') {\r\nadd_header 'Access-Control-Max-Age' 1728000;\r\nadd_header 'Content-Type' 'text\/plain; charset=utf-8';\r\nadd_header 'Content-Length' 0;\r\nreturn 204;\r\n}\r\n<\/pre>\n

\u4e0b\u9762\u7b80\u5355\u8bb2\u89e3\u4e00\u4e0b\uff0c\u4ee5\u4fbf\u5927\u5bb6\u914d\u7f6e\u6210\u529f\uff01<\/p>\n

1\u3001Access-Control-Allow-Origin\uff0c\u8fd9\u91cc\u4f7f\u7528\u53d8\u91cf $http_origin\u53d6\u5f97\u5f53\u524d\u6765\u6e90\u57df\uff0c\u5927\u5bb6\u8bf4\u7528\u201c*\u201d\u4ee3\u8868\u5141\u8bb8\u6240\u6709\uff0c\u6211\u5b9e\u9645\u4f7f\u7528\u5e76\u4e0d\u6210\u529f\uff0c\u539f\u56e0\u672a\u77e5\uff1b<\/p>\n

2\u3001Access-Control-Allow-Credentials\uff0c\u4e3a true \u7684\u65f6\u5019\u6307\u8bf7\u6c42\u65f6\u53ef\u5e26\u4e0aCookie\uff0c\u81ea\u5df1\u6309\u60c5\u51b5\u914d\u7f6e\u5427\uff1b<\/p>\n

3\u3001Access-Control-Allow-Methods\uff0cOPTIONS\u4e00\u5b9a\u8981\u6709\u7684\uff0c\u53e6\u5916\u4e00\u822c\u4e5f\u5c31GET\u548cPOST\uff0c\u5982\u679c\u4f60\u6709\u5176\u5b83\u7684\u4e5f\u53ef\u52a0\u8fdb\u53bb\uff1b<\/p>\n

4\u3001Access-Control-Allow-Headers\uff0c\u8fd9\u4e2a\u8981\u6ce8\u610f\uff0c\u91cc\u9762\u4e00\u5b9a\u8981\u5305\u542b\u81ea\u5b9a\u4e49\u7684http\u5934\u5b57\u6bb5\uff08\u5c31\u662f\u8bf4\u524d\u7aef\u8bf7\u6c42\u63a5\u53e3\u65f6\uff0c\u5982\u679c\u5728http\u5934\u91cc\u52a0\u4e86\u81ea\u5b9a\u4e49\u7684\u5b57\u6bb5\uff0c\u8fd9\u91cc\u914d\u7f6e\u4e00\u5b9a\u8981\u5199\u4e0a\u76f8\u5e94\u7684\u5b57\u6bb5\uff09\uff0c\u4ece\u4e0a\u9762\u53ef\u770b\u5230\u6211\u5199\u7684\u6bd4\u8f83\u957f\uff0c\u6211\u5728\u7f51\u4e0a\u641c\u7d22\u4e00\u4e9b\u5e38\u7528\u7684\u5199\u8fdb\u53bb\u4e86\uff0c\u91cc\u9762\u6709\u201cweb-token\u201d\u548c\u201capp-token\u201d\uff0c\u8fd9\u4e2a\u662f\u6211\u9879\u76ee\u91cc\u524d\u7aef\u8bf7\u6c42\u65f6\u8bbe\u7f6e\u7684\uff0c\u6240\u4ee5\u6211\u5728\u8fd9\u91cc\u8981\u5199\u4e0a\uff1b<\/p>\n

5\u3001Access-Control-Expose-Headers\uff0c\u53ef\u4e0d\u8bbe\u7f6e\uff0c\u770b\u7f51\u4e0a\u5927\u81f4\u610f\u601d\u662f\u9ed8\u8ba4\u53ea\u80fd\u83b7\u8fd4\u56de\u5934\u76846\u4e2a\u57fa\u672c\u5b57\u6bb5\uff0c\u8981\u83b7\u53d6\u5176\u5b83\u989d\u5916\u7684\uff0c\u5148\u5728\u8fd9\u8bbe\u7f6e\u624d\u80fd\u83b7\u53d6\u5b83\uff1b<\/p>\n

6\u3001\u8bed\u53e5\u201c if ($request_method = 'OPTIONS') { \u201d\uff0c\u56e0\u4e3a\u6d4f\u89c8\u5668\u5224\u65ad\u662f\u5426\u5141\u8bb8\u8de8\u57df\u65f6\u4f1a\u5148\u5f80\u540e\u7aef\u53d1\u4e00\u4e2a options \u8bf7\u6c42\uff0c\u7136\u540e\u6839\u636e\u8fd4\u56de\u7684\u7ed3\u679c\u5224\u65ad\u662f\u5426\u5141\u8bb8\u8de8\u57df\u8bf7\u6c42\uff0c\u6240\u4ee5\u8fd9\u91cc\u5355\u72ec\u5224\u65ad\u8fd9\u4e2a\u8bf7\u6c42\uff0c\u7136\u540e\u76f4\u63a5\u8fd4\u56de\uff1b<\/p>\n

\u597d\u4e86\uff0c\u6309\u6211\u4e0a\u9762\u914d\u7f6e\u57fa\u672c\u90fd\u53ef\u4f7f\u7528\uff08\u6709\u4e9b\u670b\u53cb\u786e\u5b9a\u53ea\u767e\u5ea6\u590d\u5236\u4e86\u4e24\u884c\uff0c\u7136\u540e\u8bf4\u914d\u7f6e\u597d\u4e86\uff0c\u8ddf\u524d\u7aef\u670b\u53cb\u4e92\u626f\uff09\uff0c<\/p>\n

\u4e0b\u9762\u53d1\u4e00\u4e2a\u5b9e\u9645\u914d\u7f6e\u4f9b\u53c2\u8003\uff0c\u6211\u505a\u4e86\u5c11\u91cf\u66f4\u6539\uff0c\u5982\u4e0b\uff1a<\/p>\n

server {\r\nlisten 80;\r\nserver_name xxx.com;\r\n\r\nlocation \/xxx-web\/papi {\r\nadd_header 'Access-Control-Allow-Origin' $http_origin;\r\nadd_header 'Access-Control-Allow-Credentials' 'true';\r\nadd_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';\r\nadd_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';\r\nadd_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';\r\nif ($request_method = 'OPTIONS') {\r\nadd_header 'Access-Control-Max-Age' 1728000;\r\nadd_header 'Content-Type' 'text\/plain; charset=utf-8';\r\nadd_header 'Content-Length' 0;\r\nreturn 204;\r\n}\r\nroot html;\r\nindex index.html index.htm;\r\nproxy_pass http:\/\/127.0.0.1:7071;\r\nproxy_set_header Host $host;\r\nproxy_set_header X-Real-IP $remote_addr;\r\nproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\r\nproxy_set_header X-Forwarded-Proto $scheme;\r\nproxy_connect_timeout 5;\r\n}\r\n\r\nlocation \/xxx-web {\r\nadd_header 'Access-Control-Allow-Origin' $http_origin;\r\nadd_header 'Access-Control-Allow-Credentials' 'true';\r\nadd_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';\r\nadd_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';\r\nadd_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';\r\nif ($request_method = 'OPTIONS') {\r\nadd_header 'Access-Control-Max-Age' 1728000;\r\nadd_header 'Content-Type' 'text\/plain; charset=utf-8';\r\nadd_header 'Content-Length' 0;\r\nreturn 204;\r\n}\r\nroot html;\r\nindex index.html index.htm;\r\nproxy_pass http:\/\/127.0.0.1:8080;\r\nproxy_set_header Host $host;\r\nproxy_set_header X-Real-IP $remote_addr;\r\nproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\r\nproxy_set_header X-Forwarded-Proto $scheme;\r\nproxy_connect_timeout 5;\r\n}\r\n\r\nlocation \/ {\r\nroot \/var\/www\/xxx\/wechat\/webroot;\r\nindex index.html index.htm;\r\n}\r\n\r\nerror_page 500 502 503 504 \/50x.html;\r\nlocation = \/50x.html {\r\nroot html;\r\n}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u5148\u8bf4\u660e\u4e00\u4e0b\uff0c\u6211\u5e76\u4e0d\u592a\u4e86\u89e3\u8fd9\u914d\u7f6e\uff0c\u6ca1\u7cbe\u529b\u53bb\u4e86\u89e3\u592a\u591a\uff0c\u4f46\u6211\u89c9\u5f97\u5176\u4e2d\u6709\u4e00\u4e9b\u5173\u952e\u7684\u5c0f\u6ce8\u610f\u70b9\uff0c\u53ef\u80fd\u6709\u4e9b\u521d\u5b66\u8005\u4e0d\u592a\u6ce8\u610f\u5230\uff0c […]<\/p>\n","protected":false},"author":643,"featured_media":172698,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-172660","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\/172660","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=172660"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/172660\/revisions"}],"predecessor-version":[{"id":172699,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/172660\/revisions\/172699"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/172698"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=172660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=172660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=172660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}