我的产品堆栈:
- Vue.js CLI - https://example.com
- Laravel API - https://example.com/api
我已经在我的本地主机上使用laravel-echo server和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;
}
}
我的main.js
文件,我连接到Web套接字:
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"
}
}
2条答案
按热度按时间6l7fqoea1#
尝试将此
host: 'https://example.com/socket.io'
更改为host: 'https://example.com'
agxfikkp2#
我在laravel-echo配置中保留了普通https的站点,但我删除了ssl规则,如证书等。而不是指向localhost,我指向了主机ip,只改变了端口:
在ngnix中添加:
在vue客户端中: