Ôõô½â¾önginx¿çÓòÎÊÌâ
½â¾ö nginx ¿çÓòÎÊÌâÓÐÁ½ÖÖÒªÁ죺Ð޸ĿçÓòÏìӦͷ£ºÌí¼ÓÖ¸ÁîÒÔÔÊÐí¿çÓòÇëÇó£¬Ö¸¶¨ÔÊÐíµÄÒªÁìºÍÍ·£¬ÒÔ¼°ÉèÖûº´æʱ¼ä¡£Ê¹Óà cors Ä£¿é£ºÆôÓÃÄ£¿é²¢ÉèÖà cors ¹æÔò£¬ÔÊÐí¿çÓòÇëÇó¡¢ÒªÁ졢ͷºÍÉèÖûº´æʱ¼ä¡£
½â¾ö Nginx ¿çÓòÎÊÌâ
¿çÓòÎÊÌâÊÇÖ¸ä¯ÀÀÆ÷³öÓÚÇ徲˼Á¿£¬ÏÞÖÆÀ´×Ô²î±ðÓòÃûµÄÍøÒ³¾ç±¾»á¼ûÆäËûÓòÃûµÄ×ÊÔ´¡£ÔÚʹÓà Nginx ×÷Ϊ Web ЧÀÍÆ÷ʱ£¬¿ÉÄÜ»áÓöµ½¿çÓòÎÊÌâ¡£
ÔõÑù½â¾ö Nginx ¿çÓòÎÊÌâ
½â¾ö Nginx ¿çÓòÎÊÌâÓÐÁ½ÖÖÖ÷ÒªÒªÁ죺
1. Ð޸ĿçÓòÏìӦͷ
ÔÚ Nginx ÉèÖÃÎļþÖÐÌí¼ÓÒÔÏÂÖ¸ÁÒÔÐ޸ĿçÓòÏìӦͷ£º
add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods GET, POST, PUT, DELETE, OPTIONS; add_header Access-Control-Allow-Headers Content-Type, Authorization; add_header Access-Control-Max-Age 86400;
µÇ¼ºó¸´ÖÆ
ÕâЩָÁîµÄ×÷ÓÃÈçÏ£º
Access-Control-Allow-Origin: * ÔÊÐíÀ´×ÔËùÓÐÓòÃûµÄÇëÇó¡£
Access-Control-Allow-Methods Ö¸¶¨ÔÊÐíµÄÇëÇóÒªÁì¡£
Access-Control-Allow-Headers Ö¸¶¨ÔÊÐíµÄÇëÇóÍ·¡£
Access-Control-Max-Age Ö¸¶¨Ô¤¼ìÇëÇóµÄЧ¹û»º´æʱ¼ä¡£
2. ʹÓà CORS Ä£¿é
Nginx ¹Ù·½ÌṩÁËÒ»¸öÃûΪ ngx_http_cors_module µÄ CORS Ä£¿é¡£Ëü¿ÉÒÔ¸üÎÞаµØ´¦Àí¿çÓòÇëÇó¡£
ÒªÔÚ Nginx ÖÐÆôÓà CORS Ä£¿é£¬ÇëÔÚ main ÉÏÏÂÎÄÖÐÌí¼ÓÒÔÏÂÖ¸Á
load_module modules/ngx_http_cors_module.so;
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÔÚ server ÉÏÏÂÎÄÖÐ, Ìí¼ÓÒÔÏÂÖ¸ÁîÀ´ÉèÖà CORS ¹æÔò£º
cors_allow_origin all; cors_allow_methods all; cors_allow_headers all; cors_max_age 86400;
µÇ¼ºó¸´ÖÆ
ÕâЩָÁîÓëÐ޸ĿçÓòÏìӦͷµÄ·½·¨×÷ÓÃÀàËÆ¡£
ÒÔÉϾÍÊÇÔõô½â¾önginx¿çÓòÎÊÌâµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡