【问题标题】:CORS error while making post request to logstash向logstash发出发布请求时出现CORS错误
【发布时间】:2015-10-07 04:54:25
【问题描述】:

我已将 Logstash 1.5.2 配置为在 linux 机器上使用 http 输入。

这是我的logstash输入配置:

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

我可以使用 linux 机器上的 curl -XPOST 将数据发送到 logstash。

但是当我创建一个 $http.post(url, postData);来自我的 angularJS 应用程序的请求,我收到以下错误:

跨域请求被阻止:同源策略不允许读取 远程资源

我在 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 时,我会得到“好的”。

非常感谢任何帮助。 谢谢。

【问题讨论】:

  • 如果您使用curl -i http://10.x.x.120:8500,标题是否实际上是Access-Control-Allow-Origin: *
  • No is 没有在标题中显示。这就是我得到的全部 http/1.1 200 ok - content-type:text/plain - content-length: 2
  • 那么看来你的nginx配置有问题。 See this for a sample CORS config for nginx.
  • 是的,我尝试了这些设置,但这不起作用。我仍然得到相同的标题信息。

标签: angularjs linux http nginx cors


【解决方案1】:

这可能是一个解决方案,具体取决于您对Logstash 的设置,在我们的例子中,我们使用http 插件来接受http 调用。 The http plugin plugin supports the following configuration options

http {
    response_headers {
        "Content-Type" => "application/x-www-form-urlencoded",
        "Access-Control-Allow-Origin" => "*",
    }
}

【讨论】:

    【解决方案2】:

    我能够通过使用 nginx 的反向代理设置来运行它。

    我修改了我的网址如下: 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 端口。

    【讨论】:

      【解决方案3】:

      您不仅需要配置 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 我已经厌倦了这个,但它不起作用。
      • 您能否显示更多错误信息,或者从您的网络选项卡中截屏以查看发生了什么?
      • 将 200 添加到选项不会改变任何东西。我直到得到 CORS 错误。
      • 你重启nginx了吗?您是否使用正确的服务器?从屏幕截图中我可以看到OPTIONS 调用正确,但响应标头不显示任何应在 nginx 端设置的标头
      • 是的,我重新启动了 nginx。响应来自在 linux 机器上运行的 logstash 服务,因为 nginx 在同一台机器上的 docker 容器内。
      猜你喜欢
      • 2021-03-02
      • 2022-01-06
      • 2022-01-23
      • 2017-08-15
      • 2019-09-09
      • 1970-01-01
      • 1970-01-01
      • 2021-06-18
      • 2022-12-11
      相关资源
      最近更新 更多