Laradock中Laravel Octane与WebSocket的nginx怎么配置
更新时间:2023-10-15
段落一:Laravel Octane的配置
首先,我们需要在Laradock的docker-compose.yml文件中添加以下内容来配置Laravel Octane:
# Laravel Octane octane: build: context: ./workspace-octane args: WWWGROUP: '${WWWGROUP}' container_name: laradock_workspace_octane restart: unless-stopped tty: true working_dir: /var/www volumes: - ../:/var/www/ - ./php-worker-octane.conf:/usr/local/etc/php-fpm.d/zz-php-worker-octane.conf - ./supervisord-octane.conf:/etc/supervisor/conf.d/supervisord-octane.conf environment: - "DB_DATABASE=${DB_DATABASE}" - "DB_USERNAME=${DB_USERNAME}" - "DB_PASSWORD=${DB_PASSWORD}" - "QUEUE_CONNECTION=redis"在此配置中,我们可以看到我们正在针对Laravel Octane构建一个docker容器,以及我们正在映射必要的配置文件和卷。 段落二:WebSocket配置 然后,我们需要更新Laradock项目中的Nginx配置,允许WebSocket连接与Laravel Octane中的协程进行通信。我们可以使用以下Nginx配置文件:
upstream laravel { server octane:8000; } # HTTP Server server { listen 80; listen [::]:80; server_name yourdomain.test; root /var/www/yourdomain/public; add_header X-Frame-Options "SAMEORIGIN"; add_header X-XSS-Protection "1; mode=block"; add_header X-Content-Type-Options "nosniff"; index index.php index.html index.htm; charset utf-8; location / { try_files $uri $uri/ /index.php?$query_string; proxy_read_timeout 300; proxy_connect_timeout 300; proxy_redirect off; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; } location /ws/ { proxy_pass http://laravel; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } location ~ \.php$ { fastcgi_pass php-fpm:9000; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.(?!well-known).* { deny all; } error_log /var/log/nginx/laravel_error.log; access_log /var/log/nginx/laravel_access.log; } # HTTPS Server server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name yourdomain.test; root /var/www/yourdomain/public; add_header X-Frame-Options "SAMEORIGIN"; add_header X-XSS-Protection "1; mode=block"; add_header X-Content-Type-Options "nosniff"; index index.php index.html index.htm; charset utf-8; ssl_certificate /etc/nginx/ssl/yourdomain.test.crt; ssl_certificate_key /etc/nginx/ssl/yourdomain.test.key; ssl_session_cache shared:SSL:10m; ssl_session_timeout 5m; ssl_ciphers 'TLS13+AESGCM+AES128:TLS13+AESGCM+AES256:TLS13+CHACHA20:TLS13+AES128+CCM:TLS13+AES128+CCM8:TLS13+AES256+CCM:TLS13+AES256+CCM8:ECDHE+AES128+SHA:ECDHE+RSA+AES128+SHA:ECDHE+AES256+SHA:ECDHE+RSA+AES256+SHA:ECDHE+RSA+3DES+EDE CBC:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4:!EDH:!AECDH:!ECDHE+RC4:!3DES;'; ssl_prefer_server_ciphers on; # OCSP Stapling ssl_stapling on; ssl_stapling_verify on; resolver 1.1.1.1 8.8.8.8; location / { try_files $uri $uri/ /index.php?$query_string; proxy_read_timeout 300; proxy_connect_timeout 300; proxy_redirect off; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; } location /ws/ { proxy_pass http://laravel; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } location ~ \.php$ { fastcgi_pass php-fpm:9000; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; include fastcgi_params; } error_log /var/log/nginx/laravel_error.log; access_log /var/log/nginx/laravel_access.log; }这将允许WebSocket连接到URI /ws/,并将它们代理到我们的Octane服务器。 在这个配置中,我们可以使用``proxy_set_header Upgrade $http_upgrade;``这个语句来启用升级标头,以便WebSocket协议可以进行通信。 段落三:PHP Worker配置 最后,我们将更新Laravel Octane中的PHP工作程序以便它支持WebSocket连接。
[User] user = www-data group = www-data [global] daemonize = no pidfile = /var/run/php-worker-octane.pid error_log = /var/www/logs/php_worker_error.log log_level = warning [child_1] command = php /var/www/artisan octane:work --server=RoadRunner --host=0.0.0.0 --port=8000 --workers=16 process_name = Octane numprocs = 1 autostart = true autorestart = true在此配置中,我们可以看到我们正在使用16个工作程序来处理WebSocket连接。 段落四:总结 通过更新docker-compose.yml和Nginx配置,以及更新PHP工作程序,我们可以成功配置Laravel Octane和WebSocket在Laradock中的使用。 希望这篇文章能够帮助你,祝你好运!