使用 nginx 和 mariadb 在子目录中设置 wordpress 容器

使用 nginx 和 mariadb 在子目录中设置 wordpress 容器

好的伙计们,问题来了:我的网络服务器使用像这样的子目录方法为网站、cms 和移动应用程序 api 提供服务

  • 本地主机->网站
  • 本地主机/cms
  • 本地主机/api

一切都使用 docker compose 构建

services:
  server:
    build: ./docker/nginx
    links:
      - fpm
    ports:
      - 80:80
      - 443:443

  fpm:
    build: ./docker/php
    links:
      - database
      - smtp

  database:
    build: ./docker/mariadb
    volumes:
      - dbdata:/var/lib/mysql

volumes:
  dbdata:

我想要做的是添加两个新容器:一个用于 wordpress,一个用于 wordpress db为了代理将每个对 mydomain.dom/blog 的请求传递到 wordpress 容器。 我能够使用如下所示的 nginx 配置传递请求,但不幸的是,我正在与容器作斗争,它一直将所有内容重定向到 localhost 而不是 localhost/blog,我想我在 wordpress 中缺少一些配置。

location /blog/ {
    proxy_http_version 1.1;
    proxy_buffering off;
    proxy_read_timeout    90;
    proxy_connect_timeout 90;
    proxy_redirect        off;
    proxy_pass http://blog:80; # the wordpress container name

    proxy_set_header Host $host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $proxy_connection;
    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 $proxy_x_forwarded_proto;
    proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
    proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;

    # Mitigate httpoxy attack (see README for details)
   proxy_set_header Proxy "";
}

# Cms stuff
location /cms/ {
    try_files $uri $uri/ /staff/index.php?$args;

    include _php.conf; # php-fpm setup
}

# Site stuff
location / {

    try_files $uri $uri/ /index.php?$args;

    include _php.conf; # php-fpm setup
}

答案1

你需要告诉WordPress要使用的新基本 URL。最简单的方法是将以下两行添加wp-config.phpWordPress的基本目录:

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

答案2

初始化WordPress 的在类似的子目录中/blog,您可以使用working_dir属性。

默认WordPress 的图像/var/www/html用途/var/www/html/blog,此时您可以转至进行初始化。

# ...
  wordpress:
    image: wordpress:apache
    working_dir: /var/www/html/blog
    volumes:
      - ./data/blog:/var/www/html/blog/wp-content
      - ./blog/wp-config.php:/var/www/html/blog/wp-config.php
# ...

现在您可以导航至http://localhost/blog

完整示例

以下是完整docker-compose.yml示例:

version: '3.1'

x-wordpress: &wordpress
  depends_on:
    - db
  environment:
    WORDPRESS_DB_HOST: db:3306
    WORDPRESS_DB_NAME: wordpress
    WORDPRESS_DB_USER: wordpress
    WORDPRESS_DB_PASSWORD: wordpress
  working_dir: /var/www/html/blog
  volumes:
    - wp_root:/var/www/html/blog
    - ./wp-content/plugins:/var/www/html/blog/wp-content/plugins
    - ./wp-content/themes:/var/www/html/blog/wp-content/themes
    - ./wp-content/uploads:/var/www/html/blog/wp-content/uploads
    - ./wp-content/languages:/var/www/html/blog/wp-content/languages

services:
  # Database
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
      - ./backup/blog.sql:/docker-entrypoint-initdb.d/dump.sql:ro
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

  # phpmyadmin
  phpmyadmin:
    depends_on:
      - db
    image: phpmyadmin/phpmyadmin
    ports:
      - 8080:80
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: password

  # Wordpress
  wordpress:
    <<: *wordpress
    image: wordpress:apache
    ports:
      - 80:80

  wpcli:
    <<: *wordpress
    image: wordpress:cli

volumes:
  wp_root:
    driver_opts:
      type: tmpfs
      device: tmpfs
  db_data:

相关内容