Nginx + HapiJS CORS请求(不是GET)

时间:2016-08-04 20:42:51

标签: nginx cors hapijs

我有一个带有hapiJS API的节点服务器,我正在尝试从单独的节点服务器发送请求。

开发过程中一切正常,但服务器上没有,问题是服务器上启用了基本身份验证。

具体来说,如果请求是GET请求,请求可以正常工作,但所有其他请求都会失败。我怀疑这是由于OPTIONS请求前请求航班检查失败,而我的有限理解不会发送GET请求。

我得到的确切错误信息是:

请求的资源上没有“Access-Control-Allow-Origin”标头。因此,不允许原点“https://site.example.com”访问。响应的HTTP状态代码为401。

我的nginx配置:

server {
  listen 80;
  server_name https://api.example.com;
  return 301 https://$server_name$request_uri; ## Permanently redirect all http traffic to https
}
server {

  #SSL INIT
  listen  443;
  ssl    on;
  ssl_certificate    /var/vcap/jobs/nginx/config/site.bundle.crt;
  ssl_certificate_key    /var/vcap/jobs/nginx/config/site.key;

  server_name https://api.example.com;

  location / {
    auth_basic "Restricted Content";
    auth_basic_user_file .htpasswd;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass_request_headers on;
    proxy_pass https://api.example.com;
  }

}

我对HapiJS的CORS设置(我排除了无关的东西)。

connections:
  routes:
    cors:
      origin: ["https://api.example.com"]
      credentials: true

我已经尝试了https://www.npmjs.com/package/hapi-cors-headers hapi-cors-headers插件,但没有用(而不是上面的设置。)我尝试在nginx上启用我能想到的每一个CORS事物(大多数来自{ {3}})

无论我如何调整配置,都会发生以下两件事之一 - 我尝试了很多东西: 1)无论如何,它继续给出上述信息 2)它抱怨标题是TWICE(如果我同时把它放在nginx和hapijs中)。

在任何情况下都不起作用(GET请求除外)。

我正在使用的API的ajax调用示例(与Kendo一起使用):

$.ajax({
    url: api_address + '/vendors',
    method: 'POST',
    contentType: 'application/json',
    processData: false,
    data: JSON.stringify(this.vendor),
    xhrFields: {
      withCredentials: true
    }
    success: function(data) {

        vm.set('vendor', data);
        notification.show('Vendor created successfully', 'success');
        vm.set('has_changes', false);
    },
    error: function() {
        notification.show('Error creating vendor', 'error');
    }
});

上面提到的api_address是:

https://username:password@api.example.com

为什么这适用于GET请求但不适用于POST / PUT /等?

1 个答案:

答案 0 :(得分:0)

更改您的nginx以处理CORS并且不会担心hapi担心它。摘自http://enable-cors.org/server_nginx.html

location / {
    add_header 'Access-Control-Allow-Origin' '*';
    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 = 'OPTIONS') {
        #
        # 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;
    }
    auth_basic "Restricted Content";
    auth_basic_user_file .htpasswd;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass_request_headers on;
    proxy_pass https://api.example.com;
}

当然,如果您支持PUT或DELETE,则需要更多工作。