96 lines
5.6 KiB
Markdown
96 lines
5.6 KiB
Markdown
# Nginx 配置(新服务器无 NPM 时使用)
|
||
|
||
域名:**yuheng.yuxindazhineng.com**,强制 HTTPS,SSL 证书按域名单独存放。
|
||
|
||
## 1. 证书目录(按域名命名)
|
||
|
||
在服务器上创建专门存放 SSL 的目录,以域名为子目录名:
|
||
|
||
```bash
|
||
sudo mkdir -p /etc/ssl/yh_web/yuheng.yuxindazhineng.com
|
||
```
|
||
|
||
将证书文件放入该目录(Let's Encrypt 或自有证书均可):
|
||
|
||
- **fullchain.pem** — 证书链(或你的 `fullchain.crt`,需在配置里改扩展名)
|
||
- **privkey.pem** — 私钥(或你的 `privkey.key`)
|
||
|
||
**一键脚本自动同步**:也可把证书放在项目 **`nginx/`** 下,运行 `./pull-and-restart.sh` 或 `./restart.sh` 会自动复制到系统目录。支持两种命名方式:
|
||
- **`nginx/yuheng.yuxindazhineng.com.pem`** + **`nginx/yuheng.yuxindazhineng.com.key`**(按域名命名)
|
||
- **`nginx/fullchain.pem`** + **`nginx/privkey.pem`**
|
||
|
||
示例(若用 certbot):
|
||
|
||
```bash
|
||
# certbot 默认路径,可复制到统一目录或做软链接
|
||
sudo cp /etc/letsencrypt/live/yuheng.yuxindazhineng.com/fullchain.pem /etc/ssl/yh_web/yuheng.yuxindazhineng.com/
|
||
sudo cp /etc/letsencrypt/live/yuheng.yuxindazhineng.com/privkey.pem /etc/ssl/yh_web/yuheng.yuxindazhineng.com/
|
||
sudo chown -R root:root /etc/ssl/yh_web/yuheng.yuxindazhineng.com
|
||
sudo chmod 600 /etc/ssl/yh_web/yuheng.yuxindazhineng.com/privkey.pem
|
||
```
|
||
|
||
## 2. 部署 Nginx 配置
|
||
|
||
```bash
|
||
# 复制项目内配置到 Nginx 配置目录(按实际路径调整)
|
||
sudo cp /www/yh_web/nginx/yuheng.yuxindazhineng.com.conf /etc/nginx/conf.d/
|
||
|
||
# 检查配置
|
||
sudo nginx -t
|
||
|
||
# 重载
|
||
sudo systemctl reload nginx
|
||
```
|
||
|
||
若 Nginx 使用其他路径(如 `sites-enabled`),请把上述 conf 放到对应目录并 `include` 到主配置。
|
||
|
||
## 3. 两种部署方式(二选一)
|
||
|
||
**方式 A:仅 compose 占 443(默认)**
|
||
- `docker-compose.yml` 中 nginx 映射 `443:443`,请求直接进 compose 内 Nginx,再反代到 api/web/admin。
|
||
- 宿主机**不要**为本站点单独起 Nginx(不要用本目录的 `yuheng.yuxindazhineng.com.conf` 占 443),否则会与 compose 抢 443 或反代到已废弃的 9528/9529/8088,导致 /api/、/admin/ 404。
|
||
|
||
**方式 B:宿主机 Nginx 占 443,反代到 compose**
|
||
- 若宿主机已有 Nginx 监听 443(多站点),则把 compose 中 nginx 端口改为 **8443:443**,宿主机用本目录的 `yuheng.yuxindazhineng.com.conf`(已配置为整站反代到 `127.0.0.1:8443`)。
|
||
- 复制 conf 到 `/etc/nginx/conf.d/` 后 `nginx -t && systemctl reload nginx`。
|
||
|
||
**/api/health 或 /admin/ 返回 404 时**:在服务器执行 `ss -tlnp | grep 443`,看 443 是宿主机 nginx 还是 docker。若是宿主机 nginx,要么停用该站点配置让 compose 独占 443(方式 A),要么改为方式 B(compose 用 8443,宿主机反代到 8443)。
|
||
|
||
**验证文件热加载**:把验证文件放到项目根目录的 `verify-root/` 即可;compose 内 **`yh_nginx`** 挂载该目录并在 **443** 上直接 `root /verify-root` 提供(见 `nginx/yuheng.docker.conf.tpl`)。`reload` 后生效;若仅改文件,可 `docker compose restart nginx`。
|
||
|
||
## 4. 新服务器首次安装 Nginx
|
||
|
||
```bash
|
||
# CentOS / RHEL / 阿里云
|
||
sudo dnf install -y nginx
|
||
# 或
|
||
sudo yum install -y nginx
|
||
|
||
# 开机自启并启动
|
||
sudo systemctl enable nginx
|
||
sudo systemctl start nginx
|
||
```
|
||
|
||
然后再按上面步骤创建证书目录、放入证书、复制 conf 并重载。
|
||
|
||
## 5. 前台动态路由与 404(SPA)
|
||
|
||
- **现象**:浏览器直接打开 `https://你的域名/some-page` 出现 **nginx** 的 `404 Not Found`(页脚带 `nginx/x.x.x`),而不是网站自己的页面。
|
||
- **原因**:提供静态文件的 `server` 未把「不存在的路径」交给 `index.html`,Nginx 在磁盘上找不到 `some-page` 文件就返回 404。
|
||
- **要求**:托管 **web 前台** 的站点必须使用 **`try_files $uri $uri/ /index.html;`**(见仓库 `nginx/web.conf` 与 `web/Dockerfile` 内嵌配置)。若你自建 Nginx,请对照修改后再 `nginx -t` 并重载。
|
||
- **应用内 404**:在 SPA 已正确回退的前提下,未在后台发布的路径会由前端路由进入 **「页面不存在」** 页(`NotFound.vue`),与上述 nginx 404 不同。
|
||
- **Compose 部署**:`web` 容器实际加载的是 **`deploy/web/default.conf`**(见 `docker-compose.yml` 挂载)。若线上仍对 `/test` 等返回 **nginx 404**,请把仓库里最新的 `deploy/web/default.conf` 同步到服务器对应路径后,执行 `docker compose restart web`(或重建 `yh_web` 容器)。
|
||
|
||
## 6. 单实例:宿主机 Nginx 占 443(与 `pull-and-restart.sh` / `restart.sh` 自动切换)
|
||
|
||
逻辑由 **`scripts/lib-yh-compose-deploy.sh`** 统一处理(无需单独启动脚本):
|
||
|
||
1. **启动前**:`docker compose … down --remove-orphans`,只停本项目容器,**不**对宿主机 `nginx` 做 `start/stop`。
|
||
2. **`systemctl` 显示宿主机 Nginx 已运行**:只起 `mongo api web admin`,**不起**容器 `yh_nginx`;从 **`nginx/yuheng.host.conf`** 生成 `/etc/nginx/conf.d/<域名>.conf` 并 **`nginx -t` + `reload`**(不重载则无法指向新回环端口)。
|
||
3. **宿主机 Nginx 未运行**:起完整栈(**`--profile compose-internal-nginx`**,含容器 `yh_nginx` 终止 TLS)。
|
||
4. **证书**:同上,`/etc/ssl/yh_web/yuheng.yuxindazhineng.com/`;`pull-and-restart.sh` / `restart.sh` 仍会同步仓库内证书到该目录。
|
||
|
||
**回环端口**(与 `nginx/yuheng.host.conf` 一致):API `127.0.0.1:8088`,前台 `9080`,后台 `9081`。
|
||
|
||
**与「方式 B(8443)」**:方式 B 是宿主机 → 容器 Nginx → 各服务;本节是宿主机 → 各服务,**一层**。
|