docs: 完善部署说明文档,补充 Octane 代理说明及 WebSocket 协议升级避坑指南
This commit is contained in:
@@ -186,52 +186,66 @@ php artisan view:cache
|
|||||||
|
|
||||||
### 6. Nginx 配置
|
### 6. Nginx 配置
|
||||||
|
|
||||||
在宝塔面板的 Nginx 配置中,需要添加 WebSocket 反向代理。
|
为了让外界能够通过 HTTPS 正常访问常驻内存服务器(Roadrunner 8000端口)以及实时 WebSocket 广播(Reverb 8080端口),需要在宝塔面板的 Nginx 配置中进行反向代理。
|
||||||
|
|
||||||
**第一步:** 在 `server {}` 块的**外面上方**添加:
|
在 `server {}` 块**内部**(`#REWRITE-END` 之后,禁止访问敏感文件之前)添加以下配置:
|
||||||
|
|
||||||
```nginx
|
```nginx
|
||||||
map $http_upgrade $connection_upgrade {
|
# ── 1. 静态资源优先由 Nginx 直接响应,动态请求分流给 Octane ──
|
||||||
default upgrade;
|
location / {
|
||||||
'' close;
|
try_files $uri $uri/ @octane;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# ── 2. API、心跳等动态请求反向代理到本地 8000 端口 (Octane / Roadrunner) ──
|
||||||
|
location @octane {
|
||||||
|
proxy_set_header Host $http_host;
|
||||||
|
proxy_set_header Scheme $scheme;
|
||||||
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
|
||||||
|
proxy_pass http://127.0.0.1:8000;
|
||||||
|
}
|
||||||
|
|
||||||
|
# ── 3. ⚡ WebSocket 实时广播反向代理 (Laravel Reverb 8080端口) ──
|
||||||
|
# 浏览器通过 /app 和 /apps 路径发起 WebSocket 连接
|
||||||
|
location /app {
|
||||||
|
proxy_pass http://127.0.0.1:8080;
|
||||||
|
proxy_http_version 1.1;
|
||||||
|
proxy_set_header Upgrade $http_upgrade;
|
||||||
|
|
||||||
|
# 避坑提示:某些宝塔环境全局未配置 $connection_upgrade 变量,
|
||||||
|
# 直接写死 "Upgrade" 能 100% 避免 500 代理握手失败。
|
||||||
|
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_set_header X-Forwarded-Proto $scheme;
|
||||||
|
|
||||||
|
# WebSocket 长连接保活:120秒无数据才断开
|
||||||
|
proxy_read_timeout 120s;
|
||||||
|
proxy_send_timeout 120s;
|
||||||
|
proxy_buffering off;
|
||||||
|
}
|
||||||
|
|
||||||
|
location /apps {
|
||||||
|
proxy_pass http://127.0.0.1:8080;
|
||||||
|
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_set_header X-Forwarded-Proto $scheme;
|
||||||
|
|
||||||
|
proxy_read_timeout 120s;
|
||||||
|
proxy_send_timeout 120s;
|
||||||
|
proxy_buffering off;
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**第二步:** 在 `server {}` 块**内部**(`#REWRITE-END` 之后)添加:
|
**测试并重载 Nginx:**
|
||||||
|
|
||||||
```nginx
|
|
||||||
location /app {
|
|
||||||
proxy_pass http://127.0.0.1:8080;
|
|
||||||
proxy_http_version 1.1;
|
|
||||||
proxy_set_header Upgrade $http_upgrade;
|
|
||||||
proxy_set_header Connection $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_set_header X-Forwarded-Proto $scheme;
|
|
||||||
proxy_read_timeout 120s;
|
|
||||||
proxy_send_timeout 120s;
|
|
||||||
proxy_buffering off;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /apps {
|
|
||||||
proxy_pass http://127.0.0.1:8080;
|
|
||||||
proxy_http_version 1.1;
|
|
||||||
proxy_set_header Upgrade $http_upgrade;
|
|
||||||
proxy_set_header Connection $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_set_header X-Forwarded-Proto $scheme;
|
|
||||||
proxy_read_timeout 120s;
|
|
||||||
proxy_send_timeout 120s;
|
|
||||||
proxy_buffering off;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
**第三步:** 在宝塔面板 → 网站 → 伪静态中选择 `laravel5`。
|
|
||||||
|
|
||||||
**第四步:** 测试并重载 Nginx:
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
nginx -t && systemctl reload nginx
|
nginx -t && systemctl reload nginx
|
||||||
@@ -241,7 +255,7 @@ nginx -t && systemctl reload nginx
|
|||||||
|
|
||||||
### 7. Supervisor 守护进程(关键!)
|
### 7. Supervisor 守护进程(关键!)
|
||||||
|
|
||||||
在宝塔面板 → 软件商店 → 安装「Supervisor管理器」,然后添加两个守护进程:
|
在宝塔面板 → 软件商店 → 安装「Supervisor管理器」,然后添加三个守护进程,确保长连接和心跳在后台持续运行:
|
||||||
|
|
||||||
#### 进程一:Laravel Reverb(WebSocket 服务器)
|
#### 进程一:Laravel Reverb(WebSocket 服务器)
|
||||||
|
|
||||||
@@ -250,6 +264,7 @@ nginx -t && systemctl reload nginx
|
|||||||
| 名称 | `chatroom-reverb` |
|
| 名称 | `chatroom-reverb` |
|
||||||
| 运行目录 | `/www/wwwroot/chat.ay.lc` |
|
| 运行目录 | `/www/wwwroot/chat.ay.lc` |
|
||||||
| 启动命令 | `php artisan reverb:start` |
|
| 启动命令 | `php artisan reverb:start` |
|
||||||
|
| 运行用户 | `www` |
|
||||||
| 进程数量 | `1` |
|
| 进程数量 | `1` |
|
||||||
|
|
||||||
#### 进程二:Laravel Horizon(队列处理器)
|
#### 进程二:Laravel Horizon(队列处理器)
|
||||||
@@ -259,12 +274,23 @@ nginx -t && systemctl reload nginx
|
|||||||
| 名称 | `chatroom-horizon` |
|
| 名称 | `chatroom-horizon` |
|
||||||
| 运行目录 | `/www/wwwroot/chat.ay.lc` |
|
| 运行目录 | `/www/wwwroot/chat.ay.lc` |
|
||||||
| 启动命令 | `php artisan horizon` |
|
| 启动命令 | `php artisan horizon` |
|
||||||
|
| 运行用户 | `www` |
|
||||||
| 进程数量 | `1` |
|
| 进程数量 | `1` |
|
||||||
|
|
||||||
|
#### 进程三:Laravel Octane(Roadrunner 常驻内存服务)
|
||||||
|
|
||||||
|
| 配置项 | 值 |
|
||||||
|
| -------- | ---------------------------------------------------------- |
|
||||||
|
| 名称 | `chatroom-octane` |
|
||||||
|
| 运行目录 | `/www/wwwroot/chat.ay.lc` |
|
||||||
|
| 启动命令 | `php artisan octane:start --server=roadrunner --port=8000` |
|
||||||
|
| 运行用户 | `www` |
|
||||||
|
| 进程数量 | `1` |
|
||||||
|
|
||||||
> ⚠️ **如果不配置 Supervisor:**
|
> ⚠️ **如果不配置 Supervisor:**
|
||||||
>
|
>
|
||||||
> - 关闭 SSH 终端后,Reverb 和 Horizon 会立刻停止
|
> - 关闭 SSH 终端后,Reverb、Horizon 和 Octane 会立刻停止运行,导致聊天室无法打开或无法收发消息!
|
||||||
> - 聊天室将无法发送/接收消息,在线列表为空
|
> - **更新代码后**:当您在服务器上完成 `git pull` 后,需要运行 `php artisan octane:reload` 重新载入内存,代码才会生效。
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
+23
-12
@@ -26,16 +26,23 @@ map $http_upgrade $connection_upgrade {
|
|||||||
# 建议放在 #REWRITE-END 之后,禁止访问敏感文件之前
|
# 建议放在 #REWRITE-END 之后,禁止访问敏感文件之前
|
||||||
# ═══════════════════════════════════════════════════════════
|
# ═══════════════════════════════════════════════════════════
|
||||||
|
|
||||||
# ── Laravel 伪静态规则 ──────────────────────────────────
|
# ── 1. 静态资源优先由 Nginx 直接响应,动态请求分流给 Octane ──
|
||||||
# 如果宝塔的伪静态配置文件 rewrite/chat.ay.lc.conf 为空,
|
location / {
|
||||||
# 请在宝塔面板 → 网站 → 伪静态 中选择 "laravel5",
|
try_files $uri $uri/ @octane;
|
||||||
# 或者直接在 rewrite/chat.ay.lc.conf 中写入以下内容:
|
}
|
||||||
#
|
|
||||||
# location / {
|
|
||||||
# try_files $uri $uri/ /index.php?$query_string;
|
|
||||||
# }
|
|
||||||
|
|
||||||
# ── Vite 静态资源缓存(文件名带 hash,可安全长期缓存)────────────
|
# ── 2. API、心跳等动态请求反向代理到本地 8000 端口 (Octane / Roadrunner) ──
|
||||||
|
location @octane {
|
||||||
|
proxy_set_header Host $http_host;
|
||||||
|
proxy_set_header Scheme $scheme;
|
||||||
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
|
||||||
|
proxy_pass http://127.0.0.1:8000;
|
||||||
|
}
|
||||||
|
|
||||||
|
# ── 3. Vite 静态资源缓存(文件名带 hash,可安全长期缓存)────────────
|
||||||
location ^~ /build/assets/ {
|
location ^~ /build/assets/ {
|
||||||
access_log off;
|
access_log off;
|
||||||
expires 1y;
|
expires 1y;
|
||||||
@@ -43,7 +50,7 @@ map $http_upgrade $connection_upgrade {
|
|||||||
try_files $uri =404;
|
try_files $uri =404;
|
||||||
}
|
}
|
||||||
|
|
||||||
# ── 常规静态资源缓存(不带 hash,保守缓存 7 天)───────────────
|
# ── 4. 常规静态资源缓存(不带 hash,保守缓存 7 天)───────────────
|
||||||
location ~* \.(?:css|js|mjs|png|jpg|jpeg|gif|webp|svg|ico|woff2?|ttf|eot)$ {
|
location ~* \.(?:css|js|mjs|png|jpg|jpeg|gif|webp|svg|ico|woff2?|ttf|eot)$ {
|
||||||
access_log off;
|
access_log off;
|
||||||
expires 7d;
|
expires 7d;
|
||||||
@@ -77,7 +84,11 @@ map $http_upgrade $connection_upgrade {
|
|||||||
proxy_pass http://127.0.0.1:8080;
|
proxy_pass http://127.0.0.1:8080;
|
||||||
proxy_http_version 1.1;
|
proxy_http_version 1.1;
|
||||||
proxy_set_header Upgrade $http_upgrade;
|
proxy_set_header Upgrade $http_upgrade;
|
||||||
proxy_set_header Connection $connection_upgrade;
|
|
||||||
|
# 避坑提示:某些宝塔环境全局未配置 $connection_upgrade 变量,
|
||||||
|
# 直接写死 "Upgrade" 能 100% 避免 500 代理握手失败。
|
||||||
|
proxy_set_header Connection "Upgrade";
|
||||||
|
|
||||||
proxy_set_header Host $host;
|
proxy_set_header Host $host;
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
@@ -95,7 +106,7 @@ map $http_upgrade $connection_upgrade {
|
|||||||
proxy_pass http://127.0.0.1:8080;
|
proxy_pass http://127.0.0.1:8080;
|
||||||
proxy_http_version 1.1;
|
proxy_http_version 1.1;
|
||||||
proxy_set_header Upgrade $http_upgrade;
|
proxy_set_header Upgrade $http_upgrade;
|
||||||
proxy_set_header Connection $connection_upgrade;
|
proxy_set_header Connection "Upgrade";
|
||||||
proxy_set_header Host $host;
|
proxy_set_header Host $host;
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
|||||||
Reference in New Issue
Block a user