多项目共存部署方案 – WordPress博客与餐厅预订系统(同一接口不同域名)

问题背景

  • 服务器上已有 WordPress 博客运行在 80/443 端口
  • 需要部署餐厅预订系统到同一服务器
  • 要求:两个系统独立访问,使用标准端口(无需端口号)

解决方案:同 IP 同端口 + 不同域名

核心原理

通过 Nginx 的 server_name 指令,根据 HTTP 请求头的 Host 字段区分不同网站,实现一个 IP、一个端口服务多个域名。

架构图

服务器 IP: YOUR_SERVER_IP
    │
    ├── 端口 80/443
    │   ├── 域名: www.example.com → WordPress 博客
    │   └── 域名: restaurant.example.com → 餐厅系统
    │
    └── 端口 8080 (内部)
        └── Spring Boot 后端 API

配置步骤

步骤1:DNS 解析配置

在域名管理后台添加 A 记录:

主机记录:restaurant
记录类型:A
记录值:YOUR_SERVER_IP
TTL:600

结果: restaurant.example.com → YOUR_SERVER_IP

步骤2:Nginx 配置

一键配置命令(直接在服务器执行):

bash

# 备份原配置
cp /www/server/panel/vhost/nginx/restaurant.conf /www/server/panel/vhost/nginx/restaurant.conf.bak

# 写入新配置
cat > /www/server/panel/vhost/nginx/restaurant.conf << 'EOF'
server {
    listen 80;
    server_name restaurant.example.com;

    root /home/restaurant-reservation/frontend/dist;
    index index.html;

    location / {
        try_files $uri $uri/ /index.html;
    }

    location /api/ {
        proxy_pass http://127.0.0.1:8080/api/;
        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;
    }

    access_log  /www/wwwlogs/restaurant.log;
    error_log   /www/wwwlogs/restaurant.error.log;
}
EOF

# 测试配置
nginx -t

# 重启 Nginx
/etc/init.d/nginx restart

步骤3:验证配置

bash

# DNS 解析验证
nslookup restaurant.example.com

# HTTP 访问验证
curl -I http://restaurant.example.com

# 端口监听验证
netstat -tlnp | grep :80

关键配置对比

博客配置(/www/server/panel/vhost/nginx/blog.conf)

nginx

server {
    listen 80;
    listen 443 ssl;
    server_name www.example.com;  # 博客域名
    root /www/wwwroot/wordpress;
    # ... 其他配置
}

餐厅系统配置(/www/server/panel/vhost/nginx/restaurant.conf)

nginx

server {
    listen 80;  # 同一个 80 端口
    server_name restaurant.example.com;  # 餐厅域名
    root /home/restaurant-reservation/frontend/dist;
    # ... 其他配置
}

关键点

  • ✅ 两个配置都监听 80 端口(不冲突)
  • ✅ 通过 server_name 区分不同网站
  • ✅ Nginx 自动根据访问域名匹配对应配置

工作原理

用户访问博客时

1. 输入: www.example.com
2. DNS: www.example.com → YOUR_SERVER_IP
3. 请求: YOUR_SERVER_IP:80 (Host: www.example.com)
4. Nginx: 匹配 server_name www.example.com
5. 返回: WordPress 博客内容

用户访问餐厅系统时

1. 输入: restaurant.example.com
2. DNS: restaurant.example.com → YOUR_SERVER_IP (同一个IP)
3. 请求: YOUR_SERVER_IP:80 (Host: restaurant.example.com)
4. Nginx: 匹配 server_name restaurant.example.com
5. 返回: 餐厅系统内容

最终访问地址

  • WordPress 博客https://www.example.com
  • 餐厅预订系统http://restaurant.example.com

方案优势

  1. 零成本:无需购买新服务器,无需额外域名(使用子域名)
  2. 标准化:使用标准 80/443 端口,无需端口号
  3. 易维护:两个系统独立配置,互不影响
  4. 可扩展:可继续添加更多子域名项目
  5. 专业性:符合标准 Web 访问规范

可选优化

配置 HTTPS(推荐)

使用 certbot 自动配置

bash

certbot --nginx -d restaurant.example.com

或使用宝塔面板

网站 → restaurant.example.com → SSL → Let's Encrypt → 申请

配置后访问: https://restaurant.example.com

清理旧配置(可选)

关闭不再使用的 8081 端口:

bash

firewall-cmd --permanent --remove-port=8081/tcp
firewall-cmd --reload

排查清单

DNS 检查

bash

nslookup restaurant.example.com
# 应返回: YOUR_SERVER_IP

Nginx 检查

bash

nginx -t  # 配置语法检查
netstat -tlnp | grep :80  # 端口监听检查

访问检查

bash

curl -I http://restaurant.example.com
# 应返回: HTTP/1.1 200 OK

浏览器检查

  • ✅ 访问 http://restaurant.example.com 显示登录页面
  • ✅ F12 Console 无错误
  • ✅ Network 标签资源加载正常

技术要点总结

  1. 同 IP 多域名:一个服务器 IP 可以绑定无限个域名
  2. 同端口多网站:80 端口可以服务多个网站(通过域名区分)
  3. HTTP Host 头:浏览器请求时会带上域名,Nginx 据此路由
  4. 无需代码修改:前端代码无需改动,自然适配域名访问
  5. DNS 子域名:使用主域名的子域名,无需额外购买

Leave a Comment

您的邮箱地址不会被公开。 必填项已用 * 标注