【问题标题】:Laravel echo server with socket.io show 404 blocked by CORS带有 socket.io 的 Laravel 回显服务器显示 404 被 CORS 阻止
【发布时间】:2022-01-04 15:16:07
【问题描述】:

我正在尝试将 laravel echo 服务器与 socket.io 结合用于我的聊天应用程序。但它显示一个错误说

当我在本地设备上尝试时,服务器工作正常,它可以触发和侦听。但是当我在 AWS EC2 服务上托管我的 vps 时,它会显示一些错误,就像我上面提到的那样。我已经卡了2天了,不知道如何解决它。我正在使用 ubuntu 20.04、Web 服务器 nginx、npm laravel-echo-server 1.6.2 和 socket.io 用于客户端版本 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",
});

任何帮助将不胜感激。提前谢谢!

【问题讨论】:

    标签: node.js laravel npm socket.io laravel-echo


    【解决方案1】:

    我终于找到了解决办法!它一直在服务器块配置上。我把这段代码移到了我的服务器括号标签的上面,然后它就可以工作了!

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-17
      • 2020-06-15
      • 2020-09-18
      • 1970-01-01
      相关资源
      最近更新 更多