向 logstash 发出 post 请求时出现 CORS 错误
CORS error while making post request to logstash
我已将 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 应用程序的请求我收到以下错误:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading
the remote resource
我在同一台 linux 机器上使用 docker 容器中的 nginx 托管了我的应用程序。
我试图通过将以下行添加到 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'。
非常感谢任何帮助。
谢谢。
您不仅需要配置 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';
}
}
我能够通过使用 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 it is redirected to http://10.x.x.120:8500.
发出 HTTP POST 请求时
瞧!! Logstash 获取数据是因为它正在监听 8500 端口。
这可能是一个解决方案,具体取决于您对接受 http 调用的 Logstash in our case we use the http 插件的设置。
The http plugin plugin supports the following configuration options:
http {
response_headers {
"Content-Type" => "application/x-www-form-urlencoded",
"Access-Control-Allow-Origin" => "*",
}
}
我有一个简单的解决方法。
使用logstash输入插件开启两个端口
- 假设 5544 没有凭据设置和“Access-Control-Allow-Origin”=>“*”
- 说 5545 凭据
然后将来自开放端口 5544(未经身份验证)的请求转发到具有授权的 5545 headers,以便请求得到保护。
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"
}
}
我已将 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 应用程序的请求我收到以下错误:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource
我在同一台 linux 机器上使用 docker 容器中的 nginx 托管了我的应用程序。 我试图通过将以下行添加到 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'。
您不仅需要配置 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';
}
}
我能够通过使用 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 it is redirected to http://10.x.x.120:8500.
发出 HTTP POST 请求时瞧!! Logstash 获取数据是因为它正在监听 8500 端口。
这可能是一个解决方案,具体取决于您对接受 http 调用的 Logstash in our case we use the http 插件的设置。 The http plugin plugin supports the following configuration options:
http {
response_headers {
"Content-Type" => "application/x-www-form-urlencoded",
"Access-Control-Allow-Origin" => "*",
}
}
我有一个简单的解决方法。 使用logstash输入插件开启两个端口
- 假设 5544 没有凭据设置和“Access-Control-Allow-Origin”=>“*”
- 说 5545 凭据
然后将来自开放端口 5544(未经身份验证)的请求转发到具有授权的 5545 headers,以便请求得到保护。
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"
}
}