ÔõÑùʹÓÃNginx Proxy ManagerʵÏÖ¿çÓò»á¼ûµÄÊÚȨÖÎÀí
ÔõÑùʹÓÃNginx Proxy ManagerʵÏÖ¿çÓò»á¼ûµÄÊÚȨÖÎÀí
Nginx Proxy ManagerÊÇÒ»¸öÇ¿Ê¢µÄÊðÀíЧÀÍÆ÷£¬¿ÉÒÔʵÏÖ·´ÏòÊðÀí¡¢¸ºÔØƽºâ¡¢SSL/TLSÖÕ¶ËÊðÀíµÈ¹¦Ð§¡£ÔÚÏÖʵӦÓÃÖУ¬ÎÒÃǾ³£»áÓöµ½Ç°¶Ë¿çÓò»á¼ûµÄÎÊÌ⣬ΪÁ˱£»¤ºó¶Ë×ÊÔ´£¬ÎÒÃÇÐèÒª¾ÙÐÐÊÚȨÖÎÀí¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃNginx Proxy ManagerʵÏÖ¿çÓò»á¼ûµÄÊÚȨÖÎÀí£¬²¢ÌṩһЩÏêϸµÄ´úÂëʾÀý¡£
×°ÖúÍÉèÖÃNginx Proxy Manager
Ê×ÏÈ£¬ÎÒÃÇÐèҪװÖúÍÉèÖÃNginx Proxy Manager¡£¿ÉÒÔͨ¹ýÒÔÏ°취¾ÙÐвÙ×÷£º
1.1 ×°ÖÃNginx Proxy Manager
¿ÉÒÔͨ¹ý¹Ù·½ÍøÕ¾»òÕßÆäËûÇþµÀÏÂÔØNginx Proxy ManagerµÄ×°Öðü£¬²¢Æ¾Ö¤Æä¹Ù·½Îĵµ¾ÙÐÐ×°Öá£
1.2 ÉèÖÃNginx Proxy Manager
ÔÚ×°ÖÃÍê³Éºó£¬ÎÒÃÇÐèÒª¶ÔNginx Proxy Manager¾ÙÐÐÉèÖá£ÉèÖÃÎļþÒ»Ñùƽ³£Î»ÓÚ/etc/nginx/nginx.conf£¬¿ÉÒÔʹÓÃÎı¾±à¼Æ÷·¿ª¸ÃÎļþ¾ÙÐÐÉèÖá£
ÉèÖÿçÓò»á¼ûÊÚȨÖÎÀí
½ÓÏÂÀ´£¬ÎÒÃǽ«ÉèÖÿçÓò»á¼ûµÄÊÚȨÖÎÀí¡£ÏêϸµÄÉèÖÃÈçÏÂËùʾ£º
2.1 ½ç˵¿çÓò»á¼ûµÄÊÚȨ¹æÔò
ÔÚNginx Proxy ManagerµÄÉèÖÃÎļþÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃlocationÖ¸Áî½ç˵¿çÓò»á¼ûµÄÊÚȨ¹æÔò¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏ·½·¨½ç˵һ¸öÔÊÐíÌض¨ÓòÃû¾ÙÐпçÓò»á¼ûµÄ¹æÔò£º
location /api { add_header 'Access-Control-Allow-Origin' 'http://example.com'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type'; add_header 'Access-Control-Allow-Credentials' 'true'; }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöʾÀýÖУ¬ÎÒÃÇʹÓÃadd_headerÖ¸ÁîÌí¼ÓÁËһЩ¿çÓò»á¼ûµÄÊÚȨͷ×ֶΡ£ÆäÖУ¬Access-Control-Allow-Origin×Ö¶ÎÖ¸¶¨ÁËÔÊÐí¿çÓò»á¼ûµÄÓòÃû£»Access-Control-Allow-Methods×Ö¶ÎÖ¸¶¨ÁËÔÊÐíµÄHTTPÒªÁ죻Access-Control-Allow-Headers×Ö¶ÎÖ¸¶¨ÁËÔÊÐíµÄHTTPÍ·×ֶΣ»Access-Control-Allow-Credentials×Ö¶ÎÖ¸¶¨ÁËÊÇ·ñÔÊÐíЯ´øcookie¾ÙÐпçÓò»á¼û¡£
2.2 ÉèÖÿçÓò»á¼ûµÄ¹ýʧ´¦Àí
ΪÁËÌá¸ßÇå¾²ÐÔ£¬µ±¿çÓò»á¼ûµÄÊÚȨ¹æÔò²»Æ¥Åäʱ£¬ÎÒÃÇ¿ÉÒÔÉèÖÃNginx Proxy Manager·µ»ØÌض¨µÄ¹ýʧÐÅÏ¢¡£ÀýÈ磬¿ÉÒÔͨ¹ýÒÔÏ·½·¨ÉèÖ÷µ»Ø403 Forbidden¹ýʧ£º
location /api { if ($http_origin != http://example.com) { return 403; } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöʾÀýÖУ¬ÎÒÃÇʹÓÃifÖ¸ÁîÅжϿçÓò»á¼ûµÄÓòÃûÊÇ·ñÇкÏÒªÇó¡£ÈôÊDz»ÇкÏÒªÇó£¬Ôò·µ»Ø403¹ýʧ¡£
ÔËÐÐNginx Proxy Manager
ÉèÖÃÍê³Éºó£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÆô¶¯Nginx Proxy Manager£º
sudo service nginx start
µÇ¼ºó¸´ÖÆ
´Ëʱ£¬Nginx Proxy Manager½«×îÏȼàÌýÉèÖõĶ˿ڣ¬²¢Æ¾Ö¤ÉèÖõĿçÓò»á¼ûÊÚȨ¹æÔò¾ÙÐд¦Àí¡£
Ç°¶Ë´úÂëʾÀý
×îºó£¬ÎÒÃÇÌṩһ¸ö¼òÆÓµÄÇ°¶Ë´úÂëʾÀý£¬ÓÃÓÚÑÝʾÔõÑù¾ÙÐпçÓò»á¼û¡£ÏêϸʾÀýÈçÏ£º
const url = 'http://api.example.com'; const headers = { 'Authorization': 'Bearer xxxxxxxx', 'Content-Type': 'application/json' }; fetch(url, { method: 'GET', headers: headers, credentials: 'include' }) .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error(error));
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöʾÀýÖУ¬ÎÒÃÇʹÓÃfetchº¯Êý·¢ËÍÒ»¸ö¿çÓò»á¼ûµÄGETÇëÇó¡£ÆäÖУ¬url±äÁ¿Ö¸¶¨ÁË¿çÓò»á¼ûµÄÄ¿µÄURL£»headers±äÁ¿Ö¸¶¨ÁËÇëÇóµÄHTTPÍ·×ֶΣ»credentials²ÎÊýÖ¸¶¨ÁËÊÇ·ñЯ´øcookie¾ÙÐпçÓò»á¼û¡£
×ܽ᣺
±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃNginx Proxy ManagerʵÏÖ¿çÓò»á¼ûµÄÊÚȨÖÎÀí£¬²¢ÌṩÁËһЩÏêϸµÄ´úÂëʾÀý¡£Í¨¹ýÉèÖÃNginx Proxy ManagerµÄ¿çÓò»á¼û¹æÔò£¬ÎÒÃÇ¿ÉÒÔÎÞаµØ¿ØÖƶԺó¶Ë×ÊÔ´µÄ»á¼ûȨÏÞ£¬´Ó¶ø±£»¤ÏµÍ³µÄÇå¾²ÐÔ¡£Ï£Íû±¾ÎĶԶÁÕßÄܹ»ÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃNginx Proxy ManagerʵÏÖ¿çÓò»á¼ûµÄÊÚȨÖÎÀíµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡