Nginx:我从localhost获得Cross-Origin错误

时间:2017-02-02 18:57:10

标签: node.js ssl nginx socket.io cors

我正在尝试使用http从我的localhost到达在AWS上托管的这个nginx服务器。

Nginx(见下文)被配置为反向代理,将请求发送到在同一实例上运行的NodeJS服务器。但是,我从socket.io收到这些错误消息。 localhost是否需要在https下?

  

XMLHttpRequest无法加载   https://sub.domain.com/api/socket/?EIO=3&transport=polling&t=Le0AoIj。   请求中不存在“Access-Control-Allow-Origin”标头   资源。因此不允许来源“http://localhost:8888”   访问。响应具有HTTP状态代码502.

Nginx服务器块:

server {
    listen                  443 ssl;
    server_name             sub.domain.com;

    ssl_certificate         /etc/letsencrypt/live/sub.domain.com/fullchain.pem;
    ssl_certificate_key     /etc/letsencrypt/live/sub.domain.com/privkey.pem;

    ssl_protocols           TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers             HIGH:!aNULL:!MD5;

    location / {
            root            /usr/share/nginx/html;
            index           index.html index.htm;
    }

    location /api {
            add_header              'Access-Control-Allow-Origin' '*';
            add_header              'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, PATCH, DELETE';
            add_header              'Access-Control-Allow-Credentials' 'true';
            add_header              'Access-Control-Allow-Headers' 'X-Requested-With, content-type';
            proxy_redirect          off;
            proxy_pass              http://localhost:3000;
            proxy_http_version      1.1;
            proxy_set_header        Upgrade $http_upgrade;
            proxy_set_header        Connection "upgrade";

            proxy_set_header        Host $http_host;
            proxy_set_header        X-Scheme $scheme;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        Access-Control-Allow-Origin $http_origin;
            proxy_cache_bypass      $http_upgrade;
            proxy_hide_header       X-Powered-By;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }

}

0 个答案:

没有答案