在同一台机器上,通过不同的端口使用Jenkins和Nginx Web App解决CORS

问题描述 投票:0回答:1

我在尝试通过生成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应用程序。在Nginx配置文件中,与Jenkins相关的行如下:

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 -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"
javascript nginx jenkins networking cors
1个回答
0
投票

终于明白了!

前几天我没有配置它就安装了一个名为“ CORS对Jenkins的支持”的插件。我认为默认情况下,詹金斯(Jenkins)正在查看此插件,并发现我禁用了它。

一旦启用插件并在(JENKINS_URL)/ configure中对其进行配置,并在我的nginx配置中删除了“ Access-Control-Allow-Origin:*”行,我的JavaScript现在就可以使用了。

© www.soinside.com 2019 - 2024. All rights reserved.