我已經能夠使用中提供的程式碼向我的 nginx 伺服器添加基本的 CORS 支持啟用cors.org。但是,此解決方案意味著將該程式碼複製並貼上到每個位置區塊中,並且我有幾個位置,如下所示:
location /game1 {
alias /development/games/game1/output;
index index.html;
}
location /game2 {
alias /development/games/game2/output;
index index.html;
}
add_header
有沒有辦法建立包含區塊外的規則location
?
編輯以澄清: 我嘗試過以下方法
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
答案1
當然,你可以在塊add_header
下添加server
,但它會總是被發送,這可能不是您想要的。
否則,您可以使用所需的指令建立一個文件,然後include
從location
您想要新增 CORS 標頭的每個位置建立該文件。