我在Docker-Compose上运行Wordpress,docker-compose.yml
如下:
version: '2'
services:
db_wordpress:
image: mysql:5.7
volumes:
- db_wordpress_data:/var/lib/mysql
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: pwd
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: pwd
wordpress:
image: wordpress:latest
volumes:
- ./wp-content:/var/www/html/wp-content
restart: unless-stopped
expose:
- 80
depends_on:
- db_wordpress
environment:
WORDPRESS_DB_HOST: db_wordpress:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: pwd
WORDPRESS_DEBUG: 1
WORDPRESS_CONFIG_EXTRA: |
// Enable Debug logging to the /wp-content/debug.log file
define('WP_DEBUG_LOG', true);
// Disable display of errors and warnings
define('WP_DEBUG_DISPLAY', false);
// Handle subpath /blog
define('WP_HOME','https://www.example.com/blog');
define('WP_SITEURL','https://www.example.com/blog');
$$_SERVER['REQUEST_URI'] = '/blog' . $$_SERVER['REQUEST_URI'];
nginx:
image: nginx
restart: unless-stopped
ports:
- "80:80"
- "443:443"
links:
- wordpress
volumes:
- /var/www/letsencrypt:/var/www/letsencrypt
- ./logs/nginx:/var/log/nginx
- ./nginx.conf:/etc/nginx/nginx.conf
volumes:
db_wordpress_data:
因此,wordpress站点位于具有以下nginx.conf
文件的nginx
容器后面:
events {
worker_connections 1024;
}
http {
upstream wordpress {
server wordpress:80;
}
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server_names_hash_bucket_size 512;
proxy_cache_path /var/cache levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=1g;
server {
listen 80;
server_name example.com;
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 default_server ssl;
server_name example.com;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
try_files $uri $uri/ =404;
location /blog/ {
proxy_pass http://wordpress/;
proxy_buffering off;
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;
}
}
}
现在我可以访问我的wordpress管理员,安装插件等,但是当我想更新帖子或页面时,我在浏览器上收到500
错误,并查看日志(docker logs -f clearroad_wordpress_1
):
[Thu Jan 03 17:03:36.105145 2019] [core:error] [pid 97] [client 172.18.0.5:51210] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: https://www.example.com/blog/wp-admin/post.php?post=62&action=edit
172.18.0.5 - - [03/Jan/2019:17:03:36 +0000] "POST /wp-json/wp/v2/posts/62/autosaves?_locale=user HTTP/1.0" 500 806 "https://www.example.com/blog/wp-admin/post.php?post=62&action=edit" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko)
因此,似乎/blog/wp-json
上的请求最终进入了重定向循环,原因可能是什么?
我检查了wordpress容器中的.htaccess
文件:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
.htaccess
应该不会在你的WordPress安装中产生影响,因为你使用的是Nginx。
请参阅这些帖子以正确配置 Nginx 实例。有几种配方,具体取决于您是否使用 HTTPS、缓存插件等。
- https://codex.wordpress.org/Nginx
- https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/
由于您在Wordpress容器上代理到http,因此您正在重定向循环,但将Wordpress配置为在https上工作。
对于您的配置,请在快乐博客行之前将以下内容添加到您的WP配置中.php以解决它:
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false) {
$_SERVER['HTTPS'] = 'on';
}
/* That's all, stop editing! Happy blogging. */