![Traefik Wordpress 重定向](https://linux22.com/image/743794/Traefik%20Wordpress%20%E9%87%8D%E5%AE%9A%E5%90%91.png)
我有一个运行 Traefik 2.0 的 Docker Swarm 设置。我们目前在多台服务器上运行着许多 wordpress 网站,我们想将它们 docker 化。我们有一个在 Docker 上运行良好的 Traefik 实例,它为这些物理服务器提供文件提供程序,并路由到 Docker Wordpress 实例。这部分似乎运行良好。
我们已经配置好了要去的地方https://myblog.example.com这是 docker 中的一个 wordpress 实例,一切运行正常。然而老板希望它转到https://example.com/myblog。我以为这会很简单但它变得越来越有挑战性。
我使用 Traefik 中的 Host && PathPrefix 规则对其进行了配置,并且它可以按预期工作,但是当它到达 Wordpress 时,它会重定向到 wp-admin/install.php 进行安装大约 20 多次,直到由于重定向次数过多而出错。
我读到的所有内容都表明是 SSL 直通导致了这些问题。我尝试过使用 SSLProxyHeaders(您会在下面的配置中看到它的残余部分),我发现 Traefik 2.0 文档与旧版本相比相当缺乏。我预料到这种情况,因为它最近才发布。
经过几天的折腾,我决定删除 http->https 重定向,只转到我的网站 http 进行测试。它也收到太多重定向。对我来说,这不是 SSL 问题,而是 Wordpress 和/或 traefik 配置问题。我倾向于使用 traefik 配置,因为它可以很好地转到 http(s)://myblog.example.com
docker-compose.yml:
version: '3'
services:
db:
image: mysql:5.7
volumes:
- /mnt/docker/data/wp-myblog/db/:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: XXXXXXXXXXXXXXXXXX
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
wordpress:
wordpress:
image: wordpress:latest
restart: always
volumes:
- /mnt/docker/data/wp-myblog/wordpress:/var/www/html
deploy:
labels:
- traefik.enable=true
- traefik.http.routers.wp-myblog-https.rule=Host(`example.com`) && PathPrefix(`/myblog`)
- traefik.http.routers.wp-myblog-http.rule=Host(`example.com`) && PathPrefix(`/myblog`)
- traefik.http.routers.wp-myblog-http.entrypoints=http
- traefik.http.routers.wp-myblog-https.entrypoints=https
- traefik.http.routers.wp-myblog-https.middlewares=sslheaders@file
- traefik.http.services.wp-myblog.loadbalancer.server.port=80
- traefik.http.routers.wp-myblog-https.tls
- traefik.docker.network=traefik_default
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_CONFIG_EXTRA: |
define('WP_SITEURL', 'http://example.com/myblog');
define('WP_HOME','http://example.com/myblog');
networks:
traefik_default:
wordpress:
networks:
wordpress:
traefik_default:
external: true
wp-config.php:
wp-config.php:
<?php
/**
* The base configuration for WordPress
*
* The wp-config.php creation script uses this file during the
* installation. You don't have to use the web site, you can
* copy this file to "wp-config.php" and fill in the values.
*
* This file contains the following configurations:
*
* * MySQL settings
* * Secret keys
* * Database table prefix
* * ABSPATH
*
* @link https://codex.wordpress.org/Editing_wp-config.php
*
* @package WordPress
*/
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress');
/** MySQL database username */
define( 'DB_USER', 'wordpress');
/** MySQL database password */
define( 'DB_PASSWORD', 'wordpress');
/** MySQL hostname */
define( 'DB_HOST', 'db:3306');
/** Database Charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '');
/**#@+
* Authentication Unique Keys and Salts.
*
* Change these to different unique phrases!
* You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
* You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
*
* @since 2.6.0
*/
define( 'AUTH_KEY', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
define( 'SECURE_AUTH_KEY', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
define( 'LOGGED_IN_KEY', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
define( 'NONCE_KEY', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
define( 'AUTH_SALT', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
define( 'SECURE_AUTH_SALT', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
define( 'LOGGED_IN_SALT', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
define( 'NONCE_SALT', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
/**#@-*/
/**
* WordPress Database Table prefix.
*
* You can have multiple installations in one database if you give each
* a unique prefix. Only numbers, letters, and underscores please!
*/
$table_prefix = 'wp_';
/**
* For developers: WordPress debugging mode.
*
* Change this to true to enable the display of notices during development.
* It is strongly recommended that plugin and theme developers use WP_DEBUG
* in their development environments.
*
* For information on other constants that can be used for debugging,
* visit the Codex.
*
* @link https://codex.wordpress.org/Debugging_in_WordPress
*/
define( 'WP_DEBUG', false );
// If we're behind a proxy server and using HTTPS, we need to alert Wordpress of that fact
// see also http://codex.wordpress.org/Administration_Over_SSL#Using_a_Reverse_Proxy
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
$_SERVER['HTTPS'] = 'on';
}
// WORDPRESS_CONFIG_EXTRA
define('WP_SITEURL', 'http://example.com/myblog');
define('WP_HOME','http://example.com/myblog');
/** That's all, stop editing! Happy publishing. */
/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', dirname( __FILE__ ) . '/' );
}
/** Sets up WordPress vars and included files. */
require_once( ABSPATH . 'wp-settings.php' );
答案1
在 ./wordpress/wp-content 中创建一个名为 blog 的目录,并将所有数据移到其中。然后让你的 docker compose 看起来像
version: '3.7'
networks:
http_network:
external: true
wp:
external: false
services:
database:
image: mariadb:latest
command: ['--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci', '--default-authentication-plugin=mysql_native_password']
volumes:
- .docker/data/db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
networks:
- wp
wordpress:
depends_on:
- database
image: wordpress:latest
volumes:
- ./wordpress/wp-content:/var/www/html/wp-content
environment:
WORDPRESS_DB_HOST: database:3306
WORDPRESS_DB_USER: ${MYSQL_USER}
WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
WORDPRESS_DB_NAME: ${MYSQL_DATABASE}
labels:
- traefik.enable=true
- traefik.frontend.rule=Host:domain;PathPrefix:/blog
- traefik.port=80
- traefik.docker.network=http_network
working_dir: /var/www/html/wp-content/blog
networks:
- wp
- http_network
现在它有一个奇怪的怪癖,当你第一次配置 wordpress 网站时,你必须转到 https://domain/blog/blog 来完成安装过程。完成后,你应该能够转到 https://domain/blog 并正常访问它。我遇到了同样的问题,/blog/wp-admin 会重定向删除 /blog,但现在却没有。