具有多个来源的Nginx CORS可以使用GET,而不能使用POST方法



我有这个问题。我的老板希望我制作我们的Nginx代理,以便与多个来源合作。到目前为止还不错。我做了以下事情:

cors.conf文件中:

if ($http_origin ~ 'HERE COMES THE REGEX FOR THE ORIGINS') {
add_header 'Access-Control-Allow-Origin' '$http_origin' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,username,x-auth-token' always;
} 

然后在nginx.conf中,我这样做:

server {
etc...
etc...
location /api/api1/ {
proxy_pass http://api1/;
include     cors.conf;
}
etc... 

问题:它适用于来自两个来源的所有GET请求,但对其他任何请求都失败。到目前为止,我找不到解决方案,但我需要一个。任何帮助或想法都将不胜感激。谢谢

附言:我尝试使用地图而不是如果,仍然不能使用

我自己刚刚找到了一个基于以下内容的解决方案:https://gist.github.com/Stanback/7145487?permalink_comment_id=2752219#gistcomment-2752219

在我的cors.conf文件中,我将内容更改为:

set $origin '$http_origin';
if ($origin !~ 'HERE COMES THE REGEX FOR THE ORIGINS') {
set $origin 'Here comes the url of one of the allowed origins';
}
add_header 'Access-Control-Allow-Origin' '$origin' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,username,x-auth-token' always;

最新更新