You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Docker中配置两个CodeIgniter模块站点?

双CodeIgniter站点Docker迁移及Nginx反向代理配置方案

一、调整Docker内Apache的sites.conf配置

原Apache配置仅指向根目录,需添加URL重写规则,让CodeIgniter能正确识别两个模块的请求路径。修改后的完整配置如下:

<VirtualHost *:80>
    ServerName 172.22.0.3 # Docker容器IP
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    # 启用URL重写模块
    RewriteEngine On

    # 将site1路径请求映射到对应模块入口
    RewriteRule ^/site1/(.*)$ /index.php/site1/$1 [L]
    # 将site2路径请求映射到对应模块入口
    RewriteRule ^/site2/(.*)$ /index.php/site2/$1 [L]

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

完成修改后,在Docker容器内启用重写模块并重启Apache:

a2enmod rewrite
service apache2 restart

二、完善Nginx反向代理配置

1. Site2的Nginx配置

复制你已有的Site1配置,修改域名、日志路径及代理地址,同时补充必要的代理请求头:

# Site2 HTTPS服务配置
server {
    listen              443 ssl http2;
    server_name         site2.domain.com;

    proxy_intercept_errors on;
    include /usr/share/nginx/html/nginx-errors-ptbr/nginx-errors.conf;

    # SSL证书(若使用泛域名证书可与Site1复用)
    ssl_certificate     /etc/nginx/ssl/mykey_bundle.crt;
    ssl_certificate_key /etc/nginx/ssl/mykey.key;

    # 日志配置
    access_log          /var/log/nginx/site2.access.log;
    error_log           /var/log/nginx/site2.error.log warn;

    # 反向代理到Apache的site2路径
    location / {
        proxy_pass http://IP_HOST_DOCKER:8085/site2;
        # 传递请求头,确保CodeIgniter获取正确的请求信息
        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;
    }
}

# Site2 HTTP自动跳转HTTPS
server {
    listen      80;
    server_name site2.domain.com;
    return      301 https://site2.domain.com$request_uri;
}

2. 优化Site1的Nginx配置

给已有Site1配置补充相同的代理请求头,避免CodeIgniter无法获取正确的客户端信息:

server {
    listen              443 ssl http2;
    server_name         site1.domain.com;

    proxy_intercept_errors on;
    include /usr/share/nginx/html/nginx-errors-ptbr/nginx-errors.conf;

    ssl_certificate     /etc/nginx/ssl/mykey_bundle.crt;
    ssl_certificate_key /etc/nginx/ssl/mykey.key;

    access_log          /var/log/nginx/site1.access.log;
    error_log           /var/log/nginx/site1.error.log warn;

    location / {
        proxy_pass http://IP_HOST_DOCKER:8085/site1;
        # 新增代理请求头
        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;
    }
}

server {
    listen      80;
    server_name site1.domain.com;
    return      301 https://site1.domain.com$request_uri;
}

三、CodeIgniter模块配置调整(可选但建议)

分别修改两个模块的config.php,设置正确的站点根URL:

  • Site1:application/modules/site1/config/config.php
$config['base_url'] = 'https://site1.domain.com/';
  • Site2:application/modules/site2/config/config.php
$config['base_url'] = 'https://site2.domain.com/';

四、验证配置并重启服务

  1. 重启Docker内的Apache服务,确保配置生效;
  2. 将Nginx的两个站点配置文件(如site1.conf、site2.conf)放入Nginx的conf.d目录;
  3. 测试Nginx配置合法性:
nginx -t
  1. 重启Nginx服务:
systemctl restart nginx

完成以上配置后,访问https://site1.domain.com和https://site2.domain.com即可分别对应Docker内的两个CodeIgniter模块站点,HTTP请求会自动跳转至HTTPS。

内容的提问来源于stack exchange,提问作者campos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 11:57:50