简体   繁体   English

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

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

i'm trying to utilize laravel echo server combined with socket.io for my chat apps.我正在尝试将 laravel 回显服务器与 socket.io 结合用于我的聊天应用程序。 But it shows an error that said但它显示一个错误说在此处输入图像描述

When i'm trying at my local device, the server is working fine, it can trigger and listen.当我在我的本地设备上尝试时,服务器工作正常,它可以触发和监听。 But when i host on my vps at AWS EC2 service, it shows some error like i mentioned above.但是当我在 AWS EC2 服务上托管我的 vps 时,它会显示一些错误,就像我上面提到的那样。 I've been stuck for 2 days already not knowing how to solve it.我已经卡了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. I'm using ubuntu 20.04, web server nginx, npm laravel-echo-server 1.6.2 and socket.io for client version 2.4. i already allow port that i'm using on my server我已经允许我在我的服务器上使用的端口

在此处输入图像描述

Here is my laravel-echo-server.json这是我的 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"
}

} }

And here is my server block configuration这是我的服务器块配置

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";
}

Here is my client code这是我的客户代码

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

Any help would be appreciated.任何帮助,将不胜感激。 Thank you in advance!先感谢您!

I finally found the solution.我终于找到了解决方案。 Its on server block configuration all along, I moved this code to the upper of my server bracket tag, then it just works!它一直在服务器块配置上,我将此代码移动到服务器支架标签的上部,然后它就可以工作了!

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