我已经设置了一个 NginX,以便从实例中提供一些静态文件。
静态文件将由我拥有的 3 个不同域使用。
NginX 服务器位于其自己的(第 4 个)域中。我想限制对我的文件的访问并应用 CORS 策略。
我已经研究过如何实现这一点,并且我确实做到了。在我的位置块中,我测试了以下代码:
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' 'http://localhost:3000';
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 = 'GET') {
add_header 'Access-Control-Allow-Origin' 'http://localhost:3000';
add_header 'Access-Control-Allow-Methods' …Run Code Online (Sandbox Code Playgroud)