【发布时间】: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