我在 nginx 服务器上托管了一个 Angular 应用程序。我还需要配置 cors,因为我需要来自不同域的资源。但是当我按如下所示配置 cors 策略时,我的 nginx 服务器无法按应有的方式提供文件。
谁能告诉我我的问题出在哪里?非常感谢!
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';
#}
try_files $uri $uri/ /index.html?$args;
}
(我注释掉了该配置,因为我需要服务器正常工作。)