nginx。被 CORS 政策阻止

2023-11-29

当前 nginx 配置:

server {
    listen hidden:80;
    server_name dev.hidden.com;
    root /var/www/back/hidden-api;


    location / {
        add_header 'Access-Control-Allow-Origin' "$http_origin" always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
        add_header 'Access-Control-Allow-Headers' "Origin, X-Requested-With, Content-Type, Accept" always;

        proxy_pass http://localhost:8081;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

}

只是没有尝试,但错误仍然如下:

Access to XMLHttpRequest at 'http://dev.hidden.com/usr/register/do' from origin 'http://front.hidden.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

您可以尝试将以下内容添加到位置块内的配置中:

if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' "$http_origin" always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
        add_header 'Access-Control-Allow-Headers' "Origin, X-Requested-With, Content-Type, Accept" always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain; charset=utf-8';
        add_header 'Content-Length' 0;
        return 204;
     }

See https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Preflighted_requests有关 CORS 预检检查的更多信息。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

nginx。被 CORS 政策阻止 的相关文章

  • Docker 网络 - nginx:[emerg] 在上游找不到主机

    我最近开始迁移到 Docker 1 9 和 Docker Compose 1 5 的网络功能来取代使用链接 到目前为止 通过链接 nginx 通过 docker compose 连接到位于一组中不同服务器中的 php5 fpm fastcg
  • iFrame 在 Facebook Canvas 应用程序中显示为空白

    我有一个非常简单的页面 我试图在 Facebook iframe 中查看它 它是一个 Django 视图 但它不依赖于请求是通过 POST 还是 GET 提交 它所做的只是返回一些简单的 HTML 如果我们直接点击链接 它会正确显示 如果在
  • nginx - 使用 ssl 支持设置多个 server_name

    我很想使用 nginx 来为具有多个域名和 SSL 的网站提供服务 webmail example com webmail beispiel de 两者都使用相同的虚拟主机 因此我只设置 server name 两次 问题是 我需要 ngi
  • AngularJS 忽略一些标头

    我正在玩一点 Angular 遇到了一个小问题 我正在尝试为http响应设置一个自定义标头 然后在角度方面读取它的值 标头已设置 我确信这一点 因为 chrome 的调试工具确认了 这意味着服务器端没问题 到目前为止 一切都很好 当我尝试通
  • 从 git repo 拉取后出现白屏死机(React JS、Nginx)

    每当我从 master 分支执行 git pull 到服务器上时 我所有的 React 文件似乎都消失了 屏幕变成白色 我发现的临时解决方法是 删除浏览器 cookie 缓存和站点历史记录 然后关闭浏览器并重试 删除node modules
  • 防止 NGINX 中的一个 WebSocket 连接泛滥?

    我使用这个配置来防止我的服务器上出现类似 DOS 的洪水 limit req zone binary remote addr zone one 10m rate 10r s limit req zone binary remote addr
  • 仅当 url 以 www 为前缀时才会发生 CORS 错误

    我目前遇到一个关于 CORS 跨源资源共享 的问题 奇怪的是 只有当我使用 www url 前缀时 才会出现这种情况 例如 当我使用以下网址访问我的网站时 http example com index http example com in
  • Nginx Node.js 快速下载大文件止于 1.08GB

    我有这个由 Nginx 代理的 Node js 应用程序 在生产环境中 路线是这样的 exports download function req res var id req params id if id id latest res do
  • nginx 的“ssl”指令已弃用,请使用“listen ... ssl”

    NGINX 升级后v1 15 2开始收到警告 nginx warn the ssl directive is deprecated use the listen ssl directive instead in usr local etc
  • 使用 Nginx 自定义错误网关页面

    是否可以在 Nginx 中提供自定义的 Bad Gateway 错误页面 类似于自定义 404 页面 为了显示您的自定义错误页面而不是一般的 错误网关 错误 必须满足三个部分 您必须创建一个名为 500 html 之类的 html 文件并将
  • Nginx:在同一服务器上为多个 React 应用程序提供服务

    我正在尝试在同一服务器上设置多个 React 应用程序 问题是在我构建项目之后 index html from build 已找到 但辅助文件来自build static不是 最初 我只用一个应用程序location static 有别名
  • 使用 nginx 将 PATCH 请求代理为 POST

    我尝试使用 nginx 将 HTTP PATCH 请求重定向到 HTTP POST 请求 我还尝试了以下配置 但它不起作用 我收到 400 错误请求 http map request method my method default req
  • XmlHttpRequest CORS POST 发送时不带 cookie

    我有一个 Rails 服务为我的 AngularJS 前端应用程序返回数据 该服务配置为通过返回足够的标头来允许 CORS 请求 当我发出 GET 请求来接收数据时 会发送 CORS 标头 以及我之前在登录时收到的会话 cookie 您可以
  • nginx 502 错误网关

    当使用 Spawn fcgi 生成 php5 cgi 时 我收到 502 Bad Gateway with nginx 我使用它来跨越服务器启动上的实例 并在 rc local 中使用以下行 usr bin spawn fcgi a 127
  • 由于浏览器设置的标头,Safari 拒绝重定向的 CORS 请求

    Summary Safari 拒绝一些涉及重定向的 CORS 请求 声称某些标头是不允许的 但该标头从来不是由脚本请求的 而是由浏览器添加的 所以我认为这应该不重要 Safari 的行为是一个错误吗 规格有问题吗 或者 事情变成这样是有原因
  • Nginx 位置、别名、重写、根

    我正在通过 proxypass 提供 foo bar 服务 并希望继续这样做 但是 我想从 var www mystatic baz swf 等静态地提供 foo bar baz swf 服务 我希望我能做类似的事情 location fo
  • 使用 nginx 在云上部署 django 和 React

    我有一个 digitalocean 服务器 并且已经使用 Gunicorn 和 nginx 部署了 Django 后端服务器 如何在同一台服务器上部署 React 应用程序 您可以构建 React 应用程序并使用 Nginx 提供其静态文件
  • 通过 ESI:include 设置 Cookie,如何?

    我正在尝试使用 esi 在我的网站上创建忍者缓存 这个想法是 该网站大部分是静态的 我只需要在用户是否登录时做一些花哨的事情 所以我试图在页面A上放置一个 并在页面B的应用程序中设置触发器 这样我就可以将页面 A 缓存在 varnish 上
  • 使用 PM2 将节点作为服务运行 - 连接被拒绝

    我正在关注this https www digitalocean com community tutorials how to set up a node js application for production on ubuntu 16
  • 浏览器显示 clojure 环中不存在 access-control-allow-origin 标头

    我通过客户端浏览器向服务器发出请求 如下所示https example com bar https example com bar 但出现错误 Access to XMLHttpRequest at https example com ba

随机推荐