当前位置: 首页 > 面试题库 >

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

程峻
2023-03-14
问题内容

作为我的标题,这是位于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.

那是服务器的回应
那是请求的样子


问题答案:

问题是您的if条件不会在中发送父项中的标头/。如果您检查飞行前响应标头,它将是

HTTP/1.1 204 No Content
Server: nginx/1.13.3
Date: Fri, 01 Sep 2017 05:24:04 GMT
Connection: keep-alive
Access-Control-Max-Age: 1728000
Content-Type: text/plain charset=UTF-8
Content-Length: 0

那什么也没给。因此,有两种可能的解决方案。复制add_header里面的if块也

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-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';
      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;
  }
}

或者您可以将其移动到位置块之外,以便每个请求都具有响应

server {
   listen 80;
   server_name api.localhost;

 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';

  location / {

    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;
  }
}

如果您只想在配置中允许CORS的某些位置。就像/api这样,您应该使用标题创建模板conf

 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';

然后使用

include conf.d/corsheaders.conf;

在你的OPTIONS块和/api块。因此,仅允许将CORS用于/api。如果您不在乎CORS的位置,则可以使用第二种方法将核心标头移动到服务器块



 类似资料:
  • 本文向大家介绍使用nginx设置代理服务器,包括了使用nginx设置代理服务器的使用技巧和注意事项,需要的朋友参考一下 nginx可以利用其反向代理的功能来进行负载均衡的实现,同时也可以使用其正向代理的功能设置代理服务器,比如在内网的环境中,在可以连接外网的机器上运行nginx作为代理服务器,其他机器通过设定此台机器的IP和port即可通过其连接上网,本文使用nginx官方镜像,通过如下步骤即可简

  • 无法启动nginx服务器OS:ubuntu服务器16.04 systemctl status nginx.status抛出此消息:nginx.service-高性能web服务器和反向代理服务器加载:加载(/lib/systemd/system/nginx.service;enabled;vendor preset:enabled)活动:失败(结果:exit-code)自UTC Thu 2016-1

  • 我试图设置nginx位置来处理各种路径,并将它们代理到我的WebApp。 以下是我的意见: 我想用各种路径访问myApp,比如:/myApp/abc、/myApp/def、myApp/geh或/myApp/zzz。当然,这些路径在MyApp中是不可用的。我希望他们指向根的myApp和保持URL。可以用nginx存档吗?

  • 问题内容: 我尝试将答案和论坛主题进行30种组合,但未找到正确的答案。 我需要做所有事情,但是如何启用curl int wamp服务器,因为我需要这样做? 有什么解决办法吗?我尝试取消注释-不起作用?我尝试插入wamp任务栏图标-php扩展名-php_curl我尝试将php_curl.dll复制到Win sistem中 不行 我需要做什么? 问题答案: 步骤如下: 关闭WAMP(如果正在运行) 导

  • 本文向大家介绍在Nginx服务器中启用SSL的配置方法,包括了在Nginx服务器中启用SSL的配置方法的使用技巧和注意事项,需要的朋友参考一下 生成证书 可以通过以下步骤生成一个简单的证书: 首先,进入你想创建证书和私钥的目录,例如: 创建服务器私钥,命令会让你输入一个口令: 创建签名请求的证书(CSR): 在加载SSL支持的Nginx并使用上述私钥时除去必须的口令: 启用一个 SSL 虚拟主机