我测试得到的结果:
前端:
后端:
这里就是使用原生代码测试一下,我们安装的swoole是否可以访问。
9:配置nginx支持webscoket
ini
复制代码
# webscoket配置 map $http_upgrade $connection_upgrade { default upgrade; '' close; } upstream laravels { # Connect IP:Port # 此处的端口号要与laravel-s启动的端口号相同 server 0.0.0.0:5200 weight=5 max_fails=3 fail_timeout=30s; keepalive 16; } # upstream swoole { # # Connect IP:Port # server 0.0.0.0:5200 weight=5 max_fails=3 fail_timeout=30s; # # Connect UnixSocket Stream file, tips: put the socket file in the /dev/shm directory to get better performance # #server unix:/yourpath/laravel-s-test/storage/laravels.sock weight=5 max_fails=3 fail_timeout=30s; # #server 192.168.1.1:5200 weight=3 max_fails=3 fail_timeout=30s; # #server 192.168.1.2:5200 backup; # keepalive 16; # } server { listen 443; server_name xxx.xxx;#填写你的域名 index index.html index.htm index.php;#默认打开页面 root /xx/xx/xxx/xxx/xx/public;#你的index.php路径 # error_page 404 /index.html; autoindex off; #https配置 ssl on; ssl_certificate xxx/xxxx/xxxx_xxxx.xxx.pem; ssl_certificate_key xxx/xxxx/xxxx_xxxx.xxx; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; #开启gzip功能,加快网站打开速度 gzip on; #开启gzip静态压缩功能 gzip_static on; #gzip缓存大小 gzip_buffers 4 16k; #gzip http版本 gzip_http_version 1.1; #gzip 压缩级别 1-10 gzip_comp_level 2; #gzip 压缩类型 gzip_types text/plain application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;# 是否在http header中添加Vary: Accept-Encoding,建议开启gzip_vary on; proxy_read_timeout 60s; location ~ .php(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; } # location / # { # #如果是二级目录就用 rewrite ^/文件夹名称/(.*)$ /index.php?s=/$1 last;) # if (!-e $request_filename) # { # rewrite ^(.*)$ /index.php?s=/$1 last; # break; # } # } # Nginx 处理静态资源,LaravelS 处理动态资源 location / { index index.html; # vue 动态路由原始配置 # try_files $uri $uri/ /index.html; # laravels 结合vue动态路由配置 try_files $uri $uri @laravels/ /index.html; } # Http and WebSocket are concomitant, Nginx identifies them by "location" # !!! The location of WebSocket is "/ws" # Javascript: var ws = new WebSocket("ws://todo-s.test/ws"); # 处理 WebSocket 通信 location /websocket { proxy_connect_timeout 60s; proxy_send_timeout 60s; # proxy_read_timeout: Nginx will close the connection if the proxied server does not send data to Nginx in 60 seconds; At the same time, this close behavior is also affected by heartbeat setting of Swoole. proxy_read_timeout 60s; proxy_http_version 1.1; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-PORT $remote_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header Scheme $scheme; proxy_set_header Server-Protocol $server_protocol; proxy_set_header Server-Name $server_name; proxy_set_header Server-Addr $server_addr; proxy_set_header Server-Port $server_port; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_pass http://127.0.0.1:5200; #此处的端口号要与laravel-s启动的端口号相同 } # laravels 配置 location @laravels { # proxy_connect_timeout 60s; # proxy_send_timeout 60s; # proxy_read_timeout 60s; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-PORT $remote_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header Scheme $scheme; proxy_set_header Server-Protocol $server_protocol; proxy_set_header Server-Name $server_name; proxy_set_header Server-Addr $server_addr; proxy_set_header Server-Port $server_port; proxy_pass http://127.0.0.1:5200; #此处的端口号要与laravel-s启动的端口号相同 } }
将上边我使用xxx代替的部分改成你自己的信息。
特别说明一下:我这里使用了ssl证书,如果您没有相关的配置,把https那部分换掉就好。
具体的配置含义,请参考官方文档: