如何解决在请求的资源上不存在“ Access-Control-Allow-Origin”标头。为python socket.io服务器配置nginx时

时间:2019-05-03 08:03:16

标签: websocket socket.io

实际上,我正在尝试为仪表板应用程序设置WebSocket服务器,但问题是我无法正确配置Nginx。我正在使用flask-socket.io服务器来处理请求,并在localhost:8000 / namespace上运行。现在,我想为此使用DNS并托管此应用程序。

  

注意*我正在使用wss协议在前端将传输模式用作[“ polling”,“ websocket”,flashsocket“]。

如果我在0.0.0.0:8000上运行服务器并在http://server_ip:8000/namespace上打开连接,则它可能起作用,这可能是因为它不需要对Nginx做任何事情。 我试图将more_set_headers用于Access-Control-Allow-Origin。但是没有用 我尝试在上游使用它不起作用。

map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server <domain>:8010;
}
server {
    #listen 443 ssl default_server;
    listen [::]:443 ssl default_server;
    #listen 80;
    server_name <domain>;


    location / {
        proxy_pass http://websocket;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

location /socket.io/ {
        include proxy_params;
        more_set_headers 'Access-Control-Allow-Origin:*';
        more_set_headers 'Access-Control-Allow-Methods: GET,POST,OPTIONS';
        more_set_headers 'Access-Control-Allow-Credentials:true';
        more_set_headers 'Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_pass http://localhost:8000/<namespace>;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /ats/ {
        proxy_pass http://127.0.0.1:5000/;
    }
    ssl_certificate /etc/letsencrypt/live/<domain>/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/<domain>/privkey.pem; # managed by Certbot


}

连接应在wss:// domain_name / namespace上建立 和传输数据。

0 个答案:

没有答案