enabe CORS构成nginx中的任何站点/本地主机

时间:2016-11-13 09:37:57

标签: nginx

我已为节点配置nginx,并成功运行简单的快速应用程序

var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.json({
    "name":"Rajesh"
  })
})

app.listen(8010, function () {
  console.log('Example app listening on port 8010!')
})

使用邮递员我可以在vpsip:8010上看到请求成为json字符串。

但是当我使用html + jquery ajax code ::

$.ajax({
      method: "GET",
      url: "http://vpsip:8010/"
    })
      .done(function( msg ) {
        alert( "Data Saved: " + msg );
    });

我收到错误

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://anything' is therefore not allowed.

我的nginx默认文件是

location /exapi {
    proxy_pass http://localhost:8010/exapi;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;

    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        #
        # Om nom nom cookies
        #
        add_header 'Access-Control-Allow-Credentials' 'true';
        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,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        #
        # 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-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';
     }
     if ($request_method = 'GET') {
        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';
     }

}

任何人都可以告诉我如何在我的nginx(在我的vps中)启用CORS请求,以便我可以创建我的快速apis。

1 个答案:

答案 0 :(得分:1)

此API服务(http://vps264757.ovh.net/GameFactoryService/resources/maze?w=50&h=50 / http://vps264757.ovh.net/maze.html)之前遇到同样的问题。这里我的默认网站启用正常,代理到localhost / 18000上的glassfish。希望它有所帮助:

    #Glassfish
    location /GameFactoryService/   {

            index index.html;

            add_header Access-Control-Allow-Origin $http_origin;

            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-NginX-Proxy true;
            proxy_ssl_session_reuse off;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://127.0.0.1:18000/GameFactoryService/;

    }

    #static content
    location / {
            root /usr/share/nginx_static_content;
    }

    error_page 500 501 502 503 504 505 506 507 508 509 510 511 /50x.html;

    #error
    location = /50x.html {

      add_header Access-Control-Allow-Origin $http_origin;          
      internal;          
    }