我正在使用Nginx來爲Angular 2客戶端和路由休息API服務於後端服務器。 在Angular開發模式中,我使用常規的'ng server',然後我應該將CORS支持添加到Nginx中,這種方式僅適用於作爲外部服務器的其餘API。如何從後端服務器壓縮Nginx CORS頭文件
我的Nginx的conf是:
server {
listen 80;
resolver 127.0.0.1 valid=30s;
root /path/dist-nginx;
index index.html index.htm;
location /prometheus/prometheus/ {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
return 204;
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://prometheus:9090/;
}
location/{
try_files $uri $uri/ /index.html;
#try_files $uri $uri/ /index.html =404;
}
}
- 位置/普羅米修斯/普羅米修斯是API
- 位置/服務角客戶
這是完全與第一請求工作:
http://localhost:3330/prometheus/prometheus/metrics
表明至少CORS設置工作正常。
但是,當客戶端執行以下請求:
http://localhost:3330/prometheus/prometheus/api/v1/query?query=http_request_size_bytes
我在瀏覽器這個問題:
XMLHttpRequest cannot load
http://local.appcelerator.io:3330/prometheus/prometheus/api/v1/query?query=http_request_size_bytes.
The 'Access-Control-Allow-Origin' header contains multiple values '*,
http://localhost:4200', but only one is allowed. Origin
'http://localhost:4200' is therefore not allowed access.
望着我得到的請求的頭回答:
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type
Access-Control-Allow-Headers:Accept, Authorization, Content-Type, Origin
Access-Control-Allow-Methods:GET, OPTIONS
Access-Control-Allow-Methods:GET, OPTIONS
Access-Control-Allow-Origin:http://localhost:4200
Access-Control-Allow-Origin:*
Access-Control-Expose-Headers:Date
Connection:keep-alive
Content-Encoding:gzip
Content-Length:657
Content-Type:application/json
Date:Sun, 11 Jun 2017 05:39:34 GMT
Server:nginx/1.13.1
因此,除了我的以外,還添加了3個CORS頭字段:
Access-Control-Allow-Origin:*
Access-Control-Allow-Headers:Accept, Authorization, Content-Type, Origin
Access-Control-Allow-Methods:GET, OPTIONS
直接客戶端請求直接Nginx,中間沒有人。
這兩個請求在nginx上的直接捲曲很完美。
如果我執行拳頭請求:
http://localhost:3330/prometheus/prometheus/metrics
的第二時間之後,所述一個女巫失敗了,我沒有錯誤,報頭字段是好的。
看來,問題是連結此請求,尤其是:
http://localhost:3330/prometheus/prometheus/api/v1/query?query=http_request_size_bytes
似乎後端服務器這裏普羅米修斯增加了自身的一些CORS頭時,它回答對URL API請求/ API/V1
我怎麼能乘坐他們?
謝謝你的任何想法,可以幫助瞭解那裏發生了什麼。
你說中間沒有任何東西,但是什麼'proxy_pass http:// prometheus:9090 /;'在做什麼?你確定那個代碼服務沒有添加自己的CORS頭文件嗎? –