我已经能够使用enable-cors.org中提供的代码为我的nginx服务器添加一个基本的CORS支持。 然而,这个解决scheme意味着复制和粘贴每个位置块的代码,我有几个位置,如下所示:
location /game1 { alias /development/games/game1/output; index index.html; } location /game2 { alias /development/games/game2/output; index index.html; }
有没有办法创build一个包含location块外的add_header的规则?
编辑澄清 :我已经尝试了以下
server { listen 80; server_name localhost; charset UTF-8; #access_log logs/host.access.log main; if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' '*'; ... } location /game1 { alias /development/games/game1/output; } ... }
但它不起作用:
2015/10/14 19:00:01 [emerg] 3464#7384: "add_header" directive is not allowed here in C:\development\servers\nginx-1.7.9/conf/nginx.conf:43
当然,你可以在server块下面添加add_header ,但是它会一直发送,这可能不是你想要的。
否则,您可以使用所需的指令创build一个文件,然后从要添加CORS头的每个location include它。