【发布时间】:2018-02-09 17:10:26
【问题描述】:
正如我的标题,这是位于 conf.d/api-server.conf 中的配置文件
server {
listen 80;
server_name api.localhost;
location / {
add_header 'Access-Control-Allow-Origin' 'http://api.localhost';
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';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
proxy_redirect off;
proxy_set_header host $host;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-forward-for $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:3000;
}
}
nginx.conf 文件保持默认不变。
向 api.localhost (api.localhost/admin/login) 发送请求后,仍然收到 405 错误:
XMLHttpRequest cannot load http://api.localhost/admin/login. Response
to preflight request doesn't pass access control check: No 'Access-
Control-Allow-Origin' header is present on the requested resource.
Origin 'http://admin.localhost:3000' is therefore not allowed access.
The response had HTTP status code 405.
【问题讨论】:
-
您没有提到您是否遇到任何问题,如果是,那么问题是什么?
-
@Tarun Lalwani 当我尝试向 api.localhost 发送请求时仍然收到 405 错误,我不知道为什么
-
我尝试了
的评论,但它对我不起作用,出现以下错误:响应中“Access-Control-Allow-Credentials”标头的值是 ''当请求的凭据模式为“包含”时必须为“真”,直到我更改了这一行:add_header 'Access-Control-Allow_Credentials' 'true'; to: add_header 'Access-Control-Allow-Credentials' 'true';
标签: node.js nginx cors reverse-proxy