繁体   English   中英

使用Nginx的socket.io出现CORS错误

[英]CORS error with socket.io using Nginx

在Ubuntu中使用Nginx代理会出现CORS的套接字连接错误,而Http请求可以正常使用。

这是我的Nginx配置文件。 此配置可在Windows和Mac上正常运行,但不适用于Ubuntu。

server 
{
    listen 7000 default_server;
    listen [::]:7000 default_server;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name localhost;
    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
             proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_http_version 1.1;
            proxy_set_header X-Forwarded-For 
            $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_pass http://nodes;
    }
}

upstream nodes {
    # enable sticky session based on IP
    ip_hash;

    server localhost:3000;
    server localhost:3001;
    server localhost:3003;
}
server{
        listen 7000;
        server_name www.whatever.com;
        location / {
                add_header 'Access-Control-Allow-Origin' 'http://localhost:7000' always;
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
                add_header 'Access-Control-Allow-Headers' 'X-Requested-With,Accept,Content-Type, Origin,x-auth' always;
                default_type application/json;
                proxy_pass //node;
                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;





    }


}

可能会有所帮助,并用适当的地址代替该地址。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM