試圖通過Ajax訪問我的API和我得到這個錯誤:Nginx的訪問控制允許來源不工作
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:9090 ' is therefore not allowed access. The response had HTTP status code 404.
我NGINX配置是這樣的,我用光油了。
server {
listen 127.0.0.1:8080;
server_name api.example.cc;
access_log /var/log/nginx/api.access.log combined;
error_log /var/log/nginx/api.error.log;
root /home/spark/api.example.cc/web;
#index index.php;
try_files $uri /index.php;
set $cache_uri $request_uri;
location/{
add_header 'Access-Control-Allow-Origin' 'http://localhost:9090';
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,Authorization';
}
}
捲曲-X選項-i http://api.example.cc結果:
HTTP/1.1 204 No Content
Server: nginx/1.8.0
Date: Wed, 30 Dec 2015 20:14:27 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization
Access-Control-Max-Age: 1728000
Content-Type: text/plain charset=UTF-8
Content-Length: 0
X-Varnish: 65550
Age: 0
Via: 1.1 varnish-v4
Connection: keep-alive
捲曲-X GET/POST -i http://api.example.cc結果:
HTTP/1.1 403 Forbidden
Server: nginx/1.8.0
Date: Wed, 30 Dec 2015 20:23:17 GMT
Content-Type: text/html
Content-Length: 168
X-Varnish: 32823
Age: 0
Via: 1.1 varnish-v4
Connection: keep-alive
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.8.0</center>
</body>
</html>
你看到'curl -X OPTIONS -i http://127.0.0.1:8080'和GET/POST – kwarunek
剛更新了信息的問題。謝謝 – GIJOW
你是否分別用curl調用GET和POST?你應該得到403.是否阿賈克斯req使用相同的來源(域)? – kwarunek