相关疑难解决方法(0)

如何在Nginx代理服务器中启用CORS?

作为我的标题,这是位于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;
  }
}
Run Code Online (Sandbox Code Playgroud)

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. …
Run Code Online (Sandbox Code Playgroud)

reverse-proxy nginx node.js cors

16
推荐指数
1
解决办法
4万
查看次数

标签 统计

cors ×1

nginx ×1

node.js ×1

reverse-proxy ×1