【问题标题】:NGINX CORS IssuesNGINX CORS 问题
【发布时间】:2020-07-07 09:21:29
【问题描述】:

有一台NGINX 1.16的服务器,用于通过AJAX上传文件

这里是NGINX文件的配置设置

location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
if ($request_method = "OPTIONS") {
    add_header Access-Control-Allow-Origin 'http://myserver.com';
    add_header Access-Control-Allow-Credentials false;
    add_header Access-Control-Allow-Methods 'DELETE,GET,OPTIONS,POST,PUT';
    add_header Access-Control-Allow-Headers 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With,X-Token-Auth,X-Mx-ReqToken,X-Requested-With';
    add_header 'Access-Control-Max-Age' 1728000;
    add_header 'Content-Type' 'text/plain charset=UTF-8';
    add_header 'Content-Length' 0;

    return 204;
}

add_header Access-Control-Allow-Origin 'http://myserver.com';
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Methods 'DELETE,GET,OPTIONS,POST,PUT';
add_header Access-Control-Allow-Headers 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With,X-Token-Auth,X-Mx-ReqToken,X-Requested-With';
}

这是 CURL 的测试结果

当您尝试上传文件时,控制台中会显示 CORS 错误

也许有人在服务器上开启СORS时遇到了类似的行为,但还是弹出错误?

UPD:还注意到浏览器中不显示标题。有一种假设是这个原因,但是如何“强制”服务器给出标头呢?

【问题讨论】:

  • 响应的 HTTP 状态码是什么?您可以使用浏览器开发工具中的网络窗格进行检查。是 4xx 还是 5xx 错误而不是 200 OK 成功响应?
  • 代码不发出,用红色“(失败)”写成
  • 尝试禁用所有扩展,和/或尝试在隐身窗口中。并尝试清除该站点的 cookie 和浏览器缓存。并禁用您已安装的任何防病毒软件。并从不同的浏览器、不同的机器和不同的网络尝试。关键是要消除一些其他正在运行的软件或您的机器可能会干扰请求的可能性,并消除某些防火墙设置干扰它的可能性,等等。
  • @sideshowbarker,我们已经不止一次这样做了——结果为零。最有意思的是服务器不给浏览器headers

标签: ajax nginx cors


【解决方案1】:

add_header Access-Control-Allow-Origin 'http://myserver.com';你的设置有问题。

   location / {
         if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            #
            # Custom headers and headers various browsers *should* be OK with but aren't
            #
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
            #
            # Tell client that this pre-flight info is valid for 20 days
            #
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain; charset=utf-8';
            add_header 'Content-Length' 0;
            return 204;
         }
         if ($request_method = 'POST') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
            add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
         }
         if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
            add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
         }
    }

【讨论】:

  • 我们尝试了这个配置选项,这并没有解决问题
  • 您是否在 php 控制器端添加了标头,您尝试使用哪个 api?
  • 在服务器端试试这个。这是工作环境。它应该工作芽
  • 也许这将是我们将使用的最后一个选项。必须尝试解决问题,而不是绕过它))
  • ourcodeworld.com/articles/read/291/… 可能会对您有所帮助。
猜你喜欢
  • 2018-08-06
  • 2020-09-15
  • 1970-01-01
  • 2019-05-17
  • 2021-04-10
  • 2019-02-03
  • 2016-12-08
相关资源
最近更新 更多