要在 Laradock 环境中同时运行 Laravel Octane 和 WebSocket 服务,需要正确配置 Nginx 作为反向代理。以下是详细配置步骤:
找到 Laradock 中的 nginx/sites/default.conf
文件,修改为以下内容:
server {
listen 80;
listen [::]:80;
server_name your-domain.com;
root /var/www/public;
index index.php index.html index.htm;
# Laravel Octane 代理配置
location / {
try_files $uri $uri/ @octane;
}
location @octane {
set $suffix "";
if ($uri = /index.php) {
set $suffix ?$query_string;
}
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
proxy_set_header SERVER_PORT $server_port;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# 根据你使用的 Octane 驱动调整端口
# Swoole 默认 8000, RoadRunner 默认 8001
proxy_pass http://workspace:8000$suffix;
}
# WebSocket 代理配置
location /ws {
proxy_pass http://workspace:6001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 86400s; # 保持长连接
}
# 静态文件和 PHP-FPM 回退(可选)
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ /\.ht {
deny all;
}
}
确保你的 Laravel 项目已安装 Octane:
composer require laravel/octane
然后发布配置文件:
php artisan octane:install
在 .env
文件中配置 Octane:
OCTANE_SERVER=swoole # 或 roadrunner
OCTANE_PORT=8000
如果你使用 beyondcode/laravel-websockets
包:
composer require beyondcode/laravel-websockets
php artisan vendor:publish --provider="BeyondCode\LaravelWebSockets\WebSocketsServiceProvider" --tag="migrations"
php artisan migrate
在 .env
中配置:
LARAVEL_WEBSOCKETS_PORT=6001
在 Laradock 中启动相关服务:
确保 workspace
容器中运行 Octane:
php artisan octane:start --host=0.0.0.0 --port=8000
确保 workspace
容器中运行 WebSocket 服务:
php artisan websockets:serve --host=0.0.0.0 --port=6001
重启 Nginx 服务:
docker-compose restart nginx
如果需要 HTTPS 和安全的 WebSocket (WSS),修改 Nginx 配置:
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name your-domain.com;
ssl_certificate /etc/nginx/ssl/your-domain.com.crt;
ssl_certificate_key /etc/nginx/ssl/your-domain.com.key;
# ... 其他配置同上 ...
location /ws {
proxy_pass http://workspace:6001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 86400s;
}
}
server {
listen 80;
listen [::]:80;
server_name your-domain.com;
return 301 https://$host$request_uri;
}
如果你运行多个 Octane 实例,可以在 Nginx 中配置负载均衡:
upstream octane_servers {
server workspace:8000;
server workspace:8001;
server workspace:8002;
# 添加更多实例...
}
server {
# ... 其他配置 ...
location @octane {
# ... 其他 proxy 设置 ...
proxy_pass http://octane_servers$suffix;
}
}
WebSocket 连接失败:
Upgrade
和 Connection
头502 Bad Gateway:
workspace
容器名称是否正确(Laradock 默认使用 workspace
)性能问题:
php artisan octane:start --workers=8
通过以上配置,你应该能够在 Laradock 环境中同时运行 Laravel Octane 和 WebSocket 服务,并通过 Nginx 正确代理所有请求。