我的刺。堆叠:
我已经在本地主机上使用laravel-echo服务器& socket.io创建了一个实时聊天。
但是,我想在我的生产服务器上运行它。为此,我使用了nginx。
问题是,当我运行laravel-echo-server start 时,它成功地启动了,但是当我从web浏览器访问我的站点时,它没有给出响应。(无用加入)
我已经重建了vue应用程序,清除了laravel缓存,并重新启动了nginx服务。
在/etc/nginx/sites-enabled/default,我有:
server {
server_name example.com www.example.com;
root /var/www/html/Example/api/public/;
index index.html index.php;
location / {
root /var/www/html/Example/web/dist/;
try_files $uri $uri/ /index.html;
}
location /socket.io/ {
proxy_pass http://localhost:6001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
location /api {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.0-fpm.sock;
}
}
连接到web套接字的main.js文件:
import Echo from "laravel-echo"
window.io = require('socket.io-client')
window.Echo = new Echo({
broadcaster: 'socket.io',
host: 'https://example.com/socket.io',
auth: {
headers: {
'Authorization': `Bearer ${localStorage.getItem('token')}`
}
}
});
我的laravel-echo.server.json
{
"authHost": "https://example.com/api",
"authEndpoint": "/broadcasting/auth",
"clients": [
{
appId: "xxxxx",
key: "xxxxx"
}
],
"database": "redis",
"databaseConfig": {
"redis": {},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": false,
"host": null,
"port": "6001",
"protocol": "http",
"socketio": {},
"secureOptions": 67108864,
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"subscribers": {
"http": true,
"redis": true
},
"apiOriginAllow": {
"allowCors": true,
"allowOrigin": "*",
"allowMethods": "GET, POST",
"allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
}
}发布于 2021-04-25 10:24:39
尝试将此host: 'https://example.com/socket.io'更改为host: 'https://example.com'
发布于 2022-05-07 05:22:19
我在laravel-echo配置中保留了带有普通https的站点,但是我删除了ssl规则,如证书等.我没有指向本地主机,而是指向主机ip,只更改端口:
{
"authHost": "https://example.com/api",
"authEndpoint": "/broadcasting/auth",
"clients": [
{
appId: "xxxxx",
key: "xxxxx"
}
],
"database": "redis",
"databaseConfig": {
"redis": {
"port": "6379",
"host": "127.0.0.1"
},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": false,
"host": 127.0.0.1,
"port": "6001",
"protocol": "http",
"socketio": {},
"secureOptions": 67108864,
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"subscribers": {
"http": true,
"redis": true
},
"apiOriginAllow": {
"allowCors": true,
"allowOrigin": "*",
"allowMethods": "GET, POST",
"allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
}
}加上ngnix:
location /socket.io {
proxy_pass http://127.0.0.1:6001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}在vue客户端:
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.origin, // sem a porta
auth: {
headers: {
Authorization: 'Bearer ' + bearerToken,
}
}
});https://stackoverflow.com/questions/67179049
复制相似问题