【问题标题】:Install wordpress in subdirectory with docker-compose and nginx使用 docker-compose 和 nginx 在子目录中安装 wordpress
【发布时间】:2021-08-23 02:38:00
【问题描述】:

我打算用 docker-compose 建一个网站。

当用户进入根目录时,它会进入网络应用程序。当用户输入http://example.com/blog时,它应该重定向到wordpress。

为了做到这一点,我已经像这样配置 docker-compose

version: "3"

services:

  nginx:
    image: nginx:latest
    depends_on:
      - web-app
      - wordpress
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
    ports:
      - 80:80

  mysql:
    image: mysql:5.7.29
    container_name: mysql
    restart: always
    volumes:
      - ./data/mysql/data:/var/lib/mysql

  wordpress:
    depends_on:
      - mysql
    image: wordpress:latest
    ports:
      - 8000:80
    restart: always
    volumes:
     - ./data/blog:/var/www/html/wp-content
     - ./blog/wp-config.php:/var/www/html/wp-config.php

  web-app:
    build: ./app
    depends_on:
      - mysql
    restart: always
    command: npm start
    environment:
      - TZ=UTC
      - NODE_ENV=production

networks:
  default:
    external:
      name: common

这是我的 nginx 配置

worker_processes auto;
events {
    worker_connections 1024;
}
http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    log_format main '[$time_local] $remote_addr - $request request_time: $request_time';
    access_log /var/log/nginx/access.log main;

    gzip on;
    gzip_http_version 1.1;
    gzip_vary on;
    gzip_comp_level 6;
    gzip_proxied any;
    gzip_types text/plain text/html text/css application/json application/javascript application/x$
    #                        make sure gzip does not lose large gzipped js or css files
    #                        see http://blog.leetsoft.com/2007/07/25/nginx-gzip-ssl.html
    gzip_buffers 16 8k;

    server {
        listen 80;
        charset utf-8;

        location /blog/ {
            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_pass http://wordpress;
        }

        location / {
            proxy_http_version 1.1;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_redirect off;
            proxy_pass http://web-app:3000;
        }

    }
} 

我已经更新了 wp-config.php 并设置了网站 url。

define('WP_SITE_URL', 'https://example.com/blog');
define('WP_SITEURL', 'https://example.com/blog');
define('WP_HOME', 'https://example.com/blog');

但是,当我输入 https://example.com/blog 时,它会返回 ERR_TOO_MANY_REDIRECTS 响应。

我搜索了很多文章,其中一些说是因为我使用的是 cloudflare。如何解决上述问题?

【问题讨论】:

  • 这不是一个好的解决方案,但它可以帮助你一段时间,直到你找到原因:remove_action( 'template_redirect', 'redirect_canonical' ); 将此行添加到functions.php 文件中。

标签: wordpress docker nginx docker-compose


【解决方案1】:

在你的 config.php 中你这样做:

// Define the website URL
define('WP_SITEURL', getenv_docker('WORDPRESS_SITEURL', 'https://example.com/blog'));

// Define the website Home page
define('WP_HOME',    getenv_docker('WORDPRESS_HOME', 'https://example.com/blog'));

// Rewrite your access URL to the admin page
$_SERVER['REQUEST_URI'] = str_replace("/wp-admin/", "/blog/wp-admin/",  $_SERVER['REQUEST_URI']);

如果你没有版本化你的wordpress资源,直接使用像这样的docker compose变量,而不是使用config.php文件:

WORDPRESS_CONFIG_EXTRA: |
   define('WP_HOME','https://example.com/blog');
   define('WP_SITEURL','https://example.com/blog');
   $$_SERVER['REQUEST_URI'] = str_replace("/wp-admin/", "/blog/wp-admin/",  $$_SERVER['REQUEST_URI']);

并且在文件 docker-compose.yml 中放置以下环境变量:

environment:
  ...
  - WORDPRESS_SITEURL="https://example.com/blog"
  - WORDPRESS_HOME="https://example.com/blog"

【讨论】:

    猜你喜欢
    • 2016-03-24
    • 2019-09-04
    • 2018-01-15
    • 2015-08-18
    • 2019-02-17
    • 2020-09-05
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多