子域和CORS上的WebSockets

时间:2017-01-06 12:45:37

标签: node.js ssl nginx cors cross-domain

我正在尝试使用单个服务器部署Web应用程序。

  • 使用nginx作为http服务器
  • Nginx通过www。*(ssl over 443)提供静态网站
  • Nginx会将https请求重定向到api0。*子域到http://127.0.0.1:8082,其中带有WebSockets的NodeJS应用正在侦听
  • 通过http和https对example.com的任何子域的任何请求都将被重定向到https://www。*

这是我当前的nginx配置(在尝试了几个小时后):

    server {
        listen  80;
        server_name www.example.com;
        return  301 https://$server_name$request_uri;
    }

    server {
        listen  443 ssl;
        server_name www.example.com;
        add_header  Strict-Transport-Security "max-age=31536000";
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
        add_header Access-Control-Allow-Origin *;
        location / {
            root    /var/www/client;
        }
        location /cdn/ {
            proxy_pass  https://s3.eu-central-1.amazonaws.com/some_bucket;
        }
    }

    server {
        listen  433 ssl;
        server_name api0.example.com;
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
      ssl_certificate_key     /etc/letsencrypt/live/example.com/privkey.pem;
        add_header 'Access-Control-Allow-Origin' '*';
      add_header 'Access-Control-Request-Method' '*';
      add_header 'Access-Control-Allow-Methods' 'OPTIONS, GET';
      add_header 'Access-Control-Allow-Headers' '*';

        location / {
            proxy_pass  http://127.0.0.1:8082/;
        }
    }

我的静态网站正在向api0发出Socket.io请求。这失败了,Chrome说:

  

XMLHttpRequest无法加载https://api0.example.com/socket.io/?EIO=3&transport=polling&t=1483705935443-97。 No' Access-Control-Allow-Origin'标头出现在请求的资源上。起源' https://www.example.com'因此不允许访问。响应的HTTP状态代码为404.

我做错了什么?

0 个答案:

没有答案