如何在nginx API调用中添加解决CORS错误的头?

jvidinwx  于 2023-10-17  发布在  Nginx
关注(0)|答案(1)|浏览(152)

我的nginx后面有两个子域:panel.example.irAPI.example.ir当我从我的panelpanel.example. ir调用API.example.ir时,我看到一个错误:

Access to XMLHttpRequest at 'https://api.example.ir/api/test/pre_sale_customer_project_view?offset=0&limit=5&order=created_at.desc' from origin 'https://panel.example.ir' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

另外,当我将add_header Access-Control-Allow-Origin *;添加到我的APInginx conf时,我有另一个错误如下!

Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains multiple values 'panel.example.ir, *', but only one is allowed.

我现在能做什么?

ivqmmu1c

ivqmmu1c1#

也许看看这篇文章。对于add_header,当你将它用作代理时,你将堆叠头值。在这篇文章中,它描述了如何隐藏原始的头文件,以便用nginx中的给定头文件替换它。
How to overwrite http Content-Disposition header in nginx.conf location block?
所以在你的情况下,也许用途:

proxy_hide_header Access-Control-Allow-Origin;
add_header Access-Control-Allow-Origin panel.example.ir;

或者配置应用程序,使其始终使用正确的标头进行响应。

相关问题