跳转到主要内容
你行网

Main navigation

  • 首页
  • Drupal
  • 编程与开发
  • 数据库
  • 操作系统与应用
  • 服务器与运维
  • 社区
User account menu
  • 登录

面包屑

  1. 首页

nginx 多站点配置方法

由 hrs, 14 四月, 2026

这是一个非常经典的“多站点”场景。如果你的服务器上需要同时运行多个网站(网站A、网站B、网站C),最佳做法**不是**把所有网站都塞进一个配置文件里,而是利用 Nginx 的 **`sites-available` 和 `sites-enabled`** 机制,为每个网站创建独立的配置文件。

这样做的好处是:互不干扰、管理清晰、配置简单。

假设你的目录结构如下:
*   `/var/www/html/siteA/web` (Drupal 站点 A)
*   `/var/www/html/siteB/web` (Drupal 站点 B)
*   `/var/www/html/siteC/web` (Drupal 站点 C)

请按照以下步骤操作:

### 第一步:清理旧配置

首先,建议把你现在这个包含了 `default_server` 的通用配置文件恢复原样或删除,避免冲突。我们为每个站点单独建文件。

### 第二步:为每个网站创建独立配置

你需要决定如何区分这些网站。通常有两种方式:
1.  **端口区分**:A用8080,B用8081,C用8082。
2.  **域名区分**(推荐):都使用80端口,但通过 `server_name` 区分(如 `sitea.test`, `siteb.test`)。

这里我以**端口区分**为例(因为你之前的配置用的是8080),演示如何配置:

#### 1. 创建网站 A 的配置文件
新建文件:`/etc/nginx/sites-available/siteA.conf`

```nginx
server {
   listen 8080;  # 网站 A 监听 8080 端口
   # listen [::]:8080;

   server_name localhost; # 或者你的域名 sitea.com

   # 指向网站 A 的 web 目录
   root /var/www/html/siteA/web;
   index index.php index.html;

   # Drupal 标准配置
   location / {
       try_files $uri $uri/ /index.php?$query_string;
   }

   # 禁止访问私有文件
   location ~ ^/sites/.*/private/ {
       deny all;
   }

   # 禁止访问敏感文件
   location ~* \.(bak|config|sql|tar|tgz|zip|sh|engine|inc|info|install|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$ {
       deny all;
   }
   
   location ~ /\. {
       deny all;
   }

   # PHP 处理
   location ~ '\.php$|^/update.php' {
       include snippets/fastcgi-php.conf;
       fastcgi_pass unix:/run/php/php8.4-fpm.sock;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
   }

   # 静态资源缓存
   location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
       expires max;
       log_not_found off;
   }
}
```

#### 2. 创建网站 B 的配置文件
新建文件:`/etc/nginx/sites-available/siteB.conf`

```nginx
server {
   listen 8081;  # 网站 B 监听 8081 端口,必须不同
   # listen [::]:8081;

   server_name localhost;

   # 指向网站 B 的 web 目录
   root /var/www/html/siteB/web;
   index index.php index.html;

   location / {
       try_files $uri $uri/ /index.php?$query_string;
   }

   # ... (复制网站 A 中剩余的 location 规则,包括 PHP 和安全规则) ...
   
   location ~ '\.php$|^/update.php' {
       include snippets/fastcgi-php.conf;
       fastcgi_pass unix:/run/php/php8.4-fpm.sock;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
   }
}
```

#### 3. 创建网站 C 的配置文件
新建文件:`/etc/nginx/sites-available/siteC.conf`

```nginx
server {
   listen 8082;  # 网站 C 监听 8082 端口
   # listen [::]:8082;

   server_name localhost;

   # 指向网站 C 的 web 目录
   root /var/www/html/siteC/web;
   
   # ... (其余配置同上) ...
   
   location ~ '\.php$|^/update.php' {
       include snippets/fastcgi-php.conf;
       fastcgi_pass unix:/run/php/php8.4-fpm.sock;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
   }
}
```

### 第三步:启用配置

创建了配置文件后,需要建立软链接到 `sites-enabled` 目录来启用它们:

```bash
# 启用网站 A, B, C
sudo ln -s /etc/nginx/sites-available/siteA.conf /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/siteB.conf /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/siteC.conf /etc/nginx/sites-enabled/

# 删除原来的默认配置(如果不再需要),防止冲突
sudo rm /etc/nginx/sites-enabled/default
```

### 第四步:测试并重启

```bash
# 测试配置是否有语法错误
sudo nginx -t

# 重载 Nginx
sudo systemctl reload nginx
```

### 结果

现在,你的服务器可以同时运行三个网站了:
*   访问 `http://127.0.0.1:8080` -> 打开网站 A
*   访问 `http://127.0.0.1:8081` -> 打开网站 B
*   访问 `http://127.0.0.1:8082` -> 打开网站 C

### 补充:如果你是想用子目录方式

如果你是想通过 `http://127.0.0.1:8080/siteA`、`http://127.0.0.1:8080/siteB` 这样访问,而不是用端口区分,那么配置会复杂很多,且对 Drupal 的兼容性不如端口方式好(涉及到 URL 重写路径问题)。

**推荐做法依然是:**
*   **开发环境**:使用不同端口(如上所示)。
*   **生产环境**:使用不同域名(如 `siteA.com` 指向服务器 IP,Nginx 配置中 `server_name siteA.com;`,所有站点都监听 80 端口)。

这样管理最清晰,每个站点的 `.conf` 文件相互独立,互不影响。

  • 登录或注册以发表评论
  • 1次浏览
内容类型分类
服务器与运维
文章归类
Linux

文章标签

  • nginx

评论

最新文章

  • 配置一个完整的Web服务器环境(LNMP:Linux + Nginx + MySQL + PHP)。
  • Nginx 各种应用场景
  • 什么是无头cms
  • 什么是站群
  • drupal 10如何配置nginx 服务器
  • nginx 多站点配置方法
  • OpenClaw 帮助文件的翻译版本
  • OpenClaw AI助手成本优化完全指南:从每天1900万Token降到合理范围
  • drupal 10 如何导出分类术语
  • vim 复制粘贴的几种方法

标签云

adsenseAIApacheaptbadblocksbreadcrumbbrewcentoscertbotckeditorcommandcomposercookiecsharpCSScurlC语言DDEVdiffdnsdockerDreamweaverDrupaldrupal 7drupal 8drupal 9drupal10drupal 11drushExcelfirewalldfirmwareflameshotformgimpgitgzipHTMLHTML5httpdhttp验证inputipjavaJavaScriptJavaSrciptkernelKVMLinuxmavenmbstringmod_expires 模块MysqlnerdtreenetstatnetworknginxnpmOpenClawpasswordphpphpmyadminphp扩展RFIDRSSselinuxSEOsharesimple_adsensesshsslStatisticssuperfishsurroundtagcloudstitleubuntuuploadprogressvimVPNVUEweb服务器WireGuardwpsxdebugyoutubeyumzip主题主题(theme)二进制五笔分类术语压缩解压哈希值声音字段密码工作流快捷键摄影无头cms更新权限果树种植标签优化模块模块(module)源地址版本号电子秤电脑基础电脑技巧短信验证站群端口简介算法网站备份网站安全网站运营翻译英语表单视图(views)计算机基础赚钱超五类线网线邮件验证重定向重定向,301错误颜色

相关文章

  • 配置一个完整的Web服务器环境(LNMP:Linux + Nginx + MySQL + PHP)。
  • Nginx 各种应用场景
  • 什么是无头cms
  • 什么是站群
  • drupal 10如何配置nginx 服务器
  • linux-firmware 是什么,有哪些作用?
  • journalctl 的使用和简介
  • linux 网络检查命令
  • 在drupal 使用composer一次性安装多个模块
  • composer update 命令的参数列表
RSS源

关于我们

  • 你行网简介
  • 关于我们
  • 版权声明

网站相关

  • 社区论坛
  • 站点反馈
  • 网址导航
  • 网站地图

友情链接

  • 申请链接
  • 英文学习

友情链接2

  • drupal 大学
  • 水滴间
  • 爱码网
  • Apache

友情链接3

  • MySQL
  • php
  • drupalcode

友情链接4

  • Drupal 中国
  • Drupal 老葛
  • 宁浩网
  • drupal 台湾
Copyright © 2019 - 2021 你行网 版权所有  粤ICP备19072650号-1