ThingWorx Flow > ThingWorx Flow 安装和管理 > 管理 ThingWorx Flow > 进行 ThingWorx Flow 的外部调用
进行 ThingWorx Flow 的外部调用
使用下列方法之一对 ThingWorx Flow 进行外部调用:
从外部网站对 ThingWorx Flow 进行 REST 调用
如果某个网站尝试对 ThingWorx Flow 服务器进行 REST 调用,则此服务器会阻止该请求,因为它是一个跨源请求。若要允许该请求,则必须将 ThingWorx Flow 服务器配置为允许来自网站的跨源请求。将 CORS (跨源资源共享) 筛选器添加到 ThingWorx Flow 服务器可启用跨源请求。之后,在其他服务器上部署的其他网站便可访问 ThingWorx Flow 服务器中的数据。
完成以下步骤,可供网站对 ThingWorx Flow 进行 REST 调用:
1. 关闭任何正在运行的 Nginx 实例。
2. 在 Nginx 安装下的下列位置打开 vhost-flow.conf 文件。
Windows: C:/Program Files/nginx-<version>/conf/conf.d
Linux: /etc/nginx/conf/conf.d
3. vhost-flow.conf 文件的 location /Thingworx 部分下,找到 proxy_set_header X-Content-Type-Options nosniff; 一行,然后添加以下行:
set $cors '';
# Right side of condition can be regular expression:
# if ($http_origin ~ '^https?://(localhost|www\.yourdomain\.com|www\.yourotherdomain\.com)')
if ($http_origin = '<Origin_that_calls_ThingWorx_Flow>') {
set $cors 'true';
}
if ($cors = 'true') {
add_header 'Access-Control-Allow-Origin' "$http_origin" always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
}
# OPTIONS indicates a CORS pre-flight request
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '$http_origin';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
4. 如果已在不同计算机上安装 ThingWorx FoundationThingWorx Flow,请执行以下操作:
vhost-flow.conf 文件中找到如下所示的行:
# add_header Content-Security-Policy
在上面所示的行下方添加以下行:
add_header Content-Security-Policy "frame-ancestors 'self' <Origin_that_calls_ThingWorx_Flow>;";
可以对域名进行硬编码或提供 RegEx。例如:add_header Content-Security-Policy "frame-ancestors 'self' https://*.ptcnet.ptc.com;";
5. <Origin_that_calls_ThingWorx_Flow> 替换为尝试访问 ThingWorx Flow 的网站的 URL。
6. 重新启动 Nginx 服务。
进行 ThingWorx Flow 的 WebSocket 调用
默认情况下,可使得 WebSocket Secure (wss) 调用 ThingWorx Flow
不支持 WebSocket (ws) 调用。