繁体   English   中英

Laravel 回显服务器与 socket.io 显示 404 被 CORS 阻止

[英]Laravel echo server with socket.io show 404 blocked by CORS

我正在尝试将 laravel 回显服务器与 socket.io 结合用于我的聊天应用程序。 但它显示一个错误说在此处输入图像描述

当我在我的本地设备上尝试时,服务器工作正常,它可以触发和监听。 但是当我在 AWS EC2 服务上托管我的 vps 时,它会显示一些错误,就像我上面提到的那样。 我已经卡了2天了,不知道如何解决它。 I'm using ubuntu 20.04, web server nginx, npm laravel-echo-server 1.6.2 and socket.io for client version 2.4. 我已经允许我在我的服务器上使用的端口

在此处输入图像描述

这是我的 laravel-echo-server.json

{
"authHost": "https://domain",
"authEndpoint": "/broadcasting/auth",
"clients": [],
"database": "redis",
"databaseConfig": {
    "redis": {
        "redis": {
            "host":"127.0.0.1",
            "port":"6379"
        }
    },
    "sqlite": {
        "databasePath": "/database/laravel-echo-server.sqlite"
    }
},
"devMode": true,
"host": null,
"port": "6002",
"protocol": "http",
"socketio": {},
"secureOptions": 67108864,
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"subscribers": {
    "http": false,
    "redis": true
},
"apiOriginAllow": {
    "allowCors": true,
    "allowOrigin": "https:domain, http://localhost:8080",
    "allowMethods": "GET, POST",
    "allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
}

}

这是我的服务器块配置

server {
server_name domain.com www.domain.com;
root /var/www/domain/public;

index index.html index.htm index.php;

location / {
  # try_files $uri $uri/ =404;
  try_files $uri $uri/ /index.php$is_args$args;
}

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
 }

location ~ /\.ht {
    deny all;
}


listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot}

server {
if ($host = www.domain.com) {
    return 301 https://$host$request_uri;
} # managed by Certbot


if ($host = domain.com) {
    return 301 https://$host$request_uri;
} # managed by Certbot

listen 80;
listen [::]:80;
server_name domain.com www.domain.com;
return 404; # managed by Certbot  

location /socket.io/ {
    proxy_pass http://localhost:6002/;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
}

这是我的客户代码

window.Echo = new Echo({
  broadcaster: "socket.io",
  host: "https://back-lms.gamifindo.com:6002",
});

任何帮助,将不胜感激。 先感谢您!

我终于找到了解决方案。 它一直在服务器块配置上,我将此代码移动到服务器支架标签的上部,然后它就可以工作了!

location /socket.io/ {
    proxy_pass http://localhost:6002/;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
}

暂无
暂无

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

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