在 nginx 上设置 CORS 标头

在 nginx 上设置 CORS 标头

从浏览器 URL 栏尝试时,地址类似http://vpsxxx.ovh.net/g/search?q=ok或代理良好http://vpsxxx.ovh.net/so/questions/34738694/how-to-move-items-on-the-screen-for-a-game-using-javascript

但不是来自 JS,即使添加了 CORS 标头之后,示例:http://jsfiddle.net/crl/svmw93a8/1/http://jsfiddle.net/crl/svmw93a8/2/

甚至向根发出请求 / like: 也ajax('http://vpsxxx.ovh.net').then(function(r){ console.log(r) });不起作用

这是配置文件:

server {
  listen 80;
  server_name vpsxxx.ovh.net;  # some ovh server

  location /g/ {
    proxy_pass http://www.google.com/;
  }

  location /so/ {

    proxy_pass http://stackoverflow.com/;
  }

  location / {  # I tried putting it before other locations too

    proxy_pass http://www.google.com/; # added here also, for tests, tried putting it just after headers too

    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        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') { # let's add them for all methods
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
    # }

  }

}

相关内容