javascript - 在同一台机器、不同端口上使用 Jenkins 和 Nginx Web App 解决 CORS
问题描述
我在尝试在生成 XMLHttpRequest 的 Web 应用程序上使用一些 javascript 代码启动 Jenkins 作业时遇到此错误。Jenkins 和 Web 应用程序都在同一台机器上,不同的端口。
Access to XMLHttpRequest at '<JENKINS_URL>' from origin '<WEB_APP_URL:443>' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Jenkins 在端口 8080 上,但我已将其重定向到端口 80。我在端口 443 上有 Web 应用程序。我在与 Jenkins 相关的 nginx 配置文件中有以下几行
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
proxy_pass http://localhost:8080;
proxy_read_timeout 90s;
}
但我仍然得到错误。奇怪的是,当我对 Jenkins URL 进行 curl 时,如下所示
curl -u <JENKINS_USER>:<TOKEN> -I <JENKINS_JOB_URL>
我得到这个输出
Server: nginx/1.16.1
Date: Wed, 03 Jun 2020 17:12:12 GMT
Location: http://<JENKINS_URL>/queue/item/331/
Connection: keep-alive
X-Content-Type-Options: nosniff
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range
Access-Control-Expose-Headers: Content-Length,Content-Range
它有标题,但我不知道还有什么问题。我可能会遗漏一些非常明显的东西,因为我对此很陌生,并且以前从未处理过 CORS,但是非常感谢任何帮助,谢谢。如果需要,很高兴上传其他信息。
另外一件事,我还在 /etc/sysconfig/jenkins 文件中编辑了以下参数,如下所示。
JENKINS_LISTEN_ADDRESS="127.0.0.1"
解决方案
终于想通了!
前几天我安装了一个名为“CORS support for Jenkins”的插件,但没有对其进行配置。我认为默认情况下 Jenkins 正在查看这个插件并看到我禁用了它。
一旦我启用插件并在 (JENKINS_URL)/configure 中配置它,并在我的 nginx 配置中删除“Access-Control-Allow-Origin: *”行,我的 javascript 现在可以工作了。
推荐阅读
- optimization - 我不知道如何在 CPLEX 中编写以下条件?
- extjs - Ext 容器布局在选择选项时破坏了多选组合框组件-EXTJS
- javascript - javascript - JSTree - 为什么在 javascript 中停止递归?
- javascript - 删除包含 JavaScript 中特定单词的 JSON 值
- ios - 从黑色到完全透明的快速渐变
- sql-server - 合并 JOIN 查询中的两行 SQL 数据
- javascript - 哪个更适合在 JavaScript 中转换为字符串?String() 还是模板文字?
- java - Servlet 中的重复语句 - 最佳实践?
- c# - Unity 3d 和 RavenDB:System.InvalidOperationException:无法转换文档 System.FormatException:输入字符串的格式不正确
- node.js - 节点 e2e 测试 - 异步 beforeEach/afterEach 挂钩在 Windows 上失败