在向Logstash发起POST请求时出现了CORS错误

7

我已经配置好了Logstash 1.5.2,使其在Linux机器上消耗http输入。

这是我的Logstash输入配置:

input {
        http {
                host => "10.x.x.120"
                port => "8500"
        }
}

我可以使用Linux机器上的curl -XPOST将数据发送到logstash。
但是,当我从我的angularJS应用程序中发出$http.post(url,postData);请求时,我收到以下错误:
跨域请求被阻止:同源策略不允许读取远程资源。
我已经在docker容器中使用nginx在同一台Linux机器上托管了我的应用程序。
我尝试通过将以下行添加到nginx.conf来配置nginx以允许CORS:
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

但错误仍然存在。 当我在浏览器地址栏中输入http://10.x.x.120:8500时,会得到“ok”的响应。 任何帮助都将不胜感激。 谢谢。

如果您执行 curl -i http://10.x.x.120:8500,那么响应头实际上是否包含 Access-Control-Allow-Origin: * - Daniel Cottone
1
不,它在头部没有显示。这是我得到的全部内容:http/1.1 200 ok - content-type:text/plain - content-length: 2 - JSNinja
那么看起来你的 nginx 配置有问题。请参考此处获取用于 nginx 的样本 CORS 配置。 - Daniel Cottone
是的,我尝试了这些设置,但是它们并没有起作用。我仍然得到相同的标题信息。 - JSNinja
4个回答

2
这可能是一个解决方案,取决于您在我们的情况下如何设置Logstash。我们使用http插件来接受http调用。http插件支持以下配置选项
http {
    response_headers {
        "Content-Type" => "application/x-www-form-urlencoded",
        "Access-Control-Allow-Origin" => "*",
    }
}

1

我通过在nginx中使用反向代理设置,成功运行了这个程序。

我修改了我的URL如下: http://10.x.x.120/logs

然后对nginx.conf文件进行了以下更改:

location^~ /logs {
    proxy_pass http://10.x.x.120:8500; 
}

现在,每当我的应用程序向 http://10.x.x.120:8500/logs 发送 HTTP POST 请求时,它会被重定向到 http://10.x.x.120:8500
太好了!因为 Logstash 正在监听端口 8500,所以可以获取数据。

0

我有一个简单的解决方法。

使用logstash输入插件打开两个端口:

  • 说5544,不需要设置凭据和“Access-Control-Allow-Origin” =>“*”
  • 说5545,需要凭据

然后将来自未经身份验证的开放端口5544的请求转发到带有授权标头的5545,以确保请求安全。

output {
  if [headers][http_host] == "localhost:5544" {
    http {
      url => "http://127.0.0.1:5545"
      retry_failed => false
      http_method => ["post"]
      headers => ["Authorization", "%{authKey}"]
   }
  } else if [headers][http_host] =~ ":5545"  {
    elasticsearch {
    hosts => ["elasticsearch:9200"]
    http_compression => "true"
    }
  }

0

你不仅需要配置POST/GET,还需要配置OPTIONS,这是我正在生产中使用并且有效的配置。

#
# Wide-open CORS config for nginx
#
location / {
     if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        #
        # Om nom nom cookies
        #
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        #
        # Custom headers and headers various browsers *should* be OK with but aren't
        #
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        #
        # Tell client that this pre-flight info is valid for 20 days
        #
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 200;
     }
     if ($request_method = 'POST') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
     }
     if ($request_method = 'GET') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
     }
}

谢谢@maurycy。我试过了,但是不起作用。 - JSNinja
你能展示更多错误信息吗?也许可以提供一张网络标签的截图,以便了解发生了什么问题。 - maurycy
将选项加200并没有改变任何东西。我仍然得到CORS错误。 - JSNinja
这是我用于不同答案的 Plunker,但它会对我的服务器进行 CORS 调用(在这种情况下是标准 LAMP),但请查看 OPTIONS 调用的标头 http://plnkr.co/edit/YOzP2VCPOXwh4qoQC73i?p=preview。 - maurycy
我们是否应该转到聊天窗口继续讨论? - JSNinja
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接