在 Docker 容器环境中设置 Wordpress 站点时出错

Error setting Wordpress site in Docker container environment

我想用三个容器设置一个 docker 环境:一个用于 mysql 服务器绑定到端口 3306,一个用于 apache/letsencrypt 绑定到端口 80 和 443,一个用于托管一些 wordpress 网站,绑定到端口 8073。

我已经配置了这个 docker-compose 文件,并且对于每个容器都有一个安装相应图像的 Dockerfile:

version: "3.3"
services:
    mysql-server:
        build: 'mysql-server'
        container_name: mysql-server
        volumes:
            - db_volumes:/var/lib/mysql
        environment:
            MYSQL_ROOT_PASSWORD: test
        ports:
            - "3306:3306"
    apache-letsencrypt:
        build: 'apache-letsencrypt'
        container_name: apache-letsencrypt
        environment:
            WEBMASTER_MAIL: "example@example.it"
        volumes:
            - /etc/letsencrypt:/etc/letsencrypt
            - vhosts_volumes:/etc/apache2/sites-available
            - log_volumes:/var/log/apache2
        ports:
            - "80:80"
            - "443:443"
        network_mode: host
    wordpress_php7.2:
        build: 'php7.2'
        container_name: wordpress_php7.2
        environment:
            DB_SERVER: mysql-server
        depends_on:
            - mysql-server
        volumes:
            - /var/www/wordpress_php7.2:/var/www
            - vhosts_volumes:/etc/apache2/sites-available
            - log_volumes:/var/log/apache2
        ports:
            - "8073:80"
volumes:
    db_volumes:
        driver: local
        driver_opts:
            type: 'none'
            o: 'bind'
            device: '/var/www/databases'
    vhosts_volumes:
        driver: local
        driver_opts:
            type: 'none'
            o: 'bind'
            device: '/etc/apache2/sites-available'
    log_volumes:
        driver: local
        driver_opts:
            type: 'none'
            o: 'bind'
            device: '/var/log/apache2'

创建容器后,我将 wordpress 站点放入 /var/www/wordpress_php7.2/www.example.com 并在 mysql 容器中设置数据库。

此外,我在 apache-letsencrypt 容器中成功创建了 letsencrypt 证书。 所以我必须设置虚拟主机,并为一个站点创建三个虚拟主机: 我放在 wordpress 容器内的一个绑定在容器内的端口 80 上:

<VirtualHost *:80>
 ServerName www.example.com
 ServerAlias example.com
 ServerAdmin webmaster@localhost
 DocumentRoot /var/www/example.com

 <Directory /var/www/example.com>
 Options Indexes FollowSymLinks
 AllowOverride All
 </Directory>


 ErrorLog /var/log/apache2/example.com.error.log
 CustomLog /var/log/apache2/example.com.access.log combined

</VirtualHost>

apache-letsencrypt 容器中的其他两个虚拟主机绑定在端口 80 和 443 上:

<VirtualHost *:80>
  ServerName www.example.com

  ## Redirect rules
  Redirect permanent / https://www.example.com/

  ## Server aliases
  ServerAlias example.com
</VirtualHost>

<VirtualHost *:443>
        ServerName www.example.com
        ServerAlias example.com

        SSLEngine On
        SSLCertificateFile      "/etc/letsencrypt/live/www.example.com/cert.pem"
        SSLCertificateKeyFile   "/etc/letsencrypt/live/www.example.com/privkey.pem"
        SSLCertificateChainFile "/etc/letsencrypt/live/www.example.com/fullchain.pem"
        SSLCACertificatePath    "/etc/ssl/certs"

        SSLProxyEngine on
        ProxyPass / http://www.example.com:8073/
        ProxyPassReverse / http://www.example.com:8073/
        ProxyRequests Off
</VirtualHost>

最后,我在 docker 网络中传递请求,并在 /etc/hosts 路径中的 apache 容器中进行设置:

172.17.0.1 www.example.com

在 wp_option table 站点的 https 路径 (https://www.example.com) 中更改内部数据库站点后,我收到错误 too_many_redirects。

我进行了调试,如果我回显某些内容并在加载站点之前在 index.php 退出,一切正常,所以我想代码中有一些进程不起作用。

怎么了?

我为 wordpress 网站找到了这个解决方案:

将此指令插入绑定在端口 443 上的虚拟主机中:

RequestHeader set X-Forwarded-Proto https

识别客户端连接到代理所使用的协议(文档 -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Proto)。

之后我在 functions.php 主题文件的第一行插入了这段代码:

remove_action('template_redirect', 'redirect_canonical');

避免模板重定向。

现在似乎一切正常