Laravel 回显服务器 socket.io 显示 404 被 CORS 阻止
Laravel echo server with socket.io show 404 blocked by CORS
我正在尝试将 laravel 回显服务器与 socket.io 结合用于我的聊天应用程序。但它显示了一个错误说
当我在我的本地设备上尝试时,服务器工作正常,它可以触发和监听。但是当我在 AWS EC2 服务上托管我的 vps 时,它显示了一些我上面提到的错误。我已经被困了2天了,已经不知道如何解决了。我正在使用 ubuntu 20.04、网络服务器 nginx、npm laravel-echo-server 1.6.2 和客户端版本 2.4 的 socket.io。我已经允许我在我的服务器上使用的端口
这是我的 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";
}
我正在尝试将 laravel 回显服务器与 socket.io 结合用于我的聊天应用程序。但它显示了一个错误说
当我在我的本地设备上尝试时,服务器工作正常,它可以触发和监听。但是当我在 AWS EC2 服务上托管我的 vps 时,它显示了一些我上面提到的错误。我已经被困了2天了,已经不知道如何解决了。我正在使用 ubuntu 20.04、网络服务器 nginx、npm laravel-echo-server 1.6.2 和客户端版本 2.4 的 socket.io。我已经允许我在我的服务器上使用的端口
这是我的 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";
}